使用 Java DSL 使 Camel 拆分选项可配置

Make Camel split options configurable using Java DSL

我的 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 保持可配置。这意味着我想 enable/disable 在外部化 属性.

的帮助下按需出现异常时停止的功能

这可以使用 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");

本例中选择异常停止的子路由

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

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