...
Code Block |
---|
public class ExampleAggregationStrategy implements AggregationStrategy { public Exchange aggregate(Exchange original, Exchange resource) { Object origBody = original.getIn().getBody(); Object theResponse = 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 Dynamic URIs with Enrich
Both the enrich()
and pollEnrich()
methods support the use of dynamic URIs that are computed based on information from the current exchange. For example, to enrich from an HTTP endpoint where the header with the orderId
key is used as part of the content path of the HTTP URL:
...
Code Block |
---|
<route>
<from uri="direct:start"/>
<enrich>
<simple>http:myserver/${header.orderId}/order</simple>
</enrich>
<to uri="direct:result"/>
</route> |
Use of pollEnrich Method
The pollEnrich
command will utilize the resource endpoint as a consumer. Therefore, instead of sending an exchange to the resource endpoint, it polls the endpoint and (by default) the poll returns immediately if there is no exchange available from the resource endpoint.
Code Block |
---|
from("activemq:queue:order")
.pollEnrich("file://order/data/additional?fileName=orderId")
.to("bean:processOrder"); |
We can limit the time to wait for the file to be ready:
Code Block |
---|
from("activemq:queue:order")
.pollEnrich("file://order/data/additional?fileName=orderId", 20000) // timeout is in milliseconds
.to("bean:processOrder"); |
Polling Methods
The pollEnrich()
method will poll our consumer endpoints by using one of the following poling methods:
receiveNoWait()
(This is the default.)receive()
receive(long timeout)
Additionally, the timeout parameter of pollEnrich()
command is specified in milliseconds and the evaluation of its value will determine which method to call:
When the timeout is
0
or not specified,pollEnrich()
callsreceiveNoWait
.When the timeout is negative,
pollEnrich()
callsreceive
.Otherwise,
pollEnrich()
callsreceive(timeout)
.
pollEnrich Examples
The following shows enrichment of the message by loading the content from the inbox/data.txt
file:
Code Block |
---|
from("direct:start")
.pollEnrich("file:inbox?fileName=data.txt")
.to("direct:result"); |
Or, this example in XML DSL:
Code Block |
---|
<route>
<from uri="direct:start"/>
<pollEnrich>
<constant>file:inbox?fileName=data.txt"</constant>
</pollEnrich>
<to uri="direct:result"/>
</route> |
NOTE: If the specified file does not exist then the message is empty.
pollEnrich Timeout
We can specify a timeout to wait (potentially forever) until a file exists or to wait up to a particular length of time. In the following example, the command waits no more than 3 seconds:
Code Block |
---|
<route>
<from uri="direct:start"/>
<pollEnrich timeout="3000">
<constant>file:inbox?fileName=data.txt"</constant>
</pollEnrich>
<to uri="direct:result"/>
</route> |