0
votes

I am new to Apache Camel. I am able to send JMS message from one queue to another queue. I would like to know how to handle the exception. I learned onException, But it is not working for me.

I changed my jms queue to jms1. However, when I execute the code i get an exception.

My expectation is whenever I get exception my bean class should be invoked, but it is not.

Exception:

org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToCreateRouteException: Failed to create route route1 at: >>> To[jms1:queue:FinalQSource] <<< in route: Route[[From[jms:queue:testQSource]] -> [OnException[[class j... because of Failed to resolve endpoint: jms1://queue:FinalQSource due to: No component found with scheme: jms1

Code

<camelContext id="jmsContext" xmlns="http://camel.apache.org/schema/spring">   
  <onException>
  <exception>org.apache.camel.RuntimeCamelException</exception>
  <exception>org.apache.camel.ResolveEndpointFailedException</exception>
    <!-- <handled><constant>true</constant></handled> -->           
      <bean ref="exceptionListener" method="orderFailed" />
  </onException>
  <route>
    <from uri="jms:queue:testQSource" />
    <to uri="jms1:queue:FinalQSource" /><!--
  </route>
</camelContext>
1

1 Answers

1
votes

This will not work as the exception is being thrown by camel when it tries to parse your route. The onException block will only catch exceptions that are thrown during execution of your route.

To test exception handling use the proper camel testing guide - http://camel.apache.org/testing.html

I would recommend mocking one of your endpoints to return an exception, example here - https://github.com/christian-posta/camel-sandbox/blob/master/one-off/src/test/java/posta/TestMockExceptions.java

MockEndpoint mockException = MockEndpoint.resolve(context, "mock:exception");
    mockException.whenAnyExchangeReceived(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            System.out.println("i got here...");
            throw new RuntimeException("fail!");
        }
});