问题
I am trying to set the 'connectionTimeout' for a Camel CXF-RS component here which produces a RESTful requests on a 3rd party service. The default 30000
miliseconds is to long.
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);
}
});
I have tried adding this to our application-context
as many have suggested, but cannot see it modifying the default values when debugging through the HTTPConduit
and HTTPClientPolicy
classes:
<http-conf:conduit name="*.http-conduit">
<http-conf:client ConnectionTimeout="5000"/>
</http-conf:conduit>
and I have tried appending
"?httpClientAPI=true&connectionTimeout=5000"
as options to the url string.
Any help or guidance would be much appreciated.
回答1:
Adding the http-conf:conduit
element in application-context
as you did is the way to go and should work. What makes you say it does not?
Quite often a backend server take too long to answer, after the connection is made; in this case setting ReceiveTimeout
is as important as ConnectionTimeout
.
This is a sample camel Route which consumes RS requests and calls a third-party RS server; the ReceiveTimeout and ConnectionTimeout parameters work as expected.
<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>
来源:https://stackoverflow.com/questions/36133777/camel-cxfrs-restful-client-producertemplate-connectiontimeout