Kasper Odgaard
Kasper Odgaard

Reputation: 113

Camel route is activated from java - how to get the response from the last route

From my spring boot using camel I would like to know if it is possible to get the response from the camel route when it's completed (the last route in my example). It's started using ProducerTemplate like this:

@Component
public class CamelSender {

@Produce(uri = "direct:start")
private ProducerTemplate template;

public void callRoute(List<String> list) throws ExecutionException, InterruptedException {
    template.sendBodyAndHeader("direct:start", list, "orderId", "123456677"
    );
}
}

// camel routes

from("direct:start")
    .log("Split -> Process order ${body}")
    .split().body().to("direct:actionQueue") 
    .end();

 from("direct:actionQueue")
    .bean(ValidateOrders.class)
    .log("Sending to join queue")
    .to("direct:joinQueue");

 from("direct:joinQueue").aggregate(new MyOrderAggregationStrategy())
    .header("orderId")
    .completionTimeout(1000L)
    .process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
                    List<String> orders =   (  (List<String>)  exchange.getIn().getBody());
                    String collect = orders.stream().map(order -> order.toString()).collect(Collectors.joining(","));
                    exchange.getIn().setBody( "Collected validations: "+collect);
                }
            })
            .log("${body}");

How to return the body with the collected validations (a string) to my java bean ?

Upvotes: 0

Views: 772

Answers (1)

Claus Ibsen
Claus Ibsen

Reputation: 55760

Use the composite message processor EIP instead of a split + aggregate. As the latter is two separate processes where the output of the aggregator cannot be sent back to the splitter as response. The former can do that by using the built-in aggregate strategy in the splitter.

You can find example and more details on the Camel EIP - see the splitter only example: http://camel.apache.org/composed-message-processor.html

Upvotes: 1

Related Questions