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

时间:2015-08-11 03:23:28

标签: java web-services soap apache-camel cxf

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

    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
 }

即使是普通异常的捕获也没有得到任何结果。如何处理这个关闭/不可用的端点,以便我可以实现重试

1 个答案:

答案 0 :(得分:0)

故障错误消息被归类为不可恢复的错误,因为默认情况下它不会返回到骆驼。为了捕获此错误,您需要在骆驼上下文中启用错误处理。

getContext().setHandleFault(true);

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

这将使故障被转换为异常。希望此后,您的代码可能会起作用。