File-To-String transformer in Mule ESB

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.

Prerequisites

Mule Studio
JDK 1.6
Maven 3
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

Name: file-to-string
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

file-to-string transformer in Mule ESB

Step 5. Modify the elements properties as shown below. You can also do it using GUI editor. The full source XML is shown below

 

The below line tells about the flow and everything should be wrapped inside this 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.

 

The file-to-string transformer converts the file object into String object

 

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.

 

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.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.