...
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.
...
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)
Code Block |
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:
Code Block |
from("activemq:My.Queue").
to("velocity:com/acme/MyResponse.vm");
|
...
Finally we can use Bean Integration to use any Java method on any bean to act as the transformer
Code Block |
from("activemq:My.Queue").
beanRef("myBeanName", "myMethodName").
to("activemq:Another.Queue");
|
...
Using Spring XML
Code Block |
<route>
<from uri="activemq:Input"/>
<bean ref="myBeanName" method="doTransform"/>
<to uri="activemq:Output"/>
</route>
|
...
div |
class |
confluenceTableSmall |
|
Name |
Default Value |
Description |
uri |
|
The endpoint uri for the external service to enrich from. You must use either uri or ref . |
ref |
|
Refers to the endpoint for the external service to enrich from. You must use either uri or ref . |
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. From Camel 2.12 onwards you can also use a POJO as the AggregationStrategy , see the Aggregate page for more details. |
strategyMethodName |
|
Camel 2.12: This option can be used to explicit declare the method name to use, when using POJOs as the AggregationStrategy . See the Aggregate page for more details. |
strategyMethodAllowNull |
false |
Camel 2.12: 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 . See the Aggregate page for more details. |
aggregateOnException |
false |
Camel 2.14: 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. |
|
Using the Fluent Builders
Code Block |
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 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:
Code Block |
|
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;
}
}
|
...
The same example in the Spring DSL
Code Block |
|
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:start"/>
<enrich uri="direct:resource" strategyRef="aggregationStrategy"/>
<to uri="direct:result"/>
</route>
<route>
<from uri="direct:resource"/>
...
</route>
</camelContext>
<bean id="aggregationStrategy" class="..." />
|
...
The aggregation strategy is optional. If you do not provide it Camel will by default just use the body obtained from the resource.
Code Block |
from("direct:start")
.enrich("direct:resource")
.to("direct:result");
|
...
And for Spring DSL just omit the strategyRef
attribute:
Code Block |
|
<route>
<from uri="direct:start"/>
<enrich uri="direct:resource"/>
<to uri="direct:result"/>
</route>
|
...
div |
class |
confluenceTableSmall |
|
Name |
Default Value |
Description |
uri |
|
The endpoint uri for the external service to enrich from. You must use either uri or ref . |
ref |
|
Refers to the endpoint for the external service to enrich from. You must use either uri or ref . |
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. From Camel 2.12 onwards you can also use a POJO as the AggregationStrategy , see the Aggregate page for more details. |
strategyMethodName |
|
Camel 2.12: This option can be used to explicit declare the method name to use, when using POJOs as the AggregationStrategy . See the Aggregate page for more details. |
strategyMethodAllowNull |
false |
Camel 2.12: 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 . See the Aggregate page for more details. |
timeout |
-1 |
Timeout in millis when polling from the external service. See below for important details about the timeout. |
aggregateOnException |
false |
Camel 2.14: 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. |
|
Info |
title |
Good practice to use timeout value |
|
By default Camel will use the receive . Which may block until there is a message available. It is therefore recommended to always provide a timeout value, to make this clear that we may wait for a message, until the timeout is hit. |
...
In this example we enrich the message by loading the content from the file named inbox/data.txt.
Code Block |
from("direct:start")
.pollEnrich("file:inbox?fileName=data.txt")
.to("direct:result");
|
And in XML DSL you do:
Code Block |
|
<route>
<from uri="direct:start"/>
<pollEnrich uri="file:inbox?fileName=data.txt"/>
<to uri="direct:result"/>
</route>
|
...
For example to wait up to 5 seconds you can do:
Code Block |
|
<route>
<from uri="direct:start"/>
<pollEnrich uri="file:inbox?fileName=data.txt" timeout="5000"/>
<to uri="direct:result"/>
</route>
|
Include Page |
|