如何在Apache Camel中处理管道级别的execption?

时间:2016-12-06 08:12:08

标签: java exception-handling apache-camel

我正在使用apache camel xml dsl在路由上创建。 我想使用errorHandlerRef处理异常。但这只能与路线标签一起使用。 这是我的路线背景:

<routeContext id="myRoute" xmlns="http://camel.apache.org/schema/spring">
    <route errorHandlerRef="myErrorHandler" id="myErrorRoute">
        <from uri="activemq:queue:{{my.queue}}" />
        <multicast>
            <pipeline>
       //setting headers and other properties
                <to uri="spring-redis://localhost:6379?serializer=#stringSerializer" />
            </pipeline>
            <pipeline>
      //want to put error handling here
      <log message="Insert to DB" />
      <to uri="mybatis:insertToDB?statementType=InsertList"></to>
            </pipeline>
        </multicast>
    </route>
</routeContext>

有什么方法可以在管道级别处理异常。 例如,这里我想为两个管道制作不同的错误处理程序。我怎么能做到这一点? 我尝试将errorHanlerRef与管道标记放在一起,但是出现了编译错误。

1 个答案:

答案 0 :(得分:0)

从驼峰2.0开​​始,可以使用do http://camel.apache.org/try-catch-finally.html中所述的doTry,doCatch和doFinally来完成,并且可以按如下方式实现:

<doTry>
    <to uri="mybatis:insertToDB?statementType=InsertList"></to>
    <doCatch>
        <exception>java.io.IOException</exception>
        <to uri="mock:catch"/>
        <log message="Exception message : ${exception.message}"
            loggingLevel="ERROR" />
    </doCatch>
    <doFinally>
        <to uri="mock:finally"/>
    </doFinally>
 </doTry>