【发布时间】:2013-02-22 07:33:04
【问题描述】:
我有一个 Camel 应用程序,它使用 netty 组件通过 TCP 与远程服务器通信。服务器有挂起的趋势,所以我希望能够为请求添加可配置的超时,这样如果在 X 时间内没有返回响应,我就会得到一个异常。
mina 组件在timeout 参数中具有此功能,但 netty 组件似乎没有相应的选项。
我尝试使用ReadTimeoutHandler 来完成此操作,结果非常接近……但不完全是。它首先在初始化CamelContext 时创建HashedWheelTimer:
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)
【问题讨论】:
标签: netty apache-camel