Question

I am retrieving multiple rows from table and then want that my processor process them, but i observe camel is calling my processor for every row, i want to pass List of entities

following is my code

from("jpa:com.pns.ab.model.LoanRequest?consumeDelete=false"
            + "&consumer.delay=20000"
            + "&consumer.namedQuery=selectLoanRequests"
            + "&persistenceUnit=LoanServicePU").process(new JpaProcessor());

in processor

LoanRequest lr = exchange.getIn().getBody(LoanRequest.class);

but i want option like

List<LoanRequest> requests  = exchange....

Regards,

Was it helpful?

Solution

Use an aggregator:

private static class JpaAggregationRouteBuilder extends RouteBuilder {
    @Override
    public void configure() {
        from("jpa:com.pns.ab.model.LoanRequest?consumeDelete=false"
             + "&consumer.delay=20000"
             + "&consumer.namedQuery=selectLoanRequests"
             + "&persistenceUnit=LoanServicePU")
                .aggregate(constant(true), new ArrayListAggregationStrategy())
                .completionFromBatchConsumer()
                .process(new JpaProcessor());
    }
}

// Simply combines Exchange body values into an ArrayList<Object>
// Taken from http://camel.apache.org/aggregator2
private static class ArrayListAggregationStrategy implements AggregationStrategy {

    @SuppressWarnings("unchecked")
    @Override
    public Exchange aggregate(final Exchange oldExchange, final Exchange newExchange) {
        final Object newBody = newExchange.getIn().getBody();
        ArrayList<Object> list = null;
        if (oldExchange == null) {
            list = new ArrayList<Object>();
            if (newBody != null) {
                list.add(newBody);
            }
            newExchange.getIn().setBody(list);
            return newExchange;
        } else {
            list = oldExchange.getIn().getBody(ArrayList.class);
            if (newBody != null) {
                list.add(newBody);
            }
            return oldExchange;
        }
    }

}

More information about the aggregator can be found on the Camel webpage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top