Apache camel拦截,更新所有RouteBuilder的Exchange消息,然后继续

时间:2015-11-12 19:58:57

标签: java jms apache-camel enterprise-integration

我有CamelConfiguration配置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;
     }
}

我想要实现的目标是检查路由中每个传入的Messageexchange.getHeaders())的标头,如果它不存在则添加标头。< / p>

我可以在每个Processor内使用RouteBuilder来实现这一点。实施例

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)
}

目的是在所有交换消息之间使用相同的id,以便以后更容易将它们联系起来。

那么,是否有一种Camel方式在CamelConfiguration#configure中执行此操作,以便它适用于所有Route

我期望拦截如下。

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

它会被拦截,但似乎不会继续.to() RouteBuilder {/ 1}}。

参考

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

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

1 个答案:

答案 0 :(得分:1)

您可以使用interceptFrom()子句为所有路由设置标头值

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