2016-03-21 84 views
3

我想设置一个骆驼CXF-RS组件here的'connectionTimeout',它在第三方服务上产生一个RESTful请求。默认的30000毫秒是很长的。骆驼cxfrs RESTful客户端/ ProducerTemplate ConnectionTimeout

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

http-conf:conduit元素添加到application-context中,就像您所做的那样是要走的路,并且应该有效。是什么让你说它不是?

很多时候后端服务器花费很长时间回答,之后进行连接;在这种情况下,设置ReceiveTimeoutConnectionTimeout一样重要。

这是一个骆驼路由示例,它消耗RS请求并调用第三方RS服务器; 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>