camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Claus Ibsen">
Subject RE: Handling Bean exceptions
Date Fri, 06 Jun 2008 06:52:53 GMT
Hi Drew

Is now added to the FAQ.

However you have to wait till next day for the wiki sync have run (the entry is added at -
using camel questions - something with how do I get the thrown Exception ....)

Med venlig hilsen
Claus Ibsen
Skovsgårdsvænget 21
8362 Hørning
Tlf. +45 2962 7576

-----Original Message-----
From: Bruce Elmore [] 
Sent: 5. juni 2008 17:00
Subject: Handling Bean exceptions

Hopefully, this isn't too much of a newbie question. I tried finding an
answer by searching the forums but had no luck.

I created a simple route that contains a bean component that throws an
exception. I'm calling the route synchronously (ExchangePattern.InOut) using
CamelTemplate.sendBody. I was hoping that the call to sendBody would throw
an exception (hopefully one that contained details of the exception thrown
by the bean). Instead, it simply returns the original request object passed. 

I'm aware that exceptions can be handled using an errorHandler and/or the
exception clause. But I wasn't sure how to use either to preserve the
original exception and return it to the caller synchronously. I was hoping
there was a fairly easy way to accomplish this.

I noticed in CamelCxfExample sample, calling the pingMe throws an exception
as I would expect. I was wondering about this apparent inconsistency between
calling a web service and calling a bean.

Below is some code that exemplifies the problem.
package biz.firethorn.interfaces;

import javax.jms.ConnectionFactory;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.CamelContext;
import org.apache.camel.CamelTemplate;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;

import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.processor.SendProcessor;

public final class ExceptionTest {

	public static void main(String args[]) throws Exception {
		CamelContext context = new DefaultCamelContext();
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
		context.addComponent("activemq", ActiveMQComponent

		context.addRoutes(new RouteBuilder() {
			public void configure() {
				from("activemq:queue:input").bean(new ExceptionBean());

		CamelTemplate template = new CamelTemplate(context);

		try {
			int result = (Integer) template.sendBody("activemq:queue:input",
				ExchangePattern.InOut, "Hello");
			System.out.println("result = " + result);
		catch (Exception e) {
			System.out.println("Exception caught! e.message = " + e.toString());
		finally {

	public static class ExceptionBean {
		public int doSomething(String request) throws Exception {
			//return 1;
			throw new Exception("Ouch!");


Thanks in advance for the help!
View this message in context:
Sent from the Camel - Users mailing list archive at

View raw message