Java DSL Recipient List (Dynamic Router)

When using a Dynamic Router, it implies the usage of a recipient list to control the routing of messages to a designated set of message consumers. We understand that we need to define a channel for each recipient or consumer. The role of the Recipient List is to inspect each inbound message, determine the list of desirable recipients, and forward the message to all channels that are linked with the recipients in our list.

In the following example, we see both a static and dynamic implementation of our recipient list.

 

Java DSL Recipient List Static Example

In this route option, we are routing to a static list of just two different recipients, both of them will receive a copy of the same message simultaneously.

from("jms:queue:inbox") .multicast().to("file://backup", "accounting:inbox");

Note: Camel supports the use of the static Recipient List using the multicast node, and with the dynamic Recipient List we can employ the recipientList node.

 

Java DSL Recipient List Dynamic Example

In this route option, we are routing to a dynamic list of recipients defined in the message header (mails) that contains the list of recipients as endpoint URLs. Then, the bean processMails is used to add the header[mails] to the message.

from("accounting:confirmMails").beanRef(processMails) .recipientList("destinations");

Then, in the processMails bean, we will use the @Headers annotation we specify a Map object to store all of the recipients for our list.

... public void confirm(@Headers Map headers, @Body String body} { String[] recipients = ... headers.put(""destinations", recipients); }

 

Â