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

Mule Reading Files in the Middle of the Flow Using Mule-Requester

If you're a Mule fan, this article's for you. Learn how to do a bit of Mule trickery using the MuleRequester class, and make an app that can read files during flow.

Ankit Lawaniya user avatar by
Ankit Lawaniya
CORE ·
Feb. 02, 18 · Tutorial
Like (10)
Save
Tweet
Share
27.85K Views

Join the DZone community and get the full member experience.

Join For Free

Mule supports reading files at the beginning of a flow and we can use the File Connector to do this as well, but this connector is not suitable for loading the content of a file in the middle of a flow. Sometimes, though, you need to read a file during a flow. For example:

  • Your flow is triggered by a job scheduler (e.g. Quartz), not a file polling inbound endpoint.
  • You have to process files on a strict schedule, not as soon as they arrive.
  • You have to read the file name from the DB and then you have to start processing.
  • You need to retry file processing from the beginning of the file.

To achieve this goal we need to use the mule-module-requester. This module allows the request of different resources at any point in a flow.

What Is Mule MessageRequester?

Mule MessageRequester retrieves an external event when requested by Mule and converts it into a Mule message. We can run this event anywhere in between the flow.

The Mule Requester Connector is a thin wrapper over Mule’s Java Client API. The client allows you to request org.mule.api.MuleEvent objects from endpoints using their URL syntax. This invokes the Mule endpoint’s MessageRequester class (as discussed above). This allows you to get data in the middle of your flow from endpoints that can usually only start a flow (like a file receiver).

Reading a file in the middle of a flow - Mule-requester - Ricston Ltd

The intention of this post is to describe, step-by-step, how to install and use the Mule module requester. We will see a working example of this as well.

Step 1: Download the Anypoint Studio plugin from the link.

Step 2: Install this plugin in the Anypoint studio.

Reading a file in the middle of a flow - Mule-requester - Ricston Ltd

--> To install it, click on “Help” Menu >> “Install New Software.”

Reading a file in the middle of a flow - Mule-requester - Ricston Lt

--> Click on the button “Add..”, use the option “Archive…” and search for the zip file we just downloaded.

Step 3: Set up your project -Add the below plugin and dependencies in POM.xml

<plugin>
    <groupId>org.mule.tools.maven</groupId>
    <artifactId>mule-app-maven-plugin</artifactId>
    <version>${mule.tools.version}</version>
    <extensions>true</extensions>
    <configuration>
        <copyToAppsDirectory>true</copyToAppsDirectory>
        <inclusions>
            <inclusion>
                <groupId>org.mule.modules</groupId>
                <artifactId>mule-module-requester</artifactId>
            </inclusion>
        </inclusions>
    </configuration>
</plugin>
<dependency>
    <groupId>org.mule.modules</groupId>
    <artifactId>mule-module-requester</artifactId>
    <version>1.5</version>
</dependency>

Step 4: Now your application is ready to start using Mule-Requester. We can build the flow as per our requirements.

In the example, we are reading a file in the middle of the flow and the response is going back to the HTTP client. We are using a REST client to trigger the flow.

Flow:

Image title

Code:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:mulerequester="http://www.mulesoft.org/schema/mule/mulerequester" 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.mulesoft.org/schema/mule/mulerequester http://www.mulesoft.org/schema/mule/mulerequester/current/mule-mulerequester.xsd 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 http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <file:connector name="file-connector-config" autoDelete="false" streaming="true" validateConnections="true" doc:name="File" />
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" basePath="/requester" doc:name="HTTP Listener Configuration" />
    <flow name="muleRequester">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/requester" doc:name="HTTP" />
        <logger message="Invoking Mule Requester" level="INFO" doc:name="Logger" />
        <mulerequester:request resource="file://src/main/resources/in/ReadME.txt?connector=file-connector-config" doc:name="Retrieve File" returnClass="java.lang.String" />
        <logger message="Payload after file requester #[payload]" level="INFO" doc:name="Logger" />
    </flow>
</mule>

Request & Response:

http://localhost:8081/requester/requester

Image title

Reading a file in the middle of a flow - Mule-requester - Ricston Ltd

Hope this helps, thanks!

Keep learning!

Reading a file in the middle of a flow - Mule-requester - Ricston Ltd
Flow (web browser)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Unlocking the Power of Polymorphism in JavaScript: A Deep Dive
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • Why Every Fintech Company Needs DevOps
  • Key Considerations When Implementing Virtual Kubernetes Clusters

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: