Routing each message conditionally to a series of endpoints
This scenario applies only to Talend ESB, Talend Data Services Platform, Talend MDM Platform, Talend Real-Time Big Data Platform, and Talend Data Fabric.
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:
<person>
<firstName>Nicolas</firstName>
<lastName>Yang</lastName>
<city>Beijing</city>
</person>
Paris.xml:
<person>
<firstName>Pierre</firstName>
<lastName>Dupont</lastName>
<city>Paris</city>
</person>
A predefined JavaBean, 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 JavaBeans, see Using Beans.
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";
}
}