SlideShare a Scribd company logo
1 of 6
Download to read offline
Processing Files Sequentially in Mule
In this article I am going to show how we can process files in a sequence with File connector in Mule.
Problem Statement
A local file system folder contains text files where the filenames are padded with sequence numbers
such as file101.txt, file100.txt, and so on. We are supposed to append the contents of these files into a
target file according to the ascending order of the sequence numbers associated with the filenames.
Pre-requisites
 Anypoint Studio 6+
 Mule ESB 3.8
Solution
1. Create a Mule project with Anypoint Studio. In the article it was named sequential-file-
processing.
2. Create a folder named input in src/main/resources. The input folder contains the text files to be
processed in ascending order.
3. Create a folder named output in src/main/resources. The output folder contains the target file.
4. In order to process files in an order, we need to create a Java class implementing
java.util.Comparator interface. Create the Java class in src/main/java. In the article it was
named FilenameComparator. The purpose of this java class is to order the filenames in
ascending order of the sequence numbers associated with the filenames so that the File
inbound endpoint can read the files in that order.
package file;
import java.io.File;
import java.util.Comparator;
public class FilenameComparator implements Comparator<Object> {
@Override
public int compare(Object o1, Object o2) {
File file1 = (File) o1;
File file2 = (File) o2;
int index1 =
Integer.parseInt(file1.getName().substring(4,
file1.getName().indexOf(".")));
int index2 =
Integer.parseInt(file2.getName().substring(4,
file2.getName().indexOf(".")));
if (index1 == index2) {
return 0;
} else if (index1 > index2) {
return 1;
} else {
return -1;
}
}
}
5. Create a Global Mule Configuration Element for File connector and configure as per the
following figure.
6. Drag File endpoint from the palette and place it on the canvas and configure the properties as
per figures below.
7. Drag another File endpoint from the palette and place it in the process area of the flow and
configure it as per the following figure. The purpose of this endpoint is to append the contents
of the text files into the target file in ascending order.
8. Configure the flow’s Processing Strategy to synchronous.
Complete XML Code
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
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"
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">
<file:connector name="SequentialFile" autoDelete="true"
outputAppend="true" streaming="true" validateConnections="true"
doc:name="File"/>
<flow name="sequential-file-peocessingFlow"
processingStrategy="synchronous">
<file:inbound-endpoint path="src/main/resources/input" connector-
ref="SequentialFile" responseTimeout="10000"
comparator="file.FilenameComparator" doc:name="File">
<file:filename-regex-filter pattern="^.*.txt$"
caseSensitive="true"/>
</file:inbound-endpoint>
<file:outbound-endpoint path="src/main/resources/output"
outputPattern="output.txt" connector-ref="SequentialFile"
responseTimeout="10000" doc:name="File"/>
</flow>
</mule>
Result
9. Create the following line sequential text files in src/main/resources/input folder.
Filename File Content
File100.txt One Hundred
File90.txt Ninety
File102.txt One Hundred Two
File95.txt Ninety Five
10. Run the mule application and open the output.txt file from src/main/resources/output folder.
The file contents should be as per the figure below.
11. The result depicted in the above figure assures that the input files were processed in the
ascending order of the filename sequence numbers.

More Related Content

What's hot

Idempotent filter in mule
Idempotent filter in muleIdempotent filter in mule
Idempotent filter in muleMohammed246
 
Automatic documentation with mule
Automatic documentation with muleAutomatic documentation with mule
Automatic documentation with muleF K
 
Creating restful api using mule esb
Creating restful api using mule esbCreating restful api using mule esb
Creating restful api using mule esbRaviShankar Mishra
 
Idempotent filter in Mule
Idempotent filter in MuleIdempotent filter in Mule
Idempotent filter in MuleF K
 
Send email attachment using smtp in mule esb
Send email attachment using smtp in mule esbSend email attachment using smtp in mule esb
Send email attachment using smtp in mule esbPraneethchampion
 
Velocity in Mule
Velocity in MuleVelocity in Mule
Velocity in MuleMohammed246
 
Adding dynamic file
Adding dynamic fileAdding dynamic file
Adding dynamic fileSon Nguyen
 
Mule using Salesforce
Mule using SalesforceMule using Salesforce
Mule using SalesforceKhasim Cise
 
File component in mule
File component in muleFile component in mule
File component in muleRajkattamuri
 

What's hot (16)

Data weave
Data weaveData weave
Data weave
 
Mule maven
Mule mavenMule maven
Mule maven
 
Idempotent filter in mule
Idempotent filter in muleIdempotent filter in mule
Idempotent filter in mule
 
Setting filedynamically
Setting filedynamicallySetting filedynamically
Setting filedynamically
 
Automatic documentation with mule
Automatic documentation with muleAutomatic documentation with mule
Automatic documentation with mule
 
Creating restful api using mule esb
Creating restful api using mule esbCreating restful api using mule esb
Creating restful api using mule esb
 
Idempotent filter in Mule
Idempotent filter in MuleIdempotent filter in Mule
Idempotent filter in Mule
 
