我如何处理 Apache Camel NoRouteToHostException 异常?
How can i handle Apache Camel NoRouteToHostException exception?
我想处理无法与 Camel HTTP 组件建立连接的情况
示例:
<from uri="timer:tm?period=2000"/>
<to uri="https://URI"/>
当没有连接时我得到这个异常:
java.net.NoRouteToHostException: No route to host: connect
我想处理此响应并将其更改为消费者可以理解的消息。我该怎么做?
您可以使用 try..catch..finally 处理异常。
http://camel.apache.org/try-catch-finally.html
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>
Camel 提供了几个非常强大的错误处理机制,具有非常有用的重新传递和死信功能。其中包括:
- 错误处理程序:https://camel.apache.org/error-handler.html
- 死信频道:https://camel.apache.org/dead-letter-channel.html
- 尝试,抓住并最终:https://camel.apache.org/try-catch-finally.html
- 例外条款:https://camel.apache.org/exception-clause.html
对于您的用例,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>
我想处理无法与 Camel HTTP 组件建立连接的情况
示例:
<from uri="timer:tm?period=2000"/>
<to uri="https://URI"/>
当没有连接时我得到这个异常:
java.net.NoRouteToHostException: No route to host: connect
我想处理此响应并将其更改为消费者可以理解的消息。我该怎么做?
您可以使用 try..catch..finally 处理异常。
http://camel.apache.org/try-catch-finally.html
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>
Camel 提供了几个非常强大的错误处理机制,具有非常有用的重新传递和死信功能。其中包括:
- 错误处理程序:https://camel.apache.org/error-handler.html
- 死信频道:https://camel.apache.org/dead-letter-channel.html
- 尝试,抓住并最终:https://camel.apache.org/try-catch-finally.html
- 例外条款:https://camel.apache.org/exception-clause.html
对于您的用例,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>