簡體   English   中英

netty 4 中的超時事件

[英]Timeout event in netty 4

嗨,我想收到一個事件,其中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)

我做得對嗎?

謝謝

看起來您正在向許多通道添加 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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM