Rainer Bonn
Rainer Bonn

Reputation: 133

Apache Camel / Java DSL / How to add a list of processors?

In my project I have several routes, which all have a similar setup. Concrete values for endpoints and property-values as well as which processors should be used are resolved from a config-file.

Thus, I created a method to setup those routes. One parameter is a List or Array of processor-names. Is there a possibility to add a this list of processor-references to the route-definition?

protected void setupRoute(String routeKey, String nbiSystemName, String requestEndpointUrl,
        String defaultSbi, String reqSwitchRouteId, ArrayList<String> processorNames) {
    
    from(requestEndpointUrl)
        .routeId(routeKey).transacted()
        .setProperty(PROPERTY_CBS_STARTTIME, simple("${date:now:yyyy-MM-dd'T'HH:mm:ss.SSS'Z'}"))
        .setProperty(PROPERTY_CBS_NBI, constant(nbiSystemName))
        .setProperty(PROPERTY_CBS_SBI, constant(defaultSbi))
        
//          At this place I want to add all of the processors like
//          .process("processorName")

        .to("direct:" + reqSwitchRouteId );
}

As a workaround I considered using .loop() or .loopDoWhile() with beans instead of processors. But this is the wrong approach from my point of view, because I already know which processors should be used when I setup the route-definition.

Upvotes: 2

Views: 650

Answers (2)

TacheDeChoco
TacheDeChoco

Reputation: 3913

An alternative solution would be to use the RoutingSlip pattern, where you dynamically prepare (and store in a header) the list of the next endpoints. But of course, this requires you to turn your processors into endpoints.

More info at: https://camel.apache.org/components/latest/eips/routingSlip-eip.html

Upvotes: 0

ernest_k
ernest_k

Reputation: 45339

The simplest solution here is just iterating over the list and updating the route definition:

RouteDefinition route = from(requestEndpointUrl)
    .routeId(routeKey).transacted()
    .setProperty(PROPERTY_CBS_STARTTIME, 
                 simple("${date:now:yyyy-MM-dd'T'HH:mm:ss.SSS'Z'}"))
    .setProperty(PROPERTY_CBS_NBI, constant(nbiSystemName))
    .setProperty(PROPERTY_CBS_SBI, constant(defaultSbi));

for(String processorName: processorNames)
    route = route.process(processorName);

route.to("direct:" + reqSwitchRouteId );

Upvotes: 1

Related Questions