DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Processing Files Sequentially in Mule

This easy-to-follow tutorial will get your files processed in the order you want them. All it takes is Mule ESB and a bit of Java.

Priyobroto Ghosh user avatar by
Priyobroto Ghosh
·
Sep. 14, 16 · Tutorial
Like (3)
Save
Tweet
Share
11.37K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Create a Mule project with Anypoint Studio. Here, it was named sequential-file-processing.
  • Create a folder named input in src/main/resources. The input folder contains the text files to be processed in ascending order.
  • Create a folder named output in src/main/resources. The output folder contains the target file.
  • 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;
        }
    }
}
  • Create a Global Mule Configuration Element  for File  connector and configure as per the following figure.

Image title

  • Drag the File endpoint from the palette and place it on the canvas and configure the properties as per figures below.

Image title

Image title

  • 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.

Image title

  • Configure the flow’s Processing Strategy to synchronous.

Image title

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

Create the following line sequential text files in the src/main/resources/input  folder.

Filename

File Content

File100.txt

One Hundred

File90.txt

Ninety

File102.txt

One Hundred Two

File95.txt

Ninety Five

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.

Image title

The result depicted in the above figure assures that the input files were processed in the ascending order of the filename sequence numbers.

File system Processing

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • gRPC on the Client Side
  • Fargate vs. Lambda: The Battle of the Future
  • Authenticate With OpenID Connect and Apache APISIX
  • [DZone Survey] Share Your Expertise and Take our 2023 Web, Mobile, and Low-Code Apps Survey

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: