我有一个 Camel 应用程序,它使用 netty 组件通过 TCP 与远程服务器通信。服务器有挂起的趋势,所以我希望能够在请求上添加可配置的超时,这样如果在 X 时间内没有返回响应,我会得到一个异常。
mina 组件在参数中具有此功能timeout
,但 netty 组件似乎没有相应的选项。
我试着用 aReadTimeoutHandler
来完成这个,结果很接近......但不完全是。HashedWheelTimer
它首先在初始化时创建CamelContext
:
HashedWheelTimer timer = new HashedWheelTimer();
JndiRegistry jndi = new JndiRegistry();
jndi.bind("MyClientPipelineFactory", new MyClientPipelineFactory(timer));
然后将其添加到我的管道中:
public class MyClientPipelineFactory extends ClientPipelineFactory
{
private final Timer timer;
public MyClientPipelineFactory(Timer timer) {
this.timer = timer;
}
public ChannelPipeline getPipeline(NettyProducer producer) throws Exception
{
ChannelPipeline p = pipeline();
p.addLast("timeout", new ReadTimeoutHandler(timer, 30));
// more stuff
return p;
}
}
最后,我在路由配置中发现了任何异常:
onException(ReadTimeoutException.class)
.handled(true)
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
throw new ApplicationException("Connection timed out", exception);
}
});
这几乎可以工作。不幸的是,它是特定于渠道的,而不是特定于请求的。因此,只要我与服务器通信,我就没事。但是在我完成 30 秒后,我被ReadTimeoutException
扔了。问题是它正在寻找网络上的任何活动,而不是我关心的请求/响应对。
我在这里错过了什么吗?是否有使用 Camel+Netty 进行请求超时的固有方法,或者有什么方法可以完成这项ReadTimeoutException
工作?
(骆驼 2.9.2/Netty 3.3.1)