gpt4 book ai didi

java - 如何处理 cxf/camel 生产者模板中的关闭/不可用端点

转载 作者:行者123 更新时间:2023-12-02 19:28:54 27 4
gpt4 key购买 nike

如果我发送请求的端点关闭或不可用,我想发送重试。我有这段代码,当端点关闭时会产生异常:

    ProducerTemplate template = context.createProducerTemplate();
Exchange exchange = template.request(someCloseEndpoint, someProcessor)

if(exchange.getExcpetion == null)
//do something
else
//error handling
//do some retries if close connection

当端点关闭时,我在日志中看到此异常

org.apache.cxf.binding.soap.SoapFault: Connection refused
at org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor$SAAJOutEndingInterceptor.handleMessage(SAAJOutInterceptor.java:220) ~[cxf-rt-bindings-soap-2.7.13.jar:2.7.13]
at org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor$SAAJOutEndingInterceptor.handleMessage(SAAJOutInterceptor.java:174) ~[cxf-rt-bindings-soap-2.7.13.jar:2.7.13]
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272) ~[cxf-api-2.7.13.jar:2.7.13]
...
Caused by: com.ctc.wstx.exc.WstxIOException: Connection refused
at com.ctc.wstx.sw.BaseStreamWriter.flush(BaseStreamWriter.java:255) ~[woodstox-core-asl-4.4.1.jar:4.4.1]
at org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor$SAAJOutEndingInterceptor.handleMessage(SAAJOutInterceptor.java:215) ~[cxf-rt-bindings-soap-2.7.13.jar:2.7.13]
... 28 common frames omitted
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_25]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) ~[na:1.8.0_25]

但是上面的代码仍然进入if(exchange.getException == null)而不是else部分进行错误处理。我还尝试将代码包装在 try-catch 语句中,但仍然无法捕获任何错误。

try{
ProducerTemplate template = context.createProducerTemplate();
Exchange exchange = template.request(someCloseEndpoint, someProcessor)

if(exchange.getExcpetion == null)
//do something
else
//error handling
//do some retries if close connection
} catch ( org.apache.cxf.binding.soap.SoapFault e) {
// do retries
} catch ( Exception e) {
// try catching generic Error for testing purposes
}

即使捕获通用异常也不会得到任何东西。我如何处理这个关闭/不可用的端点,以便我可以实现重试

最佳答案

故障错误消息被归类为不可恢复的错误,因为默认情况下不会将其返回给camel。为了捕获这一点,您需要在 Camel 上下文中启用错误处理。

getContext().setHandleFault(true);

from("<END_POINT>").handlefault()

这将使故障转换为异常。希望您的代码可以正常工作。

关于java - 如何处理 cxf/camel 生产者模板中的关闭/不可用端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31932682/

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