Exceptions are not caught
sanya.sokolov May 24, 2011 6:54 AMHi all,
Working with 2 routes I got a strange problem:
I stop first route if exception is thrown within it.
When exception is thrown in second route it is not caught!
Here is the unit test and appContext:
\org\apache\camel\test\TestOnException.java -
package org.apache.camel.test;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.Consumer;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.SuspendableService;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestOnException {
ClassPathXmlApplicationContext appcontext = new ClassPathXmlApplicationContext("
org
apache
camel
test
appContext.xml");
CamelContext camel = appcontext.getBean(CamelContext.class);
static boolean testPassed = false;
@Test
public void test() throws Exception {
ProducerTemplate template = camel.createProducerTemplate();
template.sendBody("amq:two", "bbb");
Thread.sleep(500);
template.sendBody("amq:one", "aaa");
Thread.sleep(500);
Assert.assertTrue(testPassed);
}
public static class TestHelper implements CamelContextAware {
CamelContext camelContext;
public void logMessage(Exchange exchange) {
testPassed = true;
System.out.println("Log for: " + exchange.getExchangeId());
}
public void suspendRoute(Exchange exchange) {
final String id = "second";
final Consumer consumer = camelContext.getRoute(id).getConsumer();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
try {
((SuspendableService) consumer).stop();//.suspend();
System.out.println("Route suspended: " + id);
} catch (Exception e) {
System.out.println("Camel consumer stop failed: " + e);
}
}
}, new Date(System.currentTimeMillis() - 1L));
}
public void throwException(Exchange exchange) throws IllegalArgumentException {
throw new IllegalArgumentException("OOps! Some problem occurs!");
}
@Override
public void setCamelContext(CamelContext camelContext) {
this.camelContext = camelContext;
}
@Override
public CamelContext getCamelContext() {
return camelContext;
}
}
}
/TestOnException.java -
\org\apache\camel\test\appContext.xml -
-
/appConfig.xml
This test is passed if I suspend route instead of stopping and in case if I remove errorhandler from context.
this problem was reproduced in Camel 2.7.0
Could you please explain what is wrong?
thank you.