如何阻止骆驼http重试

时间:2017-03-30 16:05:46

标签: java apache-camel apache-httpcomponents

我们使用camel 2.18.1的toD组件将一个主体发布到http web服务但是当url无效时,camel在内部重试2-3分钟,这是我不想要的。

我进一步探索并发现camel在内部使用org.apache.commons.httpclient.HttpMethodDirector来执行重试操作。如何阻止Camel这样做。

部分例外日志如下。

<2017-03-29 16:33:59,084>:<>:<>:<>:<org.apache.commons.httpclient.HttpMethodDirector>:<INFO >:[Camel (camel-1) thread #20 - Threads]::<I/O exception (java.net.ConnectException) caught when processing request: Connection timed out (Connection timed out)>
<2017-03-29 16:33:59,085>:<>:<>:<>:<org.apache.commons.httpclient.HttpMethodDirector>:<INFO >:[Camel (camel-1) thread #20 - Threads]::<Retrying request>
<2017-03-29 16:36:06,313>:<>:<>:<>:<org.apache.commons.httpclient.HttpMethodDirector>:<INFO >:[Camel (camel-1) thread #20 - Threads]::<I/O exception (java.net.ConnectException) caught when processing request: Connection timed out (Connection timed out)>
<2017-03-29 16:36:06,314>:<>:<>:<>:<org.apache.commons.httpclient.HttpMethodDirector>:<INFO >:[Camel (camel-1) thread #20 - Threads]::<Retrying request>
<2017-03-29 16:38:13,545>:<>:<>:<>:<org.apache.commons.httpclient.HttpMethodDirector>:<INFO >:[Camel (camel-1) thread #20 - Threads]::<I/O exception (java.net.ConnectException) caught when processing request: Connection timed out (Connection timed out)>
<2017-03-29 16:38:13,546>:<>:<>:<>:<org.apache.commons.httpclient.HttpMethodDirector>:<INFO >:[Camel (camel-1) thread #20 - Threads]::<Retrying request>

这是我得到的堆栈跟踪

java.net.ConnectException: Connection timed out (Connection timed out)
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at java.net.Socket.connect(Socket.java:538)
        at java.net.Socket.<init>(Socket.java:434)
        at java.net.Socket.<init>(Socket.java:286)
        at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
        at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
        at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
        at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.open(MultiThreadedHttpConnectionManager.java:1361)
        at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
        at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
        at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
        at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
        at org.apache.camel.component.http.HttpProducer.executeMethod(HttpProducer.java:257)
        at org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:152)
        at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
        at org.apache.camel.processor.SendDynamicProcessor$1.doInAsyncProducer(SendDynamicProcessor.java:124)
        at org.apache.camel.impl.ProducerCache.doInAsyncProducer(ProducerCache.java:436)
        at org.apache.camel.processor.SendDynamicProcessor.process(SendDynamicProcessor.java:119)
        at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197)
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:120)
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
        at org.apache.camel.processor.TryProcessor.process(TryProcessor.java:113)
        at org.apache.camel.processor.TryProcessor.process(TryProcessor.java:84)
        at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
        at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:542)
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197)
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:120)
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:120)
        at org.apache.camel.processor.Pipeline.access$100(Pipeline.java:44)
        at org.apache.camel.processor.Pipeline$1.done(Pipeline.java:138)
        at org.apache.camel.processor.ThreadsProcessor$ProcessCall.run(ThreadsProcessor.java:88)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

最后这里是路由构建器中使用的代码

 from("disruptor:" + "CallAPostService")
                    .routeId("CallAPostService")
                    .threads()
                    .log(LoggingLevel.INFO, LOG, "Processing id ${id} disruptor")
                    .doTry()
                        .process(new populateRequestBody()) // this processor sets the request xml to excahnge body
                                    .log(LoggingLevel.INFO, LOG, "Request XML = ${body}")
                                    .setHeader(Exchange.HTTP_METHOD, simple("POST"))
                                    .setHeader(Exchange.HTTP_BASE_URI, constant(""))
                                    .setHeader(Exchange.HTTP_PATH, constant(""))
                                    .setHeader(Exchange.HTTP_URI, simple(""))
                                                                    .setHeader(Exchange.HTTP

_URL, simple(""))
                                                                .toD("http://172.0.0.0:7001/myapp/cosumemessage" + "?bridgeEndpoint=true")                                                                
 .doCatch(Exception.class)                            
 .log(LoggingLevel.ERROR, LOG, "Processing id ${id} disruptor, Unknown Exception")
.doFinally();

1 个答案:

答案 0 :(得分:1)

实现HttpClientConfigurer并将其添加到注册表中。

@Bean(name = "httpConfigurer")
public HttpClientConfigurer createCustomConfigurer() {
    return new HttpClientConfigurer() {

        @Override
        public void configureHttpClient(HttpClient client) {
            client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10000);
            client.getParams().setParameter("http.method.retry-handler", new DefaultHttpMethodRetryHandler(0, false));            }
    };
}

设置HttpComponent的httpClientConfigurer,如下所示:

.toD("http://1.2.3.4/xyz?httpClientConfigurer=httpConfigurer")