- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
[问题]:我有一个 Netty 服务器,它将通过 TCP 监听客户端,处理请求并发回响应。问题是处理部分需要时间,因为它需要应用程序用户的验证。客户端接连发送多个请求,而服务器似乎在前一个请求完成之前才接受客户端的请求。
我已经复制了这样的场景:客户端将一个接一个地发送几个请求,服务器将花费一些时间来接受请求并进行处理。通过将阻塞操作传递给另一个线程并完成剩余的操作,我可以很好地处理单个请求阻塞请求。但这仍然不接受来自客户端的第二个请求。它仍然会等待阻塞操作完成,然后再接受来自客户端的另一个请求。
客户端代码
int count =0;
long currentTime = System.currentTimeMillis();
long trigger = currentTime + 5000;
long stop_loop = currentTime + 130000;
while(count<2){
boolean loop = true;
while(loop){
if(System.currentTimeMillis()>trigger){
trigger += 5000;
count++;
loop = false;
}
}
System.out.println("SENDING FOR COUNTER - "+count);
content = Unpooled.copiedBuffer(b);
System.out.println("Channel Active Method Called: "+ctx);
ctx.write(content);
ctx.flush();
}
服务器代码:
public void channelRead(ChannelHandlerContext ctx, Object msg) {
final ExecutorService blockingThreadPool = Executors.newFixedThreadPool(10);
try {
System.out.println("server receive orde:"+body+"the counter is:" + ++counter);
ctx.executor().execute(new Runnable(){
public void run(){
try{
new MyBusinessLogicHandler(blockingThreadPool).channelRead(ctx , b_buf);
}
catch(Exception e ){
logger.error(e.getMessage());
}
}
}
//ByteBuf resp = Unpooled.copiedBuffer(currentTime,CharsetUtil.UTF_8);
//ByteBuf resp = Unpooled.wrappedBuffer(response.getBytes());
//ctx.writeAndFlush(resp);
// ctx.write(resp);
//ctx.flush();
}
我的业务逻辑
@Override
public void channelRead(ChannelHandlerContext ctx, Object arg1)
throws Exception{
System.out.println(" INSIDE BUSINESS HANDLER - OBJ");
System.out.println(arg1);
String x = ctx.channel().attr(DiscardServerHandler.CHECKSUMKEY).get();
System.out.println("X -"+x);
ChannelHandlerContext temp = ctx;
resp = Unpooled.wrappedBuffer(x.getBytes());
ctx.write(resp);
ctx.flush();
ByteBuf buf = (ByteBuf) arg1;
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
String body = new String(req,CharsetUtil.UTF_8);
System.out.println(new Date());
System.out.println("Business logic receive order : " + body);
System.out.println("SERVER LISTENER READ");
boolean loop = true;
long currentTime = System.currentTimeMillis();
long trigger = currentTime + 3000;
long stop_loop = currentTime + 20000;
int count = 0;
System.out.println("Server READ");
// String response = "Server Complete Response ";
while(loop){
long now = System.currentTimeMillis();
if(now<stop_loop){
if(now>trigger){
count++;
System.out.println("Now is triggered @" + count + "-- "+(count*5)+"secs passed");
trigger += 5000;
}
}
else if(now>stop_loop){
loop = false;
System.out.println("Loop Completes");
}
}
String re = "BUSINESS LOGIC RESPONSE";
resp = Unpooled.wrappedBuffer(re.getBytes());
temp.write(resp);
temp.flush();
}
我想要的只是接受所有客户端请求,无论之前的请求是否已完成/未完成。
最佳答案
这是一种经典模式,您需要从同步服务切换到异步(“异步”)服务。异步服务的想法是,您接收来自客户端的请求,然后验证它并将其存储在服务器上的某些存储中(通常是队列)中的“待办事项”列表中。然后返回响应(在本例中称为确认(“ack”))。在您的响应中,您向客户返回其请求的 ID 以及返回答案的媒介。 (通常是一个队列)。这样客户就会知道在哪里收听结果。同时,您现在可以在服务器上处理收到的请求,而无需阻止客户端。您还可以拥有多个“工作人员”实例,它们从存储中获取传入请求并进行处理。处理请求后,您的工作进程会使用您在“ack”中返回给客户端的相同 ID 将其发布到您在“ack”中返回给客户端的介质(队列)。欲了解更多信息,请看这里:Asynchronous vs synchronous execution, what does it really mean?
关于java - Netty - 阻塞操作在完成之前不接受客户端请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57848087/
我正在尝试使用 Netty 构建一个反向代理,并且我想保留一个到后端服务器的开放套接字池,而不是每个传入套接字都需要一个从反向代理到后端服务器的新套接字。 你能用 Netty 做到这一点吗?如何? 谢
从 Netty 3.5.x 到 Netty 4 性能提升了多少?有数据吗? 最佳答案 目前没有太大改进。不过,它的 GC 开销要少得多。一旦实现缓冲池,我相信吞吐量也会变得更好。目前,吞吐量增益约为
我正在尝试关闭与它建立连接的 Netty 服务器,但它只是挂起。这就是我所做的。 在一台机器上启动服务器,在另一台机器上启动客户端。 从客户端向服务器发送一条消息,我得到响应。 使用 Ctrl-C 关
doc说“每个轮子的默认滴答数(即轮子的大小)是 512。如果你要安排很多超时,你可以指定一个更大的值。” 这是否意味着默认情况下它只能处理 512 次超时?如果我想要 25 秒的 10 万次超时(对
我正在使用 netty 4.0.25Final 编写一个 netty HTTP 服务器。我需要根据 HTTP GET 请求中的一些参数在管道中添加各种处理程序。 pipeline.addLast(ne
我现在将 Netty 用于一些服务器端组件有一段时间了,我对此感到非常满意。因此,为了我自己的方便,我还想在客户端使用它,但我想保持小程序的占用空间(在这种情况下)尽可能小。我需要从 Netty 那里
有没有办法告诉 netty 停止监听和接受套接字上的新连接,但要完成当前连接上的任何正在进行的工作? 最佳答案 您可以关闭 ServerSocketChannel创建者 ChannelFactory
我用响应式(Reactive) mongo 创建了简单的 Webflux (kotlin) 应用程序。 Controller 有一个 GET 方法,即返回 Flow(来自一个集合的 2 个对象)。 我
我有一个新项目,我将第一次使用 Netty (v4.0.4)。我将拥有一个拥有数万个连接客户端的服务器。服务器将向这些客户端发送命令并应该接收响应。 查看 API 和在线示例,我不确定如何从服务器的角
与 boost.asio 不同,netty 没有类似 read 的方法。以下情况不方便:管理节点管理一些节点,客户端连接到管理节点以检索驻留在节点中的信息。当管理节点收到客户端的请求后,向对应的节点发
我正在编写一个应用程序,其中客户端和服务器都是使用 Netty 编写的,并且服务器应该(显然)同时支持多个客户端。我试图通过创建 1000 个客户端共享一个 EventLoopGroup 并在一台机器
如果我在 Netty 101 期间睡着了,请原谅我,但我想知道是否有一种“正确”的方式来等待 Netty 完成多步骤连接过程。假设我有一个应用程序,其过程如下所示: 打开实际连接。 执行 TLS 握手
将 Netty ChannelBuffer 转换为 String 就像在 ChannelBuffer 上调用 .toString(UTF_8) 一样简单。如何从字符串创建 ChannelBuffer?
在 Netty 3 中,我们可以这样做: Channel.setReadable(false); Channel.setReadable(true); 我读了: http://netty.io/new
我知道 Storm 现在运行在 Netty 上用于节点之间的通信? Apache Spark 是否也使用 Netty?如果真是这样,那么是以哪种方式? 最佳答案 Spark使用Akka Actor进行
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。如需帮助澄清这个问题以便重新打开它,visit the help center .
我真的很困惑老板组的线程数。我想不出我们需要多个老板线程的情况。在 do we need more than a single thread for boss group? Netty 的创建者说,如
我已将其添加到我的管道中,并且 LoggingHandler 正在捕获其事件,但是由于事件系统从 Netty 3 更改为 4,我该如何处理这些事件,因为 IdleStateAwareUpstreamH
有没有办法在 channel 上保持状态。我正在编写一个聊天服务器,我想保留有关 channel 所属用户的信息。我在想也许 Channel 会提供一种方法来存储用户对象,但我看不到。有没有办法在不需
我有一个 netty channel ,我想在底层套接字上设置超时(默认设置为 0 )。 超时的目的是,如果 15 分钟内没有发生任何事情,则未使用的 channel 将被关闭。 虽然我没有看到任何配
我是一名优秀的程序员,十分优秀!