camel http4 route in thread with timeout
I'd like to use http4 route, but have it execute inside a thread with a timeout. I already have my http4 route setup, something like this:
from("direct:start")
.setHeader(Exchange.HTTP_QUERY,simple("开发者_Go百科format=json&count=${in.headers.count}"))
.to("http4://www.host.com/someapi")
.unmarshal().json(JsonLibrary.JACKSON,MyResponseType.class)
.to("bean:SomeBean?method=echo");
I'd like to apply a 100ms timeout around the http call, and route to a failure handler in that case. Does anyone know how that can be done?
You can set a 100ms timeout on the client by specifying http4://foo?httpClient.soTimeout=100
. When a timeout occurs it will probably throw an exception that you can handle like so (off the top of my head, untested code):
onException(IOException.class).to("direct:timeouts");
from("direct:start")
.setHeader(Exchange.HTTP_QUERY,simple("format=json&count=${in.headers.count}"))
.to("http4://www.host.com/someapi?httpClient.soTimeout=100")
.unmarshal().json(JsonLibrary.JACKSON,MyResponseType.class)
.to("bean:SomeBean?method=echo");
from("direct:timeouts").to("...");
The recipient list EIP has timeout support http://camel.apache.org/recipient-list.html
An update... I think the syntax for the timeout configurations has changed in later versions of Camel. With Camel 2.16.2 I found that the timeout parameters in the documentation can be set via query param options, but they need to be prefixed with httpClient. . Here is what worked for me:
?httpClient.connectTimeout=10000&httpClient.connectionRequestTimeout=10000&httpClient.socketTimeout=30000
I verified by testing with ridiculously small values (1 ms) and it produced "read timed out" errors.
It looks the availble options for httpClient are the setter values on RequestConfig.Builder.
精彩评论