代码之家  ›  专栏  ›  技术社区  ›  Peter

Netty:我可以在管道和通道的未来监听器中提供静态空闲状态处理程序吗

  •  1
  • Peter  · 技术社区  · 7 年前

    在通道初始化器中,

    protected void initChannel(SocketChannel ch) throws Exception {
            MessageHandler handler = new MessageHandler(channelGroup);
            ch.pipeline().addLast(DECODER, new MessageDecoder())
                    .addLast(ENCODER, newMessageEncoder())
                    .addLast(idleExecutor, "idleHandler", new IdleStateHandler(0, 0, 6*60))
                    .addLast(pipelineExecutor, "handler", handler);
        }
    

    而且

    当我向频道写入内容时。 我向它添加了一个空闲的读取处理程序,这样如果我在某个时间内没有得到响应,我就会关闭通道。

         ChannelPipeline pipeline = channel.pipeline();
          pipeline.addAfter(ChannelInitializer.idleExecutor, 
    "idleHandler", "idleReadHandler",new IdleStateHandler(60, 0, 0));
    

    我正在使用Netty-4.1.0

    在netty 3.x之前,它在jboss文档中被标记为sharable https://docs.jboss.org/netty/3.2/api/org/jboss/netty/handler/timeout/IdleStateHandler.html

    1 回复  |  直到 7 年前
        1
  •  1
  •   Ferrybig    7 年前

    在netty中,确定处理程序是否可以在多个通道之间使用线程安全的规则是 @Shareable 注释。自从 IdleStateHandler