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,
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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.