prayagupadhyay
prayagupadhyay

Reputation: 31232

Apache camel intercept, update the Exchange message for all RouteBuilders, and continue

I have a CamelConfiguration that configures 15 Routes.

public class CamelRoutesConf extends CamelConfiguration {

     @Override
     public List<RouteBuilder> configure() {
        List<RouteBuilder> routes = super.routes();
        routes.forEach(router -> {
                  router.onException(Exception.class).delay(5000);
        });
        return routes;
     }
}

What I want to achieve is check the header of every incoming Message (exchange.getHeaders()) inside the route, and add a header if it doesn't exist.

I can achieve that using Processor inside each RouteBuilder. Ex.

public class ArtistHiredRouteBuilder extends RouteBuilder {

  @Override
  public void configure(){
    from(incomingArtistsQ)
    .process(new Processor(){
        public void process(Exchange exchange){
            exchange.getIn().setHeader("JMSCorrelationId", randomNumberOfLength10());
        }
      })
    .to(outgoingArtistsQ)
}

Purpose is to use same id between all exchange messages, so that it becomes easier to relate them later.

So, is there a Camel way of doing this in CamelConfiguration#configure so that it applies to all the Routes.

I expected intercepting as below.

public class CamelRoutesConf extends CamelConfiguration {

     @Override
     public List<RouteBuilder> configure() {
        List<RouteBuilder> routes = super.routes();
        routes.forEach(router -> {
                  router.interceptFrom().process(headerProcessor)
                  router.onException(Exception.class).delay(5000);
        });
     }
}

It will be intecepted but doesn't seem to continue with .to() in each RouteBuilder.

References

http://camel.apache.org/intercept.html

http://www.davsclaus.com/2009/05/on-road-to-camel-20-interceptors-round.html

Upvotes: 1

Views: 1887

Answers (1)

Ben ODay
Ben ODay

Reputation: 21015

you can use the interceptFrom() clause to set your header value for all routes

// intercept all incoming routes and do something...
interceptFrom().setHeader(JMSCorrelationId", randomNumberOfLength10());

Upvotes: 1

Related Questions