Mule Tutorial Series: File Endpoint, CSV to Maps, Java Component

My previous blog post covered HTTP end point. This post provides a tutorial using File endpoint.

For our demonstration, I have created the following Mule flow. Its graphical depiction is shown below:

File-CSV2Maps

The flow contains the following Mule components:

  • File Endpoint
  • Logger
  • CSV to Maps
  • Java

The configuration details for each of the Mule flow component is explained below. Note the configuration settings are explained assuming the use of Mule Studio IDE, in case you do not have the luxury of Mule Studio, refer to the Mule Flow XML source code for understanding and implementation. Please remember to retain default settings for properties not mentioned in the instructions below.

The first component of the flow is File Endpoint. The File endpoint is configured to pick up files from specific location and on completion of processing move it to a pre-defined location. The developer is free to choose location as per his/her convenience.

For our demonstration, in the General tab, configure the following properties with specified values:
Path – C:\MuleStudio\File\Inbound
Move to Directory – C:\MuleStudio\File\Outbound

The next component is logger. The message property value of the logger component defines the logging statement. In the General tab, configure the following.

Message – #[string: output #[message]]

The message object provides developer with insights regarding message’s meta-data.

The contents of the file read by the File endpoint component is passed to the CSV to Maps component for conversion to Map data structure. Each line in the CSV file is converted into an individual Map instance. The individual Map instances are added to a Collection and passed on to the next message processor in the flow chain. The component uses FlatPack project to do file parsing. The parsed file definition is provided via the mapping file property. The file needs to be present in the application classpath. To ensure availability, the person.xml is added in the src/main/java folder.

In the General tab of the CSV to Maps component configure the following properties with specific values.

Delimiter – ,
Mapping file – person.xml

The source code of the person.xml is as shown below

<?xml version='1.0'?>
<!-- DTD can be pulled from the Jar or over the web-->
<!DOCTYPE PZMAP SYSTEM	"flatpack.dtd" >
<!--<!DOCTYPE PZMAP SYSTEM	"http://flatpack.sourceforge.net/flatpack.dtd" >-->
<PZMAP>
	<COLUMN name="FIRSTNAME" />
	<COLUMN name="LASTNAME" />
	<COLUMN name="DOB" />
</PZMAP>

The last component in the flow is the Java component. Its configuration is as below. In the Java component’s General tab configure the following property with specific value.

Class Name – com.vinraj.integration.file.CSVProcessor

The source code of the CSVProcessor is shown below.

package com.vinraj.integration.file;

import java.util.List;
import java.util.Map;

public class CSVProcessor {
		
	public void processFile(List<Map<String, String>> maps) {
		System.out.println(maps);
		System.out.println("Size: " + maps.size());
	}
}

For testing purposes, the file persons.csv is dropped in the directory mentioned in the Path property. The contents of the file are shown below:

Bill,Kent,11/02/1977
Mark,Tully,1/12/1970
Daisy,Smith,1/12/1982

Running the flow displays the following in the console

INFO  2013-12-24 20:06:24,168 [[integration].connector.file.mule.default.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file: C:\MuleStudio\File\Inbound\persons.csv
INFO  2013-12-24 20:06:24,398 [[integration].file_processingFlow1.stage1.02] org.mule.api.processor.LoggerMessageProcessor:  output 
org.mule.DefaultMuleMessage
{
  id=c389a459-6ca8-11e3-8338-3d1791049619
  payload=org.mule.transport.file.ReceiverFileInputStream
  correlationId=<not set>
  correlationGroup=-1
  correlationSeq=-1
  encoding=UTF-8
  exceptionPayload=<not set>

Message properties:
  INVOCATION scoped properties:
    moveToDirectory=C:\MuleStudio\File\Outbound
  INBOUND scoped properties:
    MULE_ORIGINATING_ENDPOINT=endpoint..C.MuleStudio.File.Inbound
    directory=C:\MuleStudio\File\Inbound
    fileSize=65
    originalFilename=persons.csv
    timestamp=1387794342917
  OUTBOUND scoped properties:
    MULE_ENCODING=UTF-8
    directory=C:\MuleStudio\File\Inbound
    fileSize=65
    filename=persons.csv
    originalFilename=persons.csv
  SESSION scoped properties:
}
[{FIRSTNAME=Bill, DOB=11/02/1977, LASTNAME=Kent}, {FIRSTNAME=Mark, DOB=1/12/1970, LASTNAME=Tully}, {FIRSTNAME=Daisy, DOB=1/12/1982, LASTNAME=Smith}]
Size: 3

The lines between 2 and 29 is the logger output. The logger statement prints the message object’s properties. The content of the message provides considerable endpoint contextual information. This can be used during message processing. The lines 30 and 31 is the output of the Java component.

The source code of the flow is shown below:

<mule>
    <flow name="file_processingFlow1" doc:name="file_processingFlow1">
        <file:inbound-endpoint path="C:\MuleStudio\File\Inbound" moveToDirectory="C:\MuleStudio\File\Outbound" responseTimeout="10000" doc:name="File"/>
        <logger message="#[string: output #[message]]" level="INFO" doc:name="Logger"/>
        <jdbc-ee:csv-to-maps-transformer delimiter="," mappingFile="person.xml" doc:name="CSV to Maps"/>
        <component class="com.vinraj.integration.file.CSVProcessor" doc:name="Java"/>
    </flow>
</mule>

Note: the XML namespaces added within the mule element tag has been removed for code legibility.

That covers the File Endpoint and CSV to Maps component.

Advertisements

5 thoughts on “Mule Tutorial Series: File Endpoint, CSV to Maps, Java Component

  1. Hi vijay,

    I am a Oralce soa developer, i am new to mulesoft, can u plz advise me how to learn and any documentation.

    can you please explain diffidence bitten mule soft with oracle fusion middle ware.

    according to mule soft tutorials i am working on file adapter, my use case is read file from one location and write into another location can you plz explain how can i approach this use case in mulesoft.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s