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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Automating Power Automate: How to Ensure Cloud Flows Are Active After Every Pipeline Deployment
  • How Power Automate Helps Analysts Send Alert Emails Faster and How AI Builder Takes It to the Next Level
  • 2 Hidden Bottlenecks in Large-Scale Azure Migrations
  • How to Create a Custom React Component in Vaadin Flow

Trending

  • TensorFlow vs PyTorch: The Real Difference Isn’t Accuracy
  • How I Built a Star Wars Grogu Product Research Agent With Codex, Lark, and SerpApi
  • Building Reliable Data Pipelines for Enterprise Analytics Using PySpark
  • Stop Writing If-Else Spaghetti: Architecting Cleaner Java with the Strategy Pattern

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.

By 
Ankit Lawaniya user avatar
Ankit Lawaniya
·
Feb. 02, 18 · Tutorial
Likes (10)
Comment
Save
Tweet
Share
29.3K 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.

Related

  • Automating Power Automate: How to Ensure Cloud Flows Are Active After Every Pipeline Deployment
  • How Power Automate Helps Analysts Send Alert Emails Faster and How AI Builder Takes It to the Next Level
  • 2 Hidden Bottlenecks in Large-Scale Azure Migrations
  • How to Create a Custom React Component in Vaadin Flow

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook