Camel cxfrs RESTful client / ProducerTemplate ConnectionTimeout

时间:2016-03-21 14:30:43

标签: rest apache-camel cxf cxfrs

我正在尝试为Camel CXF-RS组件here 设置'connectionTimeout',它在第三方服务上产生RESTful请求。默认30000毫秒是长的。

Exchange exchange = template.send("cxfrs://" + url, new Processor() {
 public void process(Exchange exchange) throws Exception {
  exchange.setPattern(ExchangePattern.InOut);
  Message inMessage = exchange.getIn();
  setupDestinationURL(inMessage);
  // using the http central client API
  inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.TRUE);
  // set the Http method
  inMessage.setHeader(Exchange.HTTP_METHOD, "PUT");
  // set the relative path
  inMessage.setHeader(Exchange.HTTP_PATH, url);                
  // Specify the response class , cxfrs will use InputStream as the response                 object type 
  inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Customer.class);
  // set a customer header
  inMessage.setHeader("key", "value");
  // since we use the Get method, so we don't need to set the message body
  inMessage.setBody(null);                
 }
});

我已尝试将其添加到我们的application-context中,正如许多人建议的那样,但在通过HTTPConduitHTTPClientPolicy类调试时无法看到它修改默认值:

<http-conf:conduit name="*.http-conduit">
  <http-conf:client ConnectionTimeout="5000"/>
</http-conf:conduit>

我尝试追加

 "?httpClientAPI=true&connectionTimeout=5000" 

作为url字符串的选项。

非常感谢任何帮助或指导。

1 个答案:

答案 0 :(得分:1)

正如您所做的那样在http-conf:conduit中添加application-context元素是可行的方法。是什么让你说它没有?

后端服务器通常需要很长时间才能回答,建立连接之后;在这种情况下,设置ReceiveTimeoutConnectionTimeout一样重要。

这是一个使用RS请求并调用第三方RS服务器的示例camel Route; ReceiveTimeout和ConnectionTimeout参数按预期工作。

<cxf:rsServer id="rsFrontServer" address="..." serviceClass="..."/>

<cxf:rsClient id="rsBackendClient" address=".../" serviceClass="..."/>

<http-conf:conduit name="*.http-conduit">
    <http-conf:client ReceiveTimeout="5000" ConnectionTimeout="5000"/>
</http-conf:conduit>

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <route id="front">
        <from uri="cxfrs:bean:rsFrontServer"/>
        <!-- do stuff -->
        <to uri="cxfrs:bean:rsBackendClient"/>
    </route>
</camelContext>