cMessageRouter
conditions so that messages can later on be treated more accurately in each new
channel.
cMessageRouter Standard properties
These properties are used to configure cMessageRouter running in the Standard Job framework.
The Standard
cMessageRouter component belongs to the Routing family.
Usage
Usage rule |
cMessageRouter is used as a |
Connections |
Row / Route: Select this link to pass on all the messages to the next component. |
Trigger / When: Select the In the Type list, select the type of language you will
Append endChoice(): If the When In the Condition field, type in the condition that will All the messages that do not match this condition are retrieved with the Note:
There can be more than one When link in a Route. |
|
 |
Trigger / Otherwise: This link Note:
There can be only one Otherwise link, which is |
Limitation |
It is recommended not to put any message handling after the |
Routing messages according to a criterion
This scenario applies only to Talend Open Studio for ESB, Talend Data Services Platform and Talend Data Fabric.
Of
the four XML files used in this scenario, Message_1.xml and Message_4.xml
contain the city name of Paris. The following is an
example:
1 2 3 4 5 |
<person> <firstName>Pierre</firstName> <lastName>Dupont</lastName> <city>Paris</city> </person> |
Dropping and linking the components
-
From the Connectivity folder of the Palette, drop three cFile and four cMessagingEndpoint components onto the design workspace, and
label them Sender, Receiver_Paris, and Receiver_Others, directParis, directOthers, directParisRoute, and directOthersRoute respectively to better identify their
roles. -
From the Routing folder, drop a cMessageRouter component onto the design
workspace, and label it Message_router. - From the Miscellaneous folder, drop two cLog components onto the design workspace, and label them Monitor_Paris and Monitor_Others respectively.
-
Right-click the cFile component labeled
Sender, select Row > Route from the
contextual menu and click the cMessageRouter component. - Right-click the cMessageRouter component, select Trigger > When from the contextual menu and click the cMessagingEndpoint component labeled directParis. This endpoint will retrieve the messages that meet the defined criterion.
- Right-click the cMessageRouter component, select Trigger > Otherwise from the contextual menu and click the cMessagingEndpoint component labeled directOthers. This endpoint will collect all the messages that do not meet the filter criterion.
- Right-click the cMessagingEndpoint component labeled directParis, select Row > Route from the contextual menu and click the cFile component labeled Receiver_Paris. Repeat this operation to link the component labeled Receiver_Paris to Monitor_Paris, directOthersRoute to Receiver_Others, and Receiver_Others to Monitor_Others respectively using the Row > Route connection.
Configuring the components and connections
The cMessageRouter component does not have any
property as it filters and routes the messages from one endpoint to others based on
the conditions set in its When
connection(s).
-
Double-click the cFile component labeled
Sender to open its Basic settings view in the Component tab. -
In the Path field, specify the file path to message source.
From the Encoding list, select the encoding type of your message files. Leave the other parameters as they are.
-
In the design workspace, click the When
connection you created and click the Component view to define a filter against which messages
will be routed. -
In the Type list, select xpath because the format of the messages used is XML.
In the Condition field, type in
"/person[city='Paris']"
to retrieve only those messages in which the value of the city node is Paris. -
Double-click the cMessagingEndpoint component labeled directParis to open its Basic settings view in the Component tab.
- In the URI field, enter the endpoint URI, for example, “direct:Paris” to receive the filtered message.
- Repeat these steps to set the endpoint URI of the cMessagingEndpoint components labeled directOthers as “direct:Others”. Set the endpoint URIs of the cMessagingEndpoint components labeled directParisRoute and directOthersRoute as “direct:Paris” and “direct:Others” respectively.
-
Double-click the cFile component labeled Receiver_Paris to open its Basic settings view in the Component tab, and specify the path for the messages meeting the filter criterion in the Path field.
Repeat this step to define the path for all the other messages from the sender in the cFile component labeled Receiver_Others.
-
Double-click the cLog component labeled Monitor_Paris to open its Basic settings view in the Component tab.
-
Select INFO in the Level list. Select the Specify output log message option and enter the following code in the Message field to display the filename of the message sent to the specified directory.
1Message sent to folder Paris_only: ${header.CamelFileNameOnly}Repeat this step to customize the message in the cLog component labeled Monitor_Others to display the filename of the message sent to the specified directory.
- Press Ctrl+S to save your Route.
Viewing code and executing the Route
-
Click the Code tab at the bottom of the
design workspace to have a look at the generated code.As shown in the code, the messages are routed according to conditions initialized with the.choice()
piece of code. The filter you defined is initialized with the.when()
piece of code, and the non filtered messages are routed through the.otherwise()
piece of code. -
Click the Run button in the Run view or press F6 to execute your Route.
RESULT: The files containing “Paris” are sent to a folder named Paris_only, and the other messages are sent in a folder called Other_cities.