Routing files conditionally to different file paths - 7.0

Dynamic Router - Mediation

author
Talend Documentation Team
EnrichVersion
7.0
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Data Governance > Third-party systems > Routing components (Mediation) > Dynamic Router components (Mediation)
Data Quality and Preparation > Third-party systems > Routing components (Mediation) > Dynamic Router components (Mediation)
Design and Development > Third-party systems > Routing components (Mediation) > Dynamic Router components (Mediation)
EnrichPlatform
Talend Studio

This scenario applies only to Talend Open Studio for ESB, Talend Data Services Platform and Talend Data Fabric.

For more technologies supported by Talend, see Talend Mediation Components.

In this scenario, three file messages containing people information are routed to different endpoints according to the city names they contain.

The following is an extract of the example XML files used in this use case:

Message_1.xml:

<person>
  <firstName>Ellen</firstName>
  <lastName>Ripley</lastName>
  <city>Washington</city>
</person>

Message_2.xml:

<person>
  <firstName>Peter</firstName>
  <lastName>Green</lastName>
  <city>London</city>
</person>

Message_3.xml:

<person>
  <firstName>Alice</firstName>
  <lastName>Yang</lastName>
  <city>Beijing</city>
</person>

A predefined Java bean, setDynaURI, is called in this use case to return endpoint URIs according to the city name contained in each message, so that the message containing the city name Washington will be routed to endpoint Washington and so forth.

For more information about creating and using Java Beans, see Talend Studio User Guide.

package beans;

import org.apache.camel.Exchange;
import org.apache.camel.Header;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class setDynaURI {

 public String setURI(Document document,
      @Header(Exchange.SLIP_ENDPOINT) String previous) {
    if(previous!=null){
     return null;
    }
  NodeList cities = document.getDocumentElement().getElementsByTagName(
    "city");
  Element city = (Element) cities.item(0);
  String textContent = city.getTextContent();
   return "direct:"+textContent;
  } 
}