使用Java DSL可以配置Camel分割选项

时间:2015-02-25 17:29:11

标签: java spring properties split apache-camel

我的Camel路线中有一个看起来像这样的分离器......

from("direct:myRoute")
// Split each exchange into multiple sub-exchanges on the basis of MySplitter.class 
// Configure the splitter to stop on exception that comes up during processing of each sub-exchange
// Configure the splitter to share unit of work with the main exchange that means processing of the entire Exchange is an atomic success/failure
.split().method(MySplitter.class).stopOnException().shareUnitOfWork()
    //do something with each sub-exchange
    .to("direct:processEachSubExchange")
.end();

我想做的是我希望将stopOnException保持为可配置状态。这意味着我希望在外部化属性的帮助下根据需要启用/禁用停止功​​能。

这可以使用Java DSL吗?

2 个答案:

答案 0 :(得分:1)

一种可能的解决方案是使用两个不同的子路由:一个在异常时停止,另一个不在异常上。使用系统属性动态选择子路由:

from("direct:start")
    .to("{{mySubRoute}}");;

from("direct:mySubRouteWithStopOnException")
    .split().method(MySplitter.class).stopOnException().shareUnitOfWork()
    .to("direct:processEachSubExchange")
    .end();

from("direct:mySubRouteWithoutStopOnException")
    .split().method(MySplitter.class).shareUnitOfWork()
    .to("direct:processEachSubExchange")
    .end();

例如,设置系统属性如下:

System.setProperty("mySubRoute", "direct:mySubRouteWithStopOnException");

在此示例中,选择了在例外情况下停止的子路线。

答案 1 :(得分:1)

您也可以使用选择块指导路线,然后您可以对内容进行特定的异常处理。

from("{{somewhere.in.endpoint}}")
   .choice()
      .when(header("endOnExceptionFlag").isEqualTo(true))
         .to("direct:splitEndOnException")
      .otherwise()
         .to("direct:splitIgnoreExceptions")
   .endChoice()
.end()

// process split with exception handling
.from("direct:splitEndOnException")
   .split().method(MySplitter.class).stopOnException().shareUnitOfWork()
      .to("direct:processEachSubExchange")
.end();

// process split ignoring exceptions
.from("direct:splitIgnoreExceptions")
   .split().method(MySplitter.class).shareUnitOfWork()
      .to("direct:processEachSubExchange")
.end();