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.
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();
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With