Connect to FTP Servers Dynamically Example - Mule 4
Many integrations require connecting to different servers based on a specific condition, such as:
-
Connecting to different invoice storage servers depending on the branch that sends an invoice.
-
Connecting to different servers depending on an integration subject, such as in a multi-tenant use case.
To accommodate these use cases, the config
element supports parameter expressions that evaluate these conditions and connect to the correct server.
The following example shows a dynamic multicast application that:
-
Defines a configuration for the connector in which the
host
,username
, andpassword
are expressions. -
Describes a flow, triggered by an HTTP Listener, in which content is posted through HTTP.
-
Uses the File Connector Read operation to load a
recipients.csv
file that contains a set of FTP destinations, with columns such ashost
,user
, andport
. -
Uses a For Each component and an FTP Write operation to write the contents into each of the FTP destinations. On each For Each iteration, the expressions set in the FTP global configuration element resolve to a different value and establish different connections to each of the servers.
To test this example, create the Mule application and run it using curl commands:
Create the Mule Application
To create the Mule application flow:
-
In the Mule Palette view, select HTTP > Listener.
-
Drag HTTP Listener to the Studio canvas.
-
On the HTTP Listener configuration screen, optionally change the value of the Display Name field.
-
Set Path to
/multitenant
. -
Click the plus sign (+) next to the Connector configuration field to configure a global element that can be used by all instances of HTTP Listener in the app.
-
On the General tab, set the following fields:
-
Host:
All Interfaces [0.0.0.0] (default)
-
Port:
8081
-
-
Click OK.
-
Drag a Set Variable component to the right of the HTTP Listener source.
-
Set Name to
content
and Value to#[payload]
. -
Drag the File Connector Read operation to the right of the Set Variable component.
-
Set File Path to
recipients.csv
. This reads a CSV file that contains a random set of FTP destinations with columns such ashost
,user
, andport
. -
Click the plus sign (+) next to the Connector configuration field to configure a global element that can be used by all instances of File Connector in the app.
-
Accept the default settings and click OK.
-
On the MIME Type tab of the Read operation, for MIME Type, select application/csv.
-
In Studio, drag a Transform Message component to the right of the Read operation.
-
In the Output section of the component, add the following DataWeave code to map the columns of the CSV file:
DataWeave script:%dw 2.0 output application/java --- payload map using (row = $) { host: row.Host, user: row.User, password: row.Password}
-
Drag a For Each component to the right of the Transform Message component.
-
Drag an FTP Write operation into the For Each component.
On each For Each iteration, the Write operation writes the contents into each of the FTP destinations. -
Click the plus sign (+) next to the Connector configuration field to configure a global element that can be used by all instances of FTP Write operation in the app, and set the following fields:
-
Host:
#[payload.host]
-
Username:
#[payload.username]
-
Password:
#[payload.password]
-
-
Click OK.
-
Set Path to
demo.txt
. -
Set the Content field to
payload
. -
Drag a Set Payload component to the right of the For Each component.
-
Set the Value field to
Multicast Ok
.
Run and Test Your Mule Application
After creating your Mule application, run and test it:
-
In Studio, save your Mule app.
-
Click the project name in Package Explorer and then click Run > Run As > Mule Application.
-
Open a browser and type
http://0.0.0.0:8081/multitenant
.
Note about Mule 4 behavior for Mule 3 users:
-
Although the information posted through the HTTP Listener source is written to each FTP site multiple times, the repeatable streams feature in Mule 4 prevents the overhead of consuming the stream multiple times.
-
In Mule 4, the For Each component automatically goes through each line of the CSV file without needing to first transform it into a Java structure.
In Mule 3, you first needed to transform the CSV file into a Java structure, but because Mule 4 is now Java agnostic, this works out-of-the-box.
When reading or listing files, you might be interested in the file’s metadata (for example, the file name, full path, size, timestamp). The connector uses the Mule message attributes to access this information.
XML for Connecting to FTP Servers Dynamically
Paste this code into the Configuration XML tab in your Studio canvas to quickly load the flow for this example into your Mule app:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:sftp="http://www.mulesoft.org/schema/mule/sftp"
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:ftp="http://www.mulesoft.org/schema/mule/ftp"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ftp http://www.mulesoft.org/schema/mule/ftp/current/mule-ftp.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.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
http://www.mulesoft.org/schema/mule/sftp http://www.mulesoft.org/schema/mule/sftp/current/mule-sftp.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<file:config name="File_Config" doc:name="File Config" doc:id="" />
<ftp:config name="FTP_Config" doc:name="FTP Config" doc:id="" >
<ftp:connection host="#[payload.host]" username="#[payload.username]" password="#[payload.password]" />
</ftp:config>
<flow name="streaming-multitenantFlow" >
<http:listener path="/multitenant"
doc:name="Listener" config-ref="HTTP_Listener_config"/>
<set-variable variableName="content" value="#[payload]" doc:name="Variable" />
<file:read path="recipients.csv" doc:name="Read"
outputMimeType="application/csv" config-ref="File_Config"/>
<ee:transform doc:name="Transform Message" doc:id="" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
payload map using (row = $) {
host: row.Host,
user: row.User,
password: row.Password
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<foreach doc:name="For Each" >
<ftp:write doc:name="Write" doc:id="" path="recipients.csv" config-ref="FTP_Config"/>
</foreach>
<set-payload doc:name="Set Payload" value="Multicast OK"/>
</flow>
</mule>