如果使用的URI是动态的,使用pollEnrich会出现以下问题:
为了解决这个问题,可以使用Recipient List EIP来代替pollEnrich。
Recipient List EIP可以动态选择要发送消息的路由,并将任何响应合并回原始Exchange。
以下是使用Recipient List EIP替换pollEnrich的示例代码:
from("direct:start") .recipientList().method(MyDynamicRecipientList.class) .parallelProcessing() .setHeader("originalMessage", simple("${body}")) .aggregate(new MyAggregationStrategy()) .completionSize(2) .completionTimeout(1000) .log("${header.originalMessage} + ' enriched with ' + ${body}") .to("direct:end");
public class MyDynamicRecipientList { public List getRecipients(@Header("myHeader") String myHeaderValue) { // Compute a dynamic URI based on the value of myHeader String dynamicURI = "ftp://myserver/" + myHeaderValue;
// Return a List containing the dynamic URI
return Collections.singletonList(dynamicURI);
} }
public class MyAggregationStrategy implements AggregationStrategy { public Exchange aggregate(Exchange oldExchange, Exchange newExchange) { // Merge the new message into the original message String enrichedMessage = oldExchange.getIn().getBody(String.class) + " + " + newExchange.getIn().getBody(String.class);
// Set the new message as the body of the original Exchange
oldExchange.getIn().setBody(enrichedMessage);
return oldExchange;
} }