Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camel - How to stop route execution on exception?

Tags:

apache-camel

Is there any way I can stop the route execution (after displaying a log message) when exception is caught?

        <doTry>
            <process ref="messageProcessor"/>
            <doCatch>
                <exception>java.lang.IllegalArgumentException</exception>
                <log message="some message related to the exception" />
            </doCatch>
        </doTry>

Please provide a way to achieve this in Spring DSL. I've already tried < stop/> but that doesn't display log message.

like image 879
Vijay Nandwana Avatar asked Sep 03 '15 15:09

Vijay Nandwana


2 Answers

Added a process in doCatch which stop's a Camel context.

        <doTry>
            <process ref="messageProcessor"/>
            <doCatch>
                <exception>java.lang.IllegalArgumentException</exception>
                <handled>
                    <constant>true</constant>
                </handled>
                <setHeader headerName="exceptionStackTrace">
                    <simple>${exception.stacktrace}</simple>
                </setHeader>
                <process ref="mandatoryParameterIsNull"/>           
            </doCatch>
        </doTry>

Processor:

@Component
public class MandatoryParameterIsNull implements Processor{

Logger log = Logger.getLogger(MandatoryParameterIsNull.class);

@Override
public void process(Exchange exchange) throws Exception {

    if (log.isDebugEnabled()) {
        log.debug("Some parameter is mandatory");
        log.debug(exchange.getIn().getHeader("exceptionStackTrace"));
    }
    exchange.getContext().getShutdownStrategy().setLogInflightExchangesOnTimeout(false);
    exchange.getContext().getShutdownStrategy().setTimeout(60);
    exchange.getContext().stop();
}
}
like image 94
Vijay Nandwana Avatar answered Sep 23 '22 09:09

Vijay Nandwana


There are many ways to approach this. In addition to the accepted answer you may also want to use one of the following:

onException(SomeException.class)
.log("Uh oh...")
.stop()

within your DSL

or:

exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE);

within your Processor()

Have a look at the official documentation on this topic here: http://camel.apache.org/intercept.html

like image 33
Christian Groleau Avatar answered Sep 24 '22 09:09

Christian Groleau