Guide to Message Router (Messaging Systems)
This is a type of filter that consumes messages from a single consumer endpoint and redirects them to the appropriate target endpoint, based on a particular decision criterion. A message router is concerned only with redirecting messages; it does not modify the message content.
When we need to decouple our individual processing steps so that the message can be routed to different filtering processes based on some identifiable conditions, then we use a Message Router. It will consume the message from a message channel, review its set of conditional criteria and then pass the message to a specific message channel.
This interaction pattern differs from the Pipes and Filters pattern since it will connect to more than one outgoing message channel.
Â
A key aspect of the Message Router, and another difference with Pipes and Filters, is that it will not modify the contents of the message. It is only concerned with the destination for the message and not any transformations.
Â
Message Router using XML DSL:
We can determine how to route a request from an input (direct:a) endpoint to either direct:x, direct:y, or direct:z depending on the evaluation of various criteria:
Â
<route>
<from uri="direct:a"/>
<choice>
<when>
<simple>${header.action} == 'add'</simple>
<to uri="direct:x"/>
</when>
<when>
<simple>${header.action} == 'upsert'</simple>
<to uri="direct:y"/>
</when>
<otherwise>
<to uri="direct:z"/>
</otherwise>
</choice>
</route>