简体   繁体   English

netty 4 中的超时事件

[英]Timeout event in netty 4

Hi I would like receive an event where messageReceived does not get called within an expected time.嗨,我想收到一个事件,其中messageReceived在预期的时间内没有被调用。 I tried with ReadTimeoutHandler where it generates exception where I can handle in exceptionCaught() where I would would do some work and return without closing the context.我尝试使用ReadTimeoutHandler ,它会生成异常,我可以在exceptionCaught()中处理异常,在那里我会做一些工作并在不关闭上下文的情况下返回。 but right after that I got a bunch of exception但在那之后我得到了一堆异常

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)

Am I doing correctly?我做得对吗?

Thanks谢谢

It looks like you are adding the same instance of io.netty.handler.timeout.ReadTimeoutHandler to many channels.看起来您正在向许多通道添加 io.netty.handler.timeout.ReadTimeoutHandler 的相同实例。 Try尝试

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

Documentation of @Sharable: @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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM