What is Mule ESB ?
Mule ESB is a lightweight Java-based enterprise service bus (ESB) and integration platform that allows developers to connect applications together quickly and easily, enabling them to exchange data. Mule ESB enables easy integration of existing systems, regardless of the different technologies that the applications use, including JMS, Web Services, JDBC, HTTP, and more.
This example shows how we can read a file from a source directory and append a string to the file content and display it into the console. We simply put a file in a source directory and Mule will read the file from the source directory. Using Mule ESB it’s very easy to read the file from a location. If we had to read the file using manual coding then we had to write many lines of code. But using Mule ESB we just simply put a file in a directory and let the Mule know the file path and Mule does the rest of the thing. You can put any kind of file to the source for reading.
What is Append-String transformer ?
Append-String transformer appends a string to a string payload.
Mule Studio 3.5.1
Knowledge of XML
Step 1. Open Mule Studio and create a mule project. Go to File->New->Mule Project. Now enter the Project Name, select Runtime – Mule Server, check the check box for Use Maven. Now enter the following information
Group Id: your company(ex. roytuts.com) name – in.webtuts
Artifact Id: project name – append-string(automatically populated when project name is getting typed)
Version: Mule studio generates default
Step 2. Click Next and verify project Location and JRE are correctly set. Click Finish.
Step 3. Now create a new flow or you can also use the existing default flow. To create a new flow, do right-click on src/main/app(Flows). Go to New->Mule Configuration File. Now enter information as below
File name: as you type the file name gets generated ending with .xml
Description: append a string to a string payload
Step 4. Now drag and drop the elements from right side of the Mule studio as shown below in the picture.
Endpoint – FILE, Transformer – File to String, Append String and Component – Logger
Step 5. Modify the elements properties as shown below. You can also do it using GUI editor. The full source XML is shown below
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.5.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd"> <flow name="append" doc:name="append"> <file:inbound-endpoint path="D:\mule_workspace\files\append-string" responseTimeout="10000" doc:name="File" /> <file:file-to-string-transformer doc:name="File to String" /> <append-string-transformer message="This is an appended string." doc:name="Append String" /> <logger message="#[message.payload]" level="INFO" doc:name="Logger" /> </flow> </mule>
The below line tells about the flow and everything should be wrapped inside this flow
<flow name="append-string" doc:name="append-string"> ... ... </flow>
The below line of code says that it is an inbound file endpoint which reads a file from a mentioned directory location as given in the path attribute and idle time-out is 10000 milliseconds.
<file:inbound-endpoint path="D:\Mule_Projects\files\append-string" responseTimeout="10000" doc:name="File" />
The file-to-string transformer converts the file object into String object
<file:file-to-string-transformer doc:name="File to String" />
The below line of code appends the message “This is an appended string.” to the string payload which is coming from file-to-string transformer
<append-string-transformer message="This is an appended string." doc:name="Append String" />
The logger component just logs or displays the payload into the console. We have used here Mule’s built-in EL expression #[message.payload] to display the file content.
<logger message="#[message.payload]" level="INFO" doc:name="Logger" />
Step 6. Now put any file at D:\Mule_Projects\files\append-string and run the append-string flow, you will see the output of Append String after file content in the console.
That’s all. Thanks for your reading.Tags: Append String • File Connector • File To String • Logger