Spring Integration Routers LAB-12
Routers direct messages to an appropriate message channel typically based on what is in
the payload or header of a message. Routers do not alter the message like a transformer.
Routers don’t remove messages from the system like a filter can. They simply provide
forks in the road of message channels in a Spring Integration (SI) application. In this lab,
you explore a few commonly used SI routers.
Specifically, in this lab you will:
- Configure and use an XPath router to route XML payload messages.
- Define a recipient router to send the same message to a collection of channels.
Scenario – Route using XPath expression
In previous filter Lab, we used XPath expressions in filters to remove or discard some XML messages
from the system. In a similar fashion, as will be demonstrated in this part of the lab, an
XPath router uses an XPath expression to determine which message channel receives a
message containing XML in its payload.
In particular, note that the application already contains an inbound file adapter and
a file-to-string transformer to take XML files and get them to String form. Below is an EIP
model representing what is currently defined in the si-components.xml file. The adapter
gets XML files to the inboundChannel and then the transformer converts the file messages to
string messages for the xml-inboundChannel.
At this point, there are no outbound adapters.
Create an XPath router The ship order XML messages will again be used in the last to experiment with routers. The XML ship order messages contain a element nested in a element which in turn is nested in the root element. In this step, you build an XPath router to direct those XML messages containing “Norway” as the elements value to a norwayChannel and those containing “USA” to a usaChannel.
Add two messages channels – one for USA ship orders and one for Norway ship orders.
Add two outbound file adapters. Add two file adapters; one for USA ship orders and one for Norway ship orders. Each file adapter will place the messages it receives from the appropriate message channel in separate file system directories so that the routing can be seen to work.
- Add a Norway outbound file adapter, as shown below, to place its messages in a directory called outboundNorway.
- Add a USA outbound file adapter to place its messages in a directory called outboundUSA.
Add an XPath message router. The message router should use and XPath expression to get the value in the element and map it to the appropriate message channel as defined in step above.
Note the use of mapping elements in the router to map the value discovered in the
XPath expression to one of two message channels.
With the addition of this router component, you have provided a “fork” that
distributes the inbound XML messages to the appropriate directory based on the
content of the message.

The XPath router is a type of content router as it uses some part of the content of
the message – in this case an element from the XML – to route the message through
its intended path in the system.
Test the Router
In the si-components.xml, find the producer-file-adapter. Note the location of the
directory. It is set, by default, to file:c://inboundXML. This is the location where the XML
ship order messages will be taken into the application by the adapter. It is the same folder
used in the last lab (lab 4). If it does not already exist, create this message folder – changing
the location to suit your needs and your file system (change the producer-file-adapter to
reflect your location).
Test the application. Test the application to see the Files in the inbound file folder are routed to the correct outbound folder based on the element value
Using a Windows Explorer, open the two folders specified as the output adapter’s directories (these should be c://outboundNorway and c://outboundUsa per configuration in step 2.2 above). See that the messages have been routed to the correct folder based on the element in the message.
Stop the application. Recall the application is running in an infinite loop to allow
for the constant publishing and consuming of messages. Stop the application now
TO DO EXAMPLE AND GIT REPO details