How can i handle Apache Camel NoRouteToHostException exception?

时间:2015-10-06 08:45:56

标签: web-services exception-handling apache-camel

i want to treat the case when connection with Camel HTTP component cannot be established

example:

<from uri="timer:tm?period=2000"/>
<to uri="https://URI"/> 

when there is no connection i get this exception:

java.net.NoRouteToHostException: No route to host: connect

I want to handle this response and change it to an understandable message by the consumer. How can i do this ?

2 个答案:

答案 0 :(得分:2)

Camel提供了一些非常强大的错误处理机制,具有非常有用的重新传递和死信功能。其中包括:

对于您的用例,Exception Clause是最合适的。重新传递将帮助您重试HTTP呼叫并避免由于临时停机而导致的故障,例如服务器重启:

<onException>
  <exception>java.net.NoRouteToHostException</exception>
  <!-- retry 3 times with a delay of 10 seconds -->
  <redeliveryPolicy maximumRedeliveries="3" logStackTrace="true" redeliveryDelay="10000" />
  <!-- only logs once redeliveries have failed -->
  <to uri="log:classToLog?level=ERROR"/>
</onException>

答案 1 :(得分:1)

You can handle exception using try..catch..finally.

http://camel.apache.org/try-catch-finally.html

Example in Spring DSL:

 <route id="send_request">
 <from uri="timer:tm?period=2000" />
 <doTry>
    <to uri="https://URI" />
    <doCatch>
        <exception>java.net.NoRouteToHostException</exception>
        <handled>
            <constant>true</constant>
        </handled>
        <log message="Some Message : ${exception.message}"
            loggingLevel="WARN" />
    </doCatch>
 </doTry>