java - netty 4 中的超时事件

标签 java sockets netty

您好,我想收到一个事件,其中 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)

我做得对吗?

谢谢

最佳答案

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

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

@Sharable 的文档:

/**
 * 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
}

关于java - netty 4 中的超时事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13438385/

相关文章:

java - jax-rs, Jersey 原型(prototype),似乎无法使用@WebListener

c++ - Boost C++ UDP 示例无法通过互联网传输

c++ - 关于一般网络编程中的write buffer

java - 没有显示监听套接字

java - Netty - 如何获得所有客户 channel ?

java - 以编程方式获取容器在 Kubernetes 中所属的 Pod 的名称?

java - 如何在 Linux CentOS 服务器上通过 yum 安装 easy_install

java - 如何使用 HMACSHA1 和 key 创建签名以连接到 Kayako API

c++ - 将套接字重新绑定(bind)到不同的接口(interface)

performance - 在 32 核/10Gbit 主机上调优 Netty