Entity view (Content)

Mule Requester Module

By bfattouh
Feb. 24, 2014

Introduction

This post is intended to demonstrate how to use the mule requester module that was created to allow developers request resources at any point of a flow. The resources can be files, messages from VM, JMS, SMTP endpoints etc... Here in I will show how to use it to read a file from a specific location and upload it to an ftp server.

Installation

Before using this module, check the installation and the configuration here Ref[1].

Demo application

The demo mule application that shows how to use the mule requester can be cloned from git here Ref[2].

This application is comprised of the following configurations:

1. Global configurations:

<context:property-placeholder location="classpath:api.properties"/>
<mulerequester:config name="Mule_Requester" doc:name="Mule Requester"/>
<file:endpoint path="${local.file.folder}" connector-ref="fileToftpConnector"
doc:name="File To Sftp Endpoint" name="fileToftpEndpoint"/>
<file:filename-regex-filter pattern="${local.file.prefix}-([0-9]{8}).${local.file.suffix}"/>
<file:endpoint/>
<file:connector name="fileToftpConnector" streaming="true" doc:name="fileToftpConnector"
autoDelete="false"/>

This configuration shows:

1.1 The Mule Requester config that will enable reading a file from a local directory.

1.2 The file endpoint that use a reference file connector that allows reading a file from a specific location.

1.3 We will see later how this file endpoint will be referenced from the mule requester component using the endpoint name: fileToSftpEndpoint

2. Flow Configuration:

<flow name="ftp-flow-write" doc:name="ftp-flow-write" >
<mulerequester:request resource="fileToFtpEndpoint" config-ref="Mule_Requester" />
<logger message="Mule requester message read: #[message]" level="INFO" doc:name="Logger" />
<logger message="Start writing to ftp server: ${ftp.host} , port: ${ftp.port}
and location ${ftp.path}" level="INFO" doc:name="Logger" />
<ftp:outbound-endpoint host="${ftp.host}"
port="${ftp.port}"
user="${ftp.username}"
password="${ftp.password}"
path="${ftp.path}"
outputPattern="${local.file.prefix}-#[server.dateTime].${local.file.suffix}" doc:name="FTP" />
<logger message="End writing to ftp server" level="INFO" doc:name="Logger" />
</flow>

This flow shows how the mule requester will be invoked at the beginning of the process to read the file resource using the referenced mule requester namely: Mule_Requester and the resource as : fileToSftpEndpoint that is configured as illustrated previously within the global configurations. If there is a file that matches the regular expression pattern set on this global endpoint, then this will be read by the mule requester and passed to the ftp outbound endpoint.

The following is the unit test snippet code that runs the flow to read a file using mule requester and upload it into ftp server, this unit test runs only inside AnypointStudio. The unit test using mvn package command requires a licence certificate (.lic) for Mule Enterprise Edition installed in the classpath.

@Test public void testFlow() throws Exception {
MessageProcessor flow = muleContext.getRegistry().lookupObject("ftp-flow-write");
flow.process(getTestEvent(null));
MuleClient client = muleContext.getClient();
MuleMessage result =
client.request("ftp://yourFtpUser:yourFtpPassword@localhost/documents/ftp", RECEIVE_TIMEOUT);
assertNotNull(result);
assertNull(result.getExceptionPayload());
assertFalse(result.getPayload() instanceof NullPayload);
assertEquals(result.getPayloadAsString(), "\"test\",\"test\",\"test\",\"test\"");
}

Notes

1. In Macbook FTP can be started using the following command:

sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist

Replace load with unload to stop the server.

2. Assuming you have a FTP server up and running edit the file api.properties and change the FTP properties to the appropriate ones that match your local server.

3. Munit offers other ways to mock a FTP server and test FTP upload/download in a flow.

References

Ref[1]:  https://github.com/mulesoft/mule-module-requester

Ref[2]:  https://github.com/fattouh/mule-requester-demo 

Post Tags: