【问题标题】:Timeout event in netty 4netty 4中的超时事件
【发布时间】:2012-11-06 11:09:31
【问题描述】:

您好,我想收到一个messageReceived 在预期时间内没有被调用的事件。我尝试使用ReadTimeoutHandler,它会生成异常,我可以在exceptionCaught() 中处理我会做一些工作并在不关闭上下文的情况下返回。但在那之后我得到了一堆异常

Nov 18, 2012 8:56:34 AM io.netty.channel.ChannelInitializer
WARNING: Failed to initialize a channel. Closing: [id: 0xa81de260, /127.0.0.1:59763 => /127.0.0.1:59724]
io.netty.channel.ChannelHandlerLifeCycleException: io.netty.handler.timeout.ReadTimeoutHandler is not a @Sharable handler, so can't be added or removed multiple times.
    at io.netty.channel.DefaultChannelPipeline.callBeforeAdd(DefaultChannelPipeline.java:629)
    at io.netty.channel.DefaultChannelPipeline.addLast0(DefaultChannelPipeline.java:173)

我做得对吗?

谢谢

【问题讨论】:

  • 你能分享你的代码吗?我很想看看你如何处理异常
  • 就这么简单 if ( cause instanceof ReadTimeoutException ) { return; }
  • 从我看来,您似乎想将 io.netty.handler.timeout.ReadTimeoutHandler 的相同实例添加到多个管道。会是这样吗?
  • 啊,您在我的 ChannelInitializer 中给出了一个很好的提示,我需要为每次调用 initChannel 时创建新的 ReadTimeoutHandler。还有有没有办法通知我的最终处理程序通道已被客户端关闭?当客户端退出时,我没有看到服务器端出现任何异常。
  • 最好的办法是做一些心跳

标签: java sockets netty


【解决方案1】:

看起来您正在向多个频道添加相同的 io.netty.handler.timeout.ReadTimeoutHandler 实例。试试

ch.pipeline()
.addLast(new ReadTimeoutHandler(3000))

@Shareable 的文档:

/**
 * Indicates that the same instance of the annotated {@link ChannelHandler}
 * can be added to one or more {@link ChannelPipeline}s multiple times
 * without a race condition.
 * <p>
 * If this annotation is not specified, you have to create a new handler
 * instance every time you add it to a pipeline because it has unshared
 * state such as member variables.
 * <p>
 * This annotation is provided for documentation purpose, just like
 * <a href="http://www.javaconcurrencyinpractice.com/annotations/doc/">the JCIP annotations</a>.
 */
@Inherited
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Sharable {
    // no value
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2011-07-04
    • 2021-01-18
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    相关资源
    最近更新 更多