简体   繁体   English

使用Apache Camel管理超时

[英]Manage Timeout with Apache Camel

I have the following Route definition: 我具有以下路线定义:

@Override
public void configure() throws Exception {
    from(String.format("direct:%s", this.connector))
    .id("Route1")
    .threads()
    .maxPoolSize(10)
    .keepAliveTime(3000)
    .timeUnit(TimeUnit.MICROSECONDS)
    .poolSize(1)
    .rejectedPolicy(ThreadPoolRejectedPolicy.Abort)
    .log("Calling WS")
    .maxQueueSize(1)
    .to("http://10.8.4.9:8080/service");
}

And the request snipet above: 以及上面的请求片段:

InputStream exchange = (InputStream) template
            .requestBodyAndHeaders(url, AppUtil.parse(this.body, input), this.headers);

The endpoint is intentionally unavaliable. 该端点是故意不可用的。 So, I expected my request wait for 3 seconds and throws some exception as response. 因此,我希望我的请求等待3秒钟并抛出一些异常作为响应。 Insted, the following behaviour happens: 插入后,会发生以下行为:

    2018-08-24 16:55:55,048 DEBUG http-nio-8081-exec-2 httpclient.HttpMethodDirector:443 - Connection timed out: connect
java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
... more stack...
    at java.lang.Thread.run(Unknown Source)
2018-08-24 16:56:38,333  INFO http-nio-8081-exec-2 httpclient.HttpMethodDirector:445 - Retrying request
2018-08-24 16:56:38,417 DEBUG http-nio-8081-exec-2 httpclient.HttpConnection:692 - Open connection to 10.8.4.9:8080/service

Camel retries for 3 times and do not respect the timeout. 骆驼重试3次,不遵守超时。

I've tried to use: 我尝试使用:

onException(ConnectException.class)
.maximumRedeliveries(0);

No success... 没有成功...

What I missed? 我错过了什么?

The .keepAliveTime(3000) is not for HTTP endpoints, its an API for the JVM thread pool in Java itself. .keepAliveTime(3000)不适用于HTTP端点,它是Java本身中JVM线程池的API。 You can read about this option in the Java API for thread pools, and there is also a bit of javadoc on that method in Camel DSL. 您可以在用于线程池的Java API中阅读有关此选项的信息,并且在Camel DSL中该方法也有一些javadoc。

If you need HTTP connection timeout etc, then you need to set specific option on the http endpoint. 如果您需要HTTP连接超时等,则需要在http端点上设置特定选项。

https://github.com/apache/camel/blob/master/components/camel-http4/src/main/docs/http4-component.adoc#using-client-timeout---so_timeout https://github.com/apache/camel/blob/master/components/camel-http4/src/main/docs/http4-component.adoc#using-client-timeout---so_timeout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM