1

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

prayagupa
  • 30,204
  • 14
  • 155
  • 192

1 Answers1

1

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());
Ben ODay
  • 20,784
  • 9
  • 45
  • 68
  • Tried similar approach with `.interceptFrom().process(myProcessor)` as well inside `CamelRoutesConf #configure`. But doesn't intercept at all. Is executed after `.from().to();` in each `RouteBuilder`s – prayagupa Nov 12 '15 at 20:28
  • Interesting, that actually does work. Let me dig more so that I can make it testable. – prayagupa Nov 12 '15 at 21:05
  • does interceptFrom() continues the routeBuilders `.to()` as it was, after intercepting `.from()`? I think it doesnt work that way?? – prayagupa Nov 16 '15 at 16:56
  • my goal was to intercept the `from(Queue)` and then add header to the exchange and continue with `.to(Queue)`. I tried `"direct:addCorrelationId"` as well, no luck – prayagupa Nov 16 '15 at 17:16