Saturday, March 9, 2013

Batch operation with Apache Camel

This is a small post of how can we organize batching in apache camel 2.8.0.

Given

Apache camel version 2.8.0. Hot higher because it's production restriction so far.
A list of pojos as input
A csv file as output

Test


Camel allows to advise producer endpoints with mocks, look at setup method. Starting from camel 2.10 mocking can be done with annotations. MockEndpoint provides flexible expectation and assertion mechanism

Implementation 

Spring context


By default split output will be the same as input. Developer can change this behavior by adding AggregationStrategy like:
In camel AggregationStrategy always receives null in oldExchange for the first record in batch. All subsequent batch records will be accumulated in oldExchange by appending String with record from newExchange. Aggregation executed after all transformation in split closure body.

Definition body within split tells camel to drop input list of pojos into single elements and pass them in subsequent csvRowFormatter processor.

CsvFormatter

It's pretty simple. Extract pojo from camel exchange, format csv from it and push it back into exchange.

Conclusion

Camel provides flexible mechanism for building routing engines. It allows to build batching as well. For full version of example refer to github

This post was created mostly like a reminder.

No comments: