cSetHeader
for subsequent message processing.
cSetHeader Standard properties
These properties are used to configure cSetHeader running in the Standard Job framework.
The Standard
cSetHeader component belongs to the Core family.
Basic settings
Headers |
Click [+] to add as many headers |
Name: Type in a name for the The header name CorrelationID is reserved. |
|
Language: Select the language of the expression you Select Bean if you want to call a Select CorrelationID to |
|
Value: Type in the expression to If CorrelationID is |
|
Add Namespaces |
Select this check box to add namespaces for the expression. Click |
Usage
Usage rule |
cSetHeader is used as a middle |
Limitation |
n/a |
Scenario: Splitting a message and renaming the sub-messages according to contained
information
This scenario applies only to a Talend solution with ESB.
In this scenario, a file message containing people information is split into
sub-messages. Each sub-messages is renamed according the city name it contains, and then
routed to another endpoint.
The following is the example XML file used in this use case:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<people> <person> <firstName>Pierre</firstName> <lastName>Dubois</lastName> <city>Paris</city> </person> <person> <firstName>Nicolas</firstName> <lastName>Yang</lastName> <city>Beijing</city> </person> <person> <firstName>Ellen</firstName> <lastName>Ripley</lastName> <city>Washington</city> </person> </people> |
A predefined Java Bean, setFileNames, is called by the cSetHeader component used in this use case to define a file
name for each message according to the city name it contains. 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 |
package beans; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class setFileNames { public String getCityName(Document document) { NodeList cities = document.getDocumentElement().getElementsByTagName( "city"); Element city = (Element) cities.item(0); String textContent = city.getTextContent(); return textContent+".xml"; } } |
Dropping and linking the components
This use case uses two cFile components, one as
the message sender and the other as the receiver, a cSplitter component to split the source message into sub-messages, a
cSetHeader component to rename each
sub-message, and a cProcessor component to display
the file name of each message routed to the receiver.
- From the Palette, expand the Connectivity folder, and drop two cFile components onto the design workspace.
-
From the Routing folder, drop a cSplitter component onto the design workspace,
between the two cFile components. -
From the Core folder, drop a cSetHeader component onto the design workspace, between the
cSplitter component and the receiving
cFile component. -
Right-click the first cFile component,
select Row > Route from the contextual menu and click the cSplitter component. -
Right-click the cSplitter component,
select Row > Split from the contextual menu and click the cSetHeader component. -
Right-click the cSetHeader component,
select Row > Route from the contextual menu and click the second
cFile component. -
Right-click the second cFile component,
select Row > Route from the contextual menu and click the cProcessor component. -
Label the components to better identify their roles in the Route, as shown
above.
Configuring the components and connections
-
Double-click the cFile component labeled
Sender to display its Basic settings view in the Component tab. -
In the Path field, fill in or browse to
the path to the folder that holds the source files.From the Encoding list, select the
encoding type of your source files.In the FileName field, type in the file
name of the source message. You can skip this step if the source folder
contains only one file. -
Repeat steps 1 and 2 above to define the output file path and encoding
type in the Basic settings view of the
other cFile component, which is labeled
Receiver. Leave the FileName field blank. -
Double-click the cSplitter component to
display its Basic settings view in the
Component tab.In this use case, as we want to split the message into sub-messages at
each person node of the XML file. Select XPath in the Language list and type in"/people/person")
in
the Expression field. -
Double-click the cSetHeader component,
which is labeled Set_file_name to display
its Basic settings view in the Component tab. -
Click [+] to add a row to the Headers table.
In the Name field, type in the name of
the header you want to give to the messages.Here, as we want to define the file name for each incoming message, fill
in"CamelFileName"
as the header name.Select Bean in the Language field and type in the name of the predefined Java
Bean in the Value field,
beans.setFileNames.class
in this use case. -
Double-click the cProcessor component to
display its Basic settings view in the
Component tab, and customize the code
so that the console will display information the way you wish.In this use case, we want to display the file name each message routed to
the receiving endpoint, so we customize the code as follows:12System.out.println("File received: "+exchange.getIn().getHeader("CamelFileName")); - 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, a message route is builtfrom
one
endpoint.to
another, and while in routing, the source message
is split according to the conditionxpath("/people/person")
by
cSplitter_1
, and each sub-message is given a header named
CamelFileName
, the value of which is returned by
.method(beans.setFileNames.class)
. -
Click the Run view to display it and
click the Run button to launch the
execution of your Route. You can also press F6 to execute it.RESULT: The source file message is split into sub-messages and each
sub-message is renamed after the city name it contains and routed to the
receiving endpoint.