【问题标题】:Netty: Can I give static Idle state handler in pipeline and channel future listenerNetty:我可以在管道和通道未来监听器中提供静态空闲状态处理程序吗
【发布时间】:2017-08-14 06:46:21
【问题描述】:

在通道初始化器中,

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);
    }

在上面这段代码中,当通道初始化时,我可以使用 IdleStateHandler 的静态对象,而不是为每个通道使用一个新实例。它是线程安全的吗?

还有,

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

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

我可以在上面的代码中使用静态 idleReadHandler 吗?

我正在使用 Netty-4.1.0

在 jboss 文档中标记为可共享,直到 netty 3.x https://docs.jboss.org/netty/3.2/api/org/jboss/netty/handler/timeout/IdleStateHandler.html 但在 4.x 文档中看不到

【问题讨论】:

    标签: netty


    【解决方案1】:

    在netty中,判断一个处理程序是否可以在多个通道之间线程安全使用的规则是@Shareable注解。由于IdleStateHandler没有被注解注解,说明不能安全使用。

    【讨论】:

    猜你喜欢
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 2014-08-10
    相关资源
    最近更新 更多