Apache Camel异常处理

来源:互联网 发布:linux登录密码修改 编辑:程序博客网 时间:2024/06/07 19:05

直接看代码

package com.lala.bean;import org.apache.camel.Exchange;import org.apache.camel.Processor;public class TestBean implements Processor {private String appId;public void process(Exchange exchange) throws Exception {if(appId == null || "".equals(appId)){throw new NullPointerException("appId cannot be null ... ");}System.out.println(appId + " : --------------- " + this.getClass());}public String getAppId() {return appId;}public void setAppId(String appId) {this.appId = appId;}}


package com.lala.bean;import org.apache.camel.Exchange;import org.apache.camel.Processor;public class Error implements Processor {public void process(Exchange exchange) throws Exception{System.out.println("-------------------------------------------");//这样获取异常信息Exception exce = exchange.getProperty("CamelExceptionCaught", Exception.class);System.out.println(exce.getMessage());exce.printStackTrace();}}



<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"><bean id="bean1" class="com.lala.bean.TestBean"><property name="appId" value="wwwwwwww"></property></bean><bean id="bean2" class="com.lala.bean.TestBean"></bean><bean id="bean3" class="com.lala.bean.TestBean"><property name="appId" value="mmmmmmmm"></property></bean><bean id="errorBean" class="com.lala.bean.Error" /><bean id="testErrorHandler" class="org.apache.camel.builder.DeadLetterChannelBuilder">  <property name="deadLetterUri" value="bean:errorBean"/></bean><camelContext errorHandlerRef="testErrorHandler" id="camelContext" xmlns="http://camel.apache.org/schema/spring"><template id="camelContextTemplate" /><route errorHandlerRef="testErrorHandler"><from uri="direct:start-error" /><to uri="bean:bean1" /><to uri="bean:bean2" /><to uri="bean:bean3" /></route></camelContext></beans>

根据上面的配置,很容易看出,bean2会抛异常
这样,在上面的那个route里面,只要有异常,就会路由到bean:errorBean里面去


注意,上面的route上的errorHandlerRef 和 camelContext上面的errorHandlerRef不需要同时添加

7 0
原创粉丝点击