简体   繁体   English

JBoss Netty中灵活的超时机制?

[英]Flexible timeout mechanism in JBoss Netty?

I am considering moving my Java NIO implementation over to JBoss Netty as it provides a much cleaner model than I have implemented. 我正在考虑将Java NIO实现转移到JBoss Netty,因为它提供的模型比我实现的更为简洁 The implementation manages a number of client connections to components over TCP using a proprietary protocol. 该实现使用专有协议通过TCP管理与组件的许多客户端连接。

One aspect of my implementation, that I cannot see in Netty, is the ability to set arbitrary timeouts, which: 我在Netty中看不到的实现的一个方面是设置任意超时的能力,该超时是:

  1. Wait for some data to be read from the Component. 等待从组件读取一些数据。 I know that Netty has a ReadTimeoutHandler but can the timeout value be changed/turned off easily by the Component as it moves through the state machine? 我知道Netty有一个ReadTimeoutHandler但是当组件在状态机中移动时,可以轻易地更改/关闭该超时值吗?
  2. Wait for time to elapse so that I can reconnect to the Component (to give the Component time to be restarted after a disconnect). 等待时间过去,以便我可以重新连接到组件(为断开连接后的组件重新启动时间)。 This is entirely unrelated to communication and is a simple timeout, however I'd like the timeout 'event/exception' to be presented to the handler class in the same manner as other communication-related timeouts. 这与通信完全无关,并且是一个简单的超时,但是我希望以与其他与通信相关的超时相同的方式将超时“事件/异常”呈现给处理程序类。

Can this timeout mechanism be accomplished using Netty? 可以使用Netty完成此超时机制吗?

Conclusion : Given I would need to implement a timeout mechanism, which would run within the its own thread, I am not going to convert to using Netty after all. 结论 :鉴于我需要实现一个超时机制,该机制将在其自己的线程中运行,所以我最终不会转换为使用Netty。

See ChannelConfig . 参见ChannelConfig The method setConnectTimeoutMillis(int) sets the timeout in milliseconds. setConnectTimeoutMillis(int)方法设置超时(以毫秒为单位)。 You can invoke this method via a Bootstrap instance by calling setOption(String, Object) . 您可以通过Bootstrap实例调用setOption(String,Object)来调用此方法。 The name would be "connectTimeoutMillis" and the value would be the desired timeout in milliseconds. name将为“ connectTimeoutMillis”,其value将是所需的超时(以毫秒为单位)。

The following snippet shows how to set the connect timeout to 5000 milliseconds (5 seconds). 以下代码段显示了如何将连接超时设置为5000毫秒(5秒)。

ClientBootstrap bootstrap... // bootstrap instance
bootstrap.setOption("connectTimeoutMillis", 5000);

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

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