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 display the content into 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 File-To-String transformer ?
File-To-String element configures a transformer that reads the content of java.io.File into a java.lang.String.
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 3.4.x, check the check box Create POM file for project and maintain with Maven. Now enter the following information
Group Id: your company(ex. roytuts.com) name – in.webtuts
Artifact Id: project name – file-to-string
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 flows. Go to New->Mule Flow. Now enter information as below
File name: as you type the file name gets generated ending with .mflow
Description: Read content a file from and display in console
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 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="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="CE-3.4.0" 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"> <flow name="file-to-string" doc:name="file-to-string"> <file:inbound-endpoint path="D:\Mule_Projects\files\file-to-string" responseTimeout="10000" doc:name="File" /> <file:file-to-string-transformer doc:name="File to 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="file-to-string" doc:name="file-to-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 timeout is 10000 milliseconds.
<file:inbound-endpoint path="D:\Mule_Projects\files\file-to-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 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" />
What is message ?
Message is the data that passes through an application via one or more flows. Message contains two elements – header and payload.
Header – contains metadata about message
Payload – contains business specific data
Step 6. Now put any file at D:\Mule_Projects\files\file-to-string and run the file-to-string flow, you will see the output of file content in the console.
Thanks for your reading.Tags: file • Log • String