简体   繁体   中英

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 Route s.

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

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM