Reputation: 2634
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.
Upvotes: 8
Views: 16880
Reputation: 95
The above answers show you how to handle the exception. But, to stop the route from throwing exception specify :
?throwExceptionOnFailure=false
in the URI.
example :
.to(http://localhost:8000/4d-analytics-mock-service/iso/retrieve?throwExceptionOnFailure=false)
Upvotes: 0
Reputation: 101
Stops the route current execution, but the route still listening
route.onException(Throwable.class).process(ThrowableProcessor.NAME).handled(true);
Upvotes: 1
Reputation: 812
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
Upvotes: 5
Reputation: 169
If you create a OnException Route? Like this:
onException(Exception.class)
.log("YOUR LOG EXCEPTION");
from("direct:start")...
Upvotes: 0
Reputation: 2634
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();
}
}
Upvotes: 6