您的位置:首页 > 运维架构 > Apache

Apache Camel异常处理

2015-09-14 22:07 477 查看
直接看代码

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不需要同时添加
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: