Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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.

· Integration Zone ·
Free Resource

The Future of Enterprise Integration: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

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

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.

Topics:
mule ,mule flow ,integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}