在这个问题
Netty Comet Async request time out
有人建议我在管道之间共享一个 HashedWheelTimer
实例。
我当前的代码如下所示
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("handler", new HTTPRequestHandler());
Timer timer = new HashedWheelTimer();
pipeline.addLast("timeout", new IdleStateHandler(timer, 30, 30, 0));
return pipeline;
据我所知,我确实在管道之间共享一个 HashedWheelTimer
实例,但每次都会创建 IdleStateHandler
的新实例。
有错吗?有人可以帮助我如何正确地做到这一点吗?我应该将 IdleStateHandler
和 HashedWheelTimer
实例设为静态吗?
最佳答案
这是错误的。
通过此管道,每个新 channel 都将拥有自己的 HashedWheelTimer 实例。
来自 Netty HashedWheelTimer 文档
you should make sure to create only one instance and share it across your application. One of the common mistakes, that makes your application unresponsive, is to create a new instance for every connection.
您应该将 HashedWheelTimer 实例更改为静态,并保持 IdleStateHandler 不变。
关于nio - 共享 HashedWheelTimer 的一个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9745084/