Integration / Apache Camel Interview Questions
How does the Enrich EIP (Content Enricher) work in Camel (enrich vs pollEnrich)?
The Content Enricher augments a message with data fetched from an external resource. Camel provides two variants:
- enrich(uri): Calls the external resource using a producer (e.g., HTTP GET, SQL query) and merges the response into the original message using an AggregationStrategy. The original Exchange is enriched in-place.
- pollEnrich(uri): Polls a consumer endpoint (e.g., file: or jms:) to fetch a resource and merges it. Useful for fetching reference data from a file or a JMS queue.
// enrich: HTTP GET to add product details to an order:
from("jms:queue:orders")
.enrich("http://product-service/api/products",
new AggregationStrategy() {
public Exchange aggregate(Exchange orig, Exchange resource) {
String product = resource.getIn().getBody(String.class);
orig.getIn().setHeader("productDetails", product);
return orig;
}
})
.to("direct:process");
// pollEnrich: fetch the latest reference file:
from("direct:start")
.pollEnrich("file:/data/config?fileName=rates.csv", 3000,
new FileBodyMergeStrategy())
.to("direct:applyRates");The key difference: enrich() PUSHES to the resource endpoint (producer call, needs active request); pollEnrich() PULLS from the resource endpoint (consumer poll). If pollEnrich() times out before finding data (timeout in ms, or -1 to wait indefinitely), it returns the original Exchange unchanged.
More Related questions...