我最近开始使用JBoss Netty,到目前为止我的理解是,channelPipelineFactory用于创建一个ChannelPipeline,每次服务器收到请求。一个ChannelPipeline包含一系列处理请求的ChannelHandler。现在我的问题是,如果我的管道中的一个处理程序需要从数据库获取数据,这是阻塞I/O。请求的处理被阻止?这与通过说Servlet的常规请求处理有什么不同?我对来自NodeJS的事件驱动异步I/O的理解是,有一个事件循环,并且有一系列回调函数注册用于阻止I/O操作,并且只要I/O完成就会调用这些函数。 Netty中的等价物是什么?Netty:回拨在哪里?
private static final HttpResponseEncoder httpResponseEncoder = new HttpResponseEncoder();
private static final JsonEncoder jsonEncoder = new JsonEncoder();
private static final ExecutionHandler executionHandler = new ExecutionHandler(
new OrderedMemoryAwareThreadPoolExecutor(5, 1048576, 1048576));
public static void main(String[] args) throws Exception {
ChannelFactory factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
SocketAddress sockAddress = new InetSocketAddress(8080);
RedisClient client = new RedisClient("127.0.0.1");
final RedisAsyncConnection<String, String> connection = client.connectAsync();
ServerBootstrap bootstrap = new ServerBootstrap(factory);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("executionHandler", executionHandler);
pipeline.addLast("weightedRandomGenerator", new WeightedRandomNumberGenerator(
connection));
pipeline.addLast("encoder", httpResponseEncoder);
pipeline.addLast("JsonConverter", jsonEncoder);
return pipeline;
}
});
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
bootstrap.bind(sockAddress);
}
好吧,我没有看到,并做了一个简单的测试,在我的IDE我把我的阻止代码断点。当我发出请求时,中断点被触发,现在线程被有效阻止。但是,如果我发出另一个请求,是否应该不处理,我希望现在有两个线程停在同一个断点处。然而,这不是我所观察到的。相反,我所看到的是第一个请求甚至没有被主IO-Thread拾取。 – VDev 2013-03-08 05:23:17
什么是工作线程?如果所有处理程序在主EventLoop或Boss线程中同步执行?我错过了什么? – VDev 2013-03-08 05:25:15
我已经包含了我的代码.. – VDev 2013-03-08 05:47:35