Netty:如何确保I/O线程触发了Channel.close()
我使用的是Netty 3.6.2,以下是我的管道工厂伪代码:Netty:如何确保I/O线程触发了Channel.close(),netty,Netty,我使用的是Netty 3.6.2,以下是我的管道工厂伪代码: private final static ThreadPoolExecutor executor = new OrderedMemoryAwareThreadPoolExecutor(8, 4194304, 4194304, 5L, TimeUnit.MINUTES); public ChannelPipeline getPipeline() throws Exception { ChannelPipeline p = pip
private final static ThreadPoolExecutor executor = new OrderedMemoryAwareThreadPoolExecutor(8, 4194304, 4194304, 5L, TimeUnit.MINUTES);
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline p = pipeline();
p.addLast("frameDecoder", protobufFrameDecoder);
p.addLast("protobufDecoder", protobufDecoder);
p.addLast("executor", new ExecutionHandler(executor));
p.addLast("handler", handler);
p.addLast("frameEncoder", protobufFrameEncoder);
p.addLast("protobufEncoder", protobufEncoder);
return p;
}
这样,处理程序的messageReceived()在不同的线程池中被调用,而不是在工作线程池中被调用,现在我想关闭通道,以防messageReceived()中发生异常,但根据此处:
作为下游事件副作用触发的任何上游事件
必须从I/O线程触发
简单地调用ctx.getChannel().close()在exceptionCaught()中是不安全的,我正试图用这种方法来解决这个问题
NettyServerSocketFactory.getWorkerExecutor().execute(new Runnable() {
@Override
public void run() {
channel.close();
}
});
以下是NetyServerSocketFactory代码:
public class NettyServerSocketFactory extends NioServerSocketChannelFactory {
private static Executor bossExecutor = Executors.newCachedThreadPool();
private static Executor workerExecutor = Executors.newCachedThreadPool();
public static Executor getBossExecutor() {
return bossExecutor;
}
public static Executor getWorkerExecutor() {
return workerExecutor;
}
public NettyServerSocketFactory() {
super(bossExecutor, workerExecutor);
}
}
但它似乎不起作用,任何建议都将受到欢迎。Channel#close()触发一个下游事件,该事件将最终到达ChannelSink,在此事件将“移交”给与频道相关的工作人员进行进一步处理。工作线程最终将触发一个通道关闭事件,并且工作线程将确保该事件在IO线程的上游发送
这就是它当前的工作方式,可能您所指的文档正在讨论以前的情况,即事件确实是在调用线程上传递的。Channel#close()触发一个下游事件,该事件将最终到达ChannelSink,在那里事件被“移交”发送给与通道关联的工作进程以进行进一步处理。工作线程最终将触发一个通道关闭事件,并且工作线程将确保该事件在IO线程的上游发送
这就是它目前的工作方式,可能您所指的文档正在讨论以前的情况,其中事件确实是在调用线程上交付的