Code Snippets

Guide to Mule Requester Module

This is a guide to installing and using the Mule Requester Module. For further information, refer to this blog post.

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\"");
}

Resource Tags: