Generate multiple from() dynamically Apache Camel RouteBuilder

自闭症网瘾萝莉.ら 提交于 2020-12-13 05:41:50

问题


I was using camel-core 2.24.1 and was able to do the following:

from( sources.toArray(new String[0]) )

where sources is a list of URIs that I get from a configuration settings. I am trying to update the code to use Camel 3 (camel-core 3.0.0-RC2) but the method mentioned above was removed and I can't find another way to accomplish the same.

Basically I need something like:

from( String uri : sources )
{
   // add the uri as from(uri) before continuing with the route
}

In case this would help understand better, the final route should look like:

      from( sources.toArray(new String[0]) )
      .routeId(Constants.ROUTE_ID)
      .split().method(WorkRequestSplitter.class, "splitMessage")
        .id(Constants.WORK_REQUEST_SPLITTER_ID)
      .split().method(RequestSplitter.class, "splitMessage")
        .id(Constants.REQUEST_SPLITTER_ID)
      .choice()
        .when(useReqProc)
          .log(LoggingLevel.INFO, "Found the request processor using it")
          .to("bean:" + reqName)
        .endChoice()
        .otherwise()
          .log(LoggingLevel.ERROR, "requestProcessor not found, stopping route")
          .stop()
        .endChoice()
      .end()
      .log("Sending the request the URI")
      .recipientList(header(Constants.HDR_ARES_URI))
      .choice()
        .when(useResProc)
          .log(LoggingLevel.INFO, "Found the results processor using it")
          .to("bean:" + resName)
        .endChoice()
        .otherwise()
          .log(LoggingLevel.INFO, "resultProcessor not found, sending 'as is'")
        .endChoice()
      .end()
      .log("Sending the request to all listeners")
      .to( this.destinations.toArray( new String[0] ) );

Any help will be greatly appreciated.


回答1:


This feature was removed with no direct replacement in CAMEL-6589.

See Migration guide:

In Camel 2.x you could have 2 or more inputs to Camel routes, however this was not supported in all use-cases in Camel, and this functionality is seldom in use. This has also been deprecated in Camel 2.x. In Camel 3 we have removed the remaining code for specifying multiple inputs to routes, and its now only possible to specify exactly only 1 input to a route.

You can always split your route definition to logical blocks with Direct endpoint. This can be also generated dynamically with for-each.

for(String uri : sources){
    from(uri).to("direct:commonProcess");
}

from("direct:commonProcess")
    .routeId(Constants.ROUTE_ID)
    //...
    .log("Sending the request to all listeners")
    .to(this.destinations.toArray(new String[0]));


来源:https://stackoverflow.com/questions/58451201/generate-multiple-from-dynamically-apache-camel-routebuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!