Showing posts with label EI. Show all posts
Showing posts with label EI. Show all posts

Tuesday, 18 October 2016

Mule ESB/Java : Reading a CSV/Delimiter file and process line by line

Enterprise Integration : Mule ESB 

Reading Pipe/CSV Files : Process individual lines and display in logger - the each line

Mule Flow Diagram :


Mule Config :

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc" 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"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
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">

<configuration doc:name="Configuration">
     <expression-language autoResolveVariables="true">
         <import class="org.mule.util.StringUtils" />
     </expression-language>
</configuration>

    <flow name="ImportCSVFile" >
     <!-- Component 1 : Folder to read CSV file source -->
<file:inbound-endpoint path="C:\InputFileFolderLocation\FileRead" pollingFrequency="5000" doc:name="Source" responseTimeout="10000"/>

<!-- Component 2: Convert between object arrays and strings -->
<object-to-string-transformer doc:name="Object to String"/>

<!-- Component 3: Split each row -->
<splitter expression="#[StringUtils.split(message.payload, '\n\r')]" doc:name="Splitter"/>
        <component class="RequestProcessor1" doc:name="Java"/>    
   
<!-- Component 4:  Paylod - display in logger -->
   <logger message=" #[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
               <!-- Component 5: Transform CSV(",")/Pipe("|") row in array and display in logger -->
              <expression-transformer expression="#[StringUtils.split(message.payload, '|')]" doc:name="Expression"/> 
 
</flow>
   </mule>

Mule Java Component Class and Code:

RequestProcessor1.java(for Java Component):

import java.util.StringTokenizer;

import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;

public class RequestProcessor1 implements Callable {

 @Override
public Object onCall(MuleEventContext eventContext) throws Exception {

String[] PayloadStr = eventContext.getMessage().getPayloadAsString().split("\\|", -1);
 Further Processing of PayloadStr ...

}
}...

So, as shown above, we can use the splitter /expression-tranform or splitter/custom java code to read file and process it line by line in Mule ESB.