Send email attachment using smtp in mule esb
Send email attachment using smtp in mule esbSend email attachment using smtp in mule esb
Send email attachment using smtp in mule esb
 
Velocity in Mule
Velocity in MuleVelocity in Mule
Velocity in Mule
 
Email using mule
Email using muleEmail using mule
Email using mule
 
Mule
MuleMule
Mule
 
Mule data bases
Mule data basesMule data bases
Mule data bases
 
Adding dynamic file
Adding dynamic fileAdding dynamic file
Adding dynamic file
 
Mule using Salesforce
Mule using SalesforceMule using Salesforce
Mule using Salesforce
 
Mule velocity
Mule velocityMule velocity
Mule velocity
 
File component in mule
File component in muleFile component in mule
File component in mule
 

Viewers also liked (20)

Mule esb com framework cucumber part 2
Mule esb com framework cucumber part 2Mule esb com framework cucumber part 2
Mule esb com framework cucumber part 2
 
MyResume
MyResumeMyResume
MyResume
 
Sarpesh mishra resume
Sarpesh mishra resumeSarpesh mishra resume
Sarpesh mishra resume
 
Anypoint mule main 3.7
Anypoint mule main 3.7Anypoint mule main 3.7
Anypoint mule main 3.7
 
OAuth 2.0 authentication
OAuth 2.0 authentication OAuth 2.0 authentication
OAuth 2.0 authentication
 
Mule 2.2.1-users-guide
Mule 2.2.1-users-guideMule 2.2.1-users-guide
Mule 2.2.1-users-guide
 
A Workhorse Named Mule
A Workhorse Named MuleA Workhorse Named Mule
A Workhorse Named Mule
 
Mule data weave_6
Mule data weave_6Mule data weave_6
Mule data weave_6
 
Trans xml into_another_xml
Trans xml into_another_xmlTrans xml into_another_xml
Trans xml into_another_xml
 
Mule data weave_8
Mule data weave_8Mule data weave_8
Mule data weave_8
 
Mule data weave_3
Mule data weave_3Mule data weave_3
Mule data weave_3
 
Mule data weave_4
Mule data weave_4Mule data weave_4
Mule data weave_4
 
OAuth 2.0 authorization
OAuth 2.0 authorization OAuth 2.0 authorization
OAuth 2.0 authorization
 
Mule caching strategy with redis cache
Mule caching strategy with redis cacheMule caching strategy with redis cache
Mule caching strategy with redis cache
 
Passing java arrays in oracle stored procedure from mule esb flow
Passing java arrays in oracle stored procedure from mule esb flowPassing java arrays in oracle stored procedure from mule esb flow
Passing java arrays in oracle stored procedure from mule esb flow
 
Mule data weave_7
Mule data weave_7Mule data weave_7
Mule data weave_7
 
Mule data weave_2
Mule data weave_2Mule data weave_2
Mule data weave_2
 
RAML
RAMLRAML
RAML
 
Mule esb beginner’s guide
Mule esb beginner’s guideMule esb beginner’s guide
Mule esb beginner’s guide
 
Resume_Arundhati Ghosh
Resume_Arundhati GhoshResume_Arundhati Ghosh
Resume_Arundhati Ghosh
 

Similar to Process Files Sequentially with Mule File Connector

Description 1) Create a Lab2 folder for this project2.docx
Description       1)  Create a Lab2 folder for this project2.docxDescription       1)  Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docxtheodorelove43763
 
Java 7 Features and Enhancements
Java 7 Features and EnhancementsJava 7 Features and Enhancements
Java 7 Features and EnhancementsGagan Agrawal
 
There are 4 part for the project and the question may be long to rea.docx
There are 4 part for the project and the question may be long to rea.docxThere are 4 part for the project and the question may be long to rea.docx
There are 4 part for the project and the question may be long to rea.docxsusannr
 
There are 4 parts for the project. The question may be long to read .docx
There are 4 parts for the project. The question may be long to read .docxThere are 4 parts for the project. The question may be long to read .docx
There are 4 parts for the project. The question may be long to read .docxsusannr
 
There are 4 parts for the project. The question may be long to r.docx
There are 4 parts for the project. The question may be long to r.docxThere are 4 parts for the project. The question may be long to r.docx
There are 4 parts for the project. The question may be long to r.docxsusannr
 
File handling in C++
File handling in C++File handling in C++
File handling in C++Hitesh Kumar
 
Automate the boring stuff with python
Automate the boring stuff with pythonAutomate the boring stuff with python
Automate the boring stuff with pythonDEEPAKSINGHBIST1
 
R12 d49656 gc10-apps dba 11
R12 d49656 gc10-apps dba 11R12 d49656 gc10-apps dba 11
R12 d49656 gc10-apps dba 11zeesniper
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))Papu Kumar
 
Murach: How to use Entity Framework EF Core
Murach: How to use Entity Framework EF  CoreMurach: How to use Entity Framework EF  Core
Murach: How to use Entity Framework EF CoreMahmoudOHassouna
 

Similar to Process Files Sequentially with Mule File Connector (20)

Description 1) Create a Lab2 folder for this project2.docx
Description       1)  Create a Lab2 folder for this project2.docxDescription       1)  Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docx
 
Java 7 Features and Enhancements
Java 7 Features and EnhancementsJava 7 Features and Enhancements
Java 7 Features and Enhancements
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
There are 4 part for the project and the question may be long to rea.docx
There are 4 part for the project and the question may be long to rea.docxThere are 4 part for the project and the question may be long to rea.docx
There are 4 part for the project and the question may be long to rea.docx
 
There are 4 parts for the project. The question may be long to read .docx
There are 4 parts for the project. The question may be long to read .docxThere are 4 parts for the project. The question may be long to read .docx
There are 4 parts for the project. The question may be long to read .docx
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
There are 4 parts for the project. The question may be long to r.docx
There are 4 parts for the project. The question may be long to r.docxThere are 4 parts for the project. The question may be long to r.docx
There are 4 parts for the project. The question may be long to r.docx
 
Data file handling
Data file handlingData file handling
Data file handling
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
Automate the boring stuff with python
Automate the boring stuff with pythonAutomate the boring stuff with python
Automate the boring stuff with python
 
History
HistoryHistory
History
 
R12 d49656 gc10-apps dba 11
R12 d49656 gc10-apps dba 11R12 d49656 gc10-apps dba 11
R12 d49656 gc10-apps dba 11
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
Murach: How to use Entity Framework EF Core
Murach: How to use Entity Framework EF  CoreMurach: How to use Entity Framework EF  Core
Murach: How to use Entity Framework EF Core
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
My History
My HistoryMy History
My History
 
Files nts
Files ntsFiles nts
Files nts
 
FILE HANDLING.pptx
FILE HANDLING.pptxFILE HANDLING.pptx
FILE HANDLING.pptx
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Recently uploaded (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Process Files Sequentially with Mule File Connector

  • 1. Processing Files Sequentially in Mule In this article I am going to show how we can process files in a sequence with File connector in Mule. Problem Statement A local file system folder contains text files where the filenames are padded with sequence numbers such as file101.txt, file100.txt, and so on. We are supposed to append the contents of these files into a target file according to the ascending order of the sequence numbers associated with the filenames. Pre-requisites  Anypoint Studio 6+  Mule ESB 3.8 Solution 1. Create a Mule project with Anypoint Studio. In the article it was named sequential-file- processing. 2. Create a folder named input in src/main/resources. The input folder contains the text files to be processed in ascending order. 3. Create a folder named output in src/main/resources. The output folder contains the target file. 4. In order to process files in an order, we need to create a Java class implementing java.util.Comparator interface. Create the Java class in src/main/java. In the article it was named FilenameComparator. The purpose of this java class is to order the filenames in ascending order of the sequence numbers associated with the filenames so that the File inbound endpoint can read the files in that order. package file; import java.io.File; import java.util.Comparator; public class FilenameComparator implements Comparator<Object> { @Override public int compare(Object o1, Object o2) { File file1 = (File) o1; File file2 = (File) o2; int index1 = Integer.parseInt(file1.getName().substring(4, file1.getName().indexOf("."))); int index2 = Integer.parseInt(file2.getName().substring(4, file2.getName().indexOf("."))); if (index1 == index2) { return 0; } else if (index1 > index2) { return 1; } else { return -1; }
  • 2. } } 5. Create a Global Mule Configuration Element for File connector and configure as per the following figure. 6. Drag File endpoint from the palette and place it on the canvas and configure the properties as per figures below.
  • 3.
  • 4. 7. Drag another File endpoint from the palette and place it in the process area of the flow and configure it as per the following figure. The purpose of this endpoint is to append the contents of the text files into the target file in ascending order. 8. Configure the flow’s Processing Strategy to synchronous.
  • 5. Complete XML Code <?xml version="1.0" encoding="UTF-8"?> <mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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" 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"> <file:connector name="SequentialFile" autoDelete="true" outputAppend="true" streaming="true" validateConnections="true" doc:name="File"/> <flow name="sequential-file-peocessingFlow" processingStrategy="synchronous"> <file:inbound-endpoint path="src/main/resources/input" connector- ref="SequentialFile" responseTimeout="10000" comparator="file.FilenameComparator" doc:name="File"> <file:filename-regex-filter pattern="^.*.txt$" caseSensitive="true"/> </file:inbound-endpoint>
  • 6. <file:outbound-endpoint path="src/main/resources/output" outputPattern="output.txt" connector-ref="SequentialFile" responseTimeout="10000" doc:name="File"/> </flow> </mule> Result 9. Create the following line sequential text files in src/main/resources/input folder. Filename File Content File100.txt One Hundred File90.txt Ninety File102.txt One Hundred Two File95.txt Ninety Five 10. Run the mule application and open the output.txt file from src/main/resources/output folder. The file contents should be as per the figure below. 11. The result depicted in the above figure assures that the input files were processed in the ascending order of the filename sequence numbers.