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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • AI Agents For Automated Claims Processing
  • SmartXML: An Alternative to XPath for Complex XML Files
  • CubeFS: High-Performance Storage for Cloud-Native Apps
  • Efficient Asynchronous Processing Using CyclicBarrier and CompletableFuture in Java

Trending

  • AI’s Role in Everyday Development
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • How to Configure and Customize the Go SDK for Azure Cosmos DB

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.

By 
Priyobroto Ghosh user avatar
Priyobroto Ghosh
·
Sep. 14, 16 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
12.1K 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.

Related

  • AI Agents For Automated Claims Processing
  • SmartXML: An Alternative to XPath for Complex XML Files
  • CubeFS: High-Performance Storage for Cloud-Native Apps
  • Efficient Asynchronous Processing Using CyclicBarrier and CompletableFuture in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!