zied123456
zied123456

Reputation: 71

How can i handle Apache Camel NoRouteToHostException exception?

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 ?

Upvotes: 1

Views: 1125

Answers (2)

Fritz Duchardt
Fritz Duchardt

Reputation: 11870

Camel provides a couple of very powerful error handling mechanisms with very useful redelivery and deadletter functionality. Those include:

For your usecase, Exception Clause is the best fit. Redelivery would help you to retry the HTTP call and avoid failure due to a temporary downtime, e.g. a server restart:

<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>

Upvotes: 2

Vijay Nandwana
Vijay Nandwana

Reputation: 2634

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>

Upvotes: 1

Related Questions