Enrich EIP
Camel supports the Content Enricher from the EIP patterns using a Message Translator, an arbitrary Processor in the routing logic, or using the enrich DSL element to enrich the message.
See the cacheSize option for more details on how much cache to use depending on how many or few unique endpoints are used.
|
The Enrich EIP supports 7 options which are listed below:
Name | Description | Default | Type |
---|---|---|---|
strategyRef |
Refers to an AggregationStrategy to be used to merge the reply from the external service, into a single outgoing message. By default Camel will use the reply from the external service as outgoing message. |
String |
|
strategyMethodName |
This option can be used to explicit declare the method name to use, when using POJOs as the AggregationStrategy. |
String |
|
strategyMethodAllowNull |
If this option is false then the aggregate method is not used if there was no data to enrich. If this option is true then null values is used as the oldExchange (when no data to enrich), when using POJOs as the AggregationStrategy. |
String |
|
aggregateOnException |
If this option is false then the aggregate method is not used if there was an exception thrown while trying to retrieve the data to enrich from the resource. Setting this option to true allows end users to control what to do if there was an exception in the aggregate method. For example to suppress the exception or set a custom message body etc. |
false |
Boolean |
shareUnitOfWork |
Shares the org.apache.camel.spi.UnitOfWork with the parent and the resource exchange. Enrich will by default not share unit of work between the parent exchange and the resource exchange. This means the resource exchange has its own individual unit of work. |
false |
Boolean |
cacheSize |
Sets the maximum size used by the org.apache.camel.spi.ProducerCache which is used to cache and reuse producer when uris are reused. Beware that when using dynamic endpoints then it affects how well the cache can be utilized. If each dynamic endpoint is unique then its best to turn of caching by setting this to -1, which allows Camel to not cache both the producers and endpoints; they are regarded as prototype scoped and will be stopped and discarded after use. This reduces memory usage as otherwise producers/endpoints are stored in memory in the caches. However if there are a high degree of dynamic endpoints that have been used before, then it can benefit to use the cache to reuse both producers and endpoints and therefore the cache size can be set accordingly or rely on the default size (1000). If there is a mix of unique and used before dynamic endpoints, then setting a reasonable cache size can help reduce memory usage to avoid storing too many non frequent used producers. |
Integer |
|
ignoreInvalidEndpoint |
Ignore the invalidate endpoint exception when try to create a producer with that endpoint |
false |
Boolean |
Content enrichment using a Message Translator or a Processor
You can use Templating to consume a message from one destination, transform it with something like Velocity or XQuery, and then send it on to another destination. For example using InOnly (one way messaging)
Samples
from("activemq:My.Queue").
to("velocity:com/acme/MyResponse.vm").
to("activemq:Another.Queue");
If you want to use InOut (request-reply) semantics to process requests on the My.Queue queue on ActiveMQ with a template generated response, then sending responses back to the JMSReplyTo Destination you could use this:
from("activemq:My.Queue").
to("velocity:com/acme/MyResponse.vm");
Here is a simple example using the DSL directly to transform the message body
from("direct:start").setBody(body().append(" World!")).to("mock:result");
In this example we add our own Processor using explicit Java code
from("direct:start").process(new Processor() {
public void process(Exchange exchange) {
Message in = exchange.getIn();
in.setBody(in.getBody(String.class) + " World!");
}
}).to("mock:result");
Finally we can use Bean Integration to use any Java method on any bean to act as the transformer
from("activemq:My.Queue").
beanRef("myBeanName", "myMethodName").
to("activemq:Another.Queue");
Content enrichment using the enrich DSL element
Camel comes with flavor enrich
as a choice of content enricher in the DSL.
The other one is pollEnrich
enrich
uses a Producer to obtain the additional data. It is usually used for Request Reply messaging, for instance to invoke an external web service.
Both enrich
and pollEnrich
supports dynamic endpoints that uses an Expression to compute the uri, which allows to use data from the current Exchange.
Enrich example using Java
AggregationStrategy aggregationStrategy = ...
from("direct:start")
.enrich("direct:resource", aggregationStrategy)
.to("direct:result");
from("direct:resource")
...
The content enricher (enrich
) retrieves additional data from a resource endpoint in order to enrich an incoming message (contained in the original exchange).
An aggregation strategy is used to combine the original exchange and the resource exchange. The first parameter of the AggregationStrategy.aggregate(Exchange, Exchange)
method corresponds to the original exchange, the second parameter the resource exchange.
The results from the resource endpoint are stored in the resource exchange’s out-message. Here’s an example template for implementing an aggregation strategy:
public class ExampleAggregationStrategy implements AggregationStrategy {
public Exchange aggregate(Exchange original, Exchange resource) {
Object originalBody = original.getIn().getBody();
Object resourceResponse = resource.getIn().getBody();
Object mergeResult = ... // combine original body and resource response
if (original.getPattern().isOutCapable()) {
original.getOut().setBody(mergeResult);
} else {
original.getIn().setBody(mergeResult);
}
return original;
}
}
Using this template the original exchange can be of any pattern. The resource exchange created by the enricher is always an in-out exchange.
Enrich example using XML
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:start"/>
<enrich strategyRef="aggregationStrategy">
<constant>direct:resource</constant>
</enrich>
<to uri="direct:result"/>
</route>
<route>
<from uri="direct:resource"/>
...
</route>
</camelContext>
<bean id="aggregationStrategy" class="..." />
Aggregation strategy is optional
The aggregation strategy is optional. If you do not provide it Camel will by default just use the body obtained from the resource.
from("direct:start")
.enrich("direct:resource")
.to("direct:result");
In the route above the message sent to the direct:result endpoint will contain the output from the direct:resource as we do not use any custom aggregation.
<route>
<from uri="direct:start"/>
<enrich>
<constant>direct:resource</constant>
</enrich>
<to uri="direct:result"/>
</route>
Using dynamic uris
Both enrich
and pollEnrich
supports using dynamic uris computed based on information from the current Exchange. For example to enrich from a HTTP endpoint where the header with key orderId is used as part of the content-path of the HTTP url:
from("direct:start")
.enrich().simple("http:myserver/${header.orderId}/order")
.to("direct:result");
And in XML DSL
<route>
<from uri="direct:start"/>
<enrich>
<simple>http:myserver/${header.orderId}/order</simple>
</enrich>
<to uri="direct:result"/>
</route>