Scenario 2: Routing each message conditionally to a series of endpoints
This scenario applies only to a Talend solution with ESB.
In this scenario, which is based on the previous scenario, each message from a file
system is routed consecutively to different endpoints according to the city name it
contains.
All files used in this use case are named after the city name they contain. The
following are the extracts of two examples:
Beijing.xml:
1 2 3 4 5 |
<person> <firstName>Nicolas</firstName> <lastName>Yang</lastName> <city>Beijing</city> </person> |
Paris.xml:
1 2 3 4 5 |
<person> <firstName>Pierre</firstName> <lastName>Dupont</lastName> <city>Paris</city> </person> |
A predefined Java Bean, setEndpoints, is called in this use case
to return endpoint URIs according to the city name contained in each message, so that
the messages will be routed as follows:
-
The message containing the city name Paris will be routed
first to endpoint a, then to endpoint
b, and finally to endpoint c. -
The message containing the city name Beijing will be
routed first to endpoint c, then to endpoint
a, and finally to endpoint b. -
Any other messages will be routed to endpoint b and then
to endpoint c.
For more information about creating and using Java Beans, see
Talend Studio User
Guide.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package beans; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class setEndpoints { public String helloExample(Document document) { NodeList cities = document.getDocumentElement().getElementsByTagName( "city"); Element city = (Element) cities.item(0); String textContent = city.getTextContent(); if ("Paris".equals(textContent)) { return "direct:a,direct:b,direct:c"; } else if ("Beijing".equals(textContent)) { return "direct:c,direct:a,direct:b"; } else return "direct:b,direct:c"; } } |