camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ranx <brad.john...@mediadriver.com>
Subject Re: How to return early from a direct: subroute?
Date Sun, 03 Apr 2016 16:28:22 GMT
You can also throw an exception in a processor to short circuit.  From the
use case it does sound like a runtime exception of some sort.  

Part of the answer to that question depends on why the processors want to
short circuit.  Can you put a validator at the beginning of the route?  If
the conditions don't meet what you require you could throw an exception from
it and stop the route from processing further.  I usually use Pojos and not
Camel Processors since I don't want to couple to the framework.  But in your
case, if the first processor at the beginning of the route is a validator
you could set a header and use an interceptor as was suggested by Taariq.

I'm not sure what your processor look like but you could also have the
processors themselves check for the header.  If you then have a base class
for your headers that implements Processor it might have a method:

protected Boolean isStop(Exchange e)
{
  //pseudo code here as I don't recall exchange semantics.
  return e.getHeader("ROUTE_STOP")!=null;

}

Then at the  beginning of the process method you could call:
public void process(Exchange exchange) throws Exception {
    if (isStop(exchange)) return;
    //Otherwise doe normal processing.
}
 
The downside is it would still call every one of your processors they simply
would be no-ops.

The parent class could also do this however:

protected void checkStop(Exchange e)
{
  //pseudo code here as I don't recall exchange semantics.
  if(e.getHeader("ROUTE_STOP")!=null) throw new RuntimeException("Route
short circuited.");

}

public void process(Exchange exchange) throws Exception {
    checkStop(exchange);
    //If the checkStop threw and exception you wouldn't reach here.  
    //Otherwise do normal processing.
}
 



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-return-early-from-a-direct-subroute-tp5780262p5780374.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Mime
View raw message