使用JavaDSL使骆驼分割选项可配置

使用JavaDSL使骆驼分割选项可配置,java,spring,properties,split,apache-camel,Java,Spring,Properties,Split,Apache 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 spli

我的骆驼路线中有一个拆分器,看起来像这样

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可以做到这一点吗?

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

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

在本例中,选择了带有stop on exception的子路由。

您也可以使用选择块来引导路由,然后可以对异常进行特定于内容的处理

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