gpt4 book ai didi

java - 如何停止camel http重试

转载 作者:行者123 更新时间:2023-11-30 06:49:21 24 4
gpt4 key购买 nike

我们正在使用 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();

最佳答案

实现 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")

关于java - 如何停止camel http重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43122983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com