nio - 共享 HashedWheelTimer 的一个实例

标签 nio netty

在这个问题

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 的新实例。

有错吗?有人可以帮助我如何正确地做到这一点吗?我应该将 IdleStateHandlerHashedWheelTimer 实例设为静态吗?

最佳答案

这是错误的。

通过此管道,每个新 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/

相关文章:

java - 使用java FileChannel复制文件并附加文件末尾,但终端卡住

Java:SelectionKey.attach() 损坏了吗?

java - 如何使用 netty 客户端获取服务器响应

java - 每个 netty worker 每秒唤醒 2 次。为什么?

java - 为什么 Netty 只给我来自 UDP 消息的 768 字节

java.nio.file.NoSuchFileException : why nio not creating file

java - 为什么我的输出文件的大小小于原始文件的大小?

java - 从套接字读取文件的正确方法是什么?

java - Netty 客户端连接操作的哪一部分应该共享?

java - Netty:未使用 DefaultFileRegion 调用 ChannelProgressiveFutureListener