我有一个需要执行一些业务逻辑的处理程序,我希望它在单独的线程池中执行,以免阻塞 io 事件循环。我已将 DefaultEventExecutorGroup 添加到管道中,如http://netty.io/4.0/api/io/netty/channel/ChannelPipeline.html javadoc 和http://netty.io/wiki/new-and-noteworthy-in -4.0.html#no-more-executionhandler---它的核心维基:
ch.pipeline().addLast(new DefaultEventExecutorGroup(10), new ServerHandler());
Run Code Online (Sandbox Code Playgroud)
仅出于测试目的,我的 ServerHandler 只是让当前线程休眠 5 秒:
protected void channelRead0(ChannelHandlerContext ctx, Command cmd) throws Exception {
System.out.println("Starting.");
try {
Thread.currentThread().sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Finished.");
}
Run Code Online (Sandbox Code Playgroud)
但显然业务逻辑仍然是同步执行的:
Starting.
Finished.
Starting.
Finished.
Starting.
Finished.
Run Code Online (Sandbox Code Playgroud)
我错过了什么?