Apache CAMEL Dynamic Recipient List (2011-04-26)
Usually one of the main reasons for using the Recipient List pattern is that the list of recipients is dynamic and calculated at runtime. The following example demonstrates how to create a dynamic recipient list using an Expression (which in this case it extracts a named header value dynamically) to calculate the list of endpoints which are either of type Endpoint or are converted to a String and then resolved using the endpoint URIs.
ตัวอย่างนี้เป็นการใช้ CamelTestSupport
public class RecipientListTest extends CamelTestSupport {
private Logger log = LoggerFactory.getLogger(RecipientListTest.class);
@EndpointInject(uri = "mock:result")
protected MockEndpoint resultEndpoint;
@Produce(uri = "direct:start")
protected ProducerTemplate template;
@Produce(uri = "direct:start2")
protected ProducerTemplate template2;
protected ConsumerTemplate consumerTemplate;
@EndpointInject(uri = "mock:result2")
protected MockEndpoint resultEndpoint2;
@EndpointInject(uri = "mock:result3")
protected MockEndpoint resultEndpoint3;
@EndpointInject(uri = "mock:result4")
protected MockEndpoint resultEndpoint4;
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
from("direct:start2").log("=====>>>>>>> send ${body} ").recipientList(simple("mock:result2, mock:result3, mock:result4")).parallelProcessing();
}
};
}
public void testMulticast() throws InterruptedException {
resultEndpoint2.expectedBodiesReceived("testMulticast");
resultEndpoint3.expectedBodiesReceived("testMulticast");
resultEndpoint4.expectedBodiesReceived("testMulticast");
template2.sendBody("testMulticast");
resultEndpoint2.assertIsSatisfied();
resultEndpoint3.assertIsSatisfied();
resultEndpoint4.assertIsSatisfied();
}
}
|
ไม่ยากนะครับลองเอาไปใช้กันดู มีประโยชน์ในการทำ Multicast ไปยัง enpoint หลายๆ ตัว
Tip อีกนิดใน RouteBuilder สามารถกำหนด DSL เป็น from("direct:start2").multicast().to("mock:b", "mock:c", "mock:d"); ก็ได้นะครับ
|