Content EnricherPage edited by Claus IbsenChanges (2)
Full ContentContent EnricherCamel 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. Content enrichment using a Message Translator or a ProcessorUsing the Fluent Builders 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) 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 In this example we add our own Processor using explicit Java code 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"); For further examples of this pattern in use you could look at one of the JUnit tests Using Spring XML <route> <from uri="activemq:Input"/> <bean ref="myBeanName" method="doTransform"/> <to uri="activemq:Output"/> </route> Content enrichment using the enrich DSL elementCamel comes with two flavors of content enricher in the DSL
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. Enrich Options
Using the Fluent Builders 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: 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. Using Spring XML The same example in the Spring DSL <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="..." /> Aggregation strategy is optionalThe 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. And for Spring DSL just omit the strategyRef attribute: <route> <from uri="direct:start"/> <enrich uri="direct:resource"/> <to uri="direct:result"/> </route> Content enrichment using pollEnrichThe pollEnrich works just as the enrich however as it uses a Polling Consumer we have 3 methods when polling
PollEnrich Options
If there is no data then the newExchange in the aggregation strategy is null. You can pass in a timeout value that determines which method to use
The timeout values is in millis.
ExampleIn this example we enrich the message by loading the content from the file named inbox/data.txt. from("direct:start") .pollEnrich("file:inbox?fileName=data.txt") .to("direct:result"); And in XML DSL you do: <route> <from uri="direct:start"/> <pollEnrich uri="file:inbox?fileName=data.txt"/> <to uri="direct:result"/> </route> If there is no file then the message is empty. We can use a timeout to either wait (potentially forever) until a file exists, or use a timeout to wait a certain period. For example to wait up to 5 seconds you can do: <route> <from uri="direct:start"/> <pollEnrich uri="file:inbox?fileName=data.txt" timeout="5000"/> <to uri="direct:result"/> </route> Using This PatternIf you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.
Stop watching space
|
Change email notification preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Camel > Content Enricher Claus Ibsen (Confluence)
- [CONF] Apache Camel > Content Enricher Claus Ibsen (Confluence)
- [CONF] Apache Camel > Content Enricher Claus Ibsen (Confluence)