FTP Connector With Mule ESB

The FTP Connector implements a file transport channel so that your Mule application can exchange files with an external FTP server.

You can configure FTP as an inbound endpoint (which receives files) or outbound endpoint (which writes files to the FTP server). The FTP transport allows integration of the File Transfer Protocol into Mule.

Mule can poll a remote FTP server directory, retrieve files. and process them as Mule messages. Messages can also be uploaded as files to a directory on a remote FTP server. Default behavior of the inbound FTP connector is it will pick the file and delete from source folder. Fore more additional info Mulesoft Certification

FTP Connector as Inbound:

Drag and drop the FTP connector to the canvas and place it at the message source.

Configure the FTP connector and provide the information like host, port, path, username, and password.

As a default, the behavior of the FTP connector is to read the file and delete it from the FTP server. There is an option available with the FTP connector that instead of deleting the file, you can move it to a backup folder on the FTP server.

FTP Connector as Outbound:

The FTP connector can be configured as Outbound to send the file to the destination. For the FTP connector to work as Outbound, place the FTP connector in the message processor region of the Mule flow.

Dynamic FTP Connections

Many integrations require connecting to different servers depending on a certain condition.

Examples of this include:

  • Connect to different invoice storage servers depending on the branch that emit an invoice.
  • Connect 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 expressions, which makes it possible for connection parameters to evaluate these conditions and connect to the correct server. For more additional skills Mulesoft Online Training

Dynamic FTP Connection Example

The following example shows a dynamic multicast application that:

  1. Defines a config for the connector in which the hostusername, and password are expressions.
  2. Describes a flow in which content is posted through HTTP.
  3. Uses the File connector to read a CSV file that contains a random set of FTP destinations with columns such as host, user, and port.
  4. Uses a <foreach> component to iterate over each of the lines in the CSV file.
  5. On each <foreach> iteration, resolves each expression in the connector config to a different value, establishing different connections to each of the servers.
<ftp:config name="FTP_Config" doc:name="FTP Config" >

<ftp:sftp-connection host="#[payload.host]" username="#[payload.user]" password="#[payload.password]" />

</ftp:config>

<flow name="streaming-multitenantFlow" >
<http:listener config-ref="HTTP_Listener_config" path="/multitenant"
doc:name="Listener" />
<set-variable variableName="content" value="#[payload]" doc:name="Variable" />
<file:read config-ref="File_Config" path="recipients.csv" doc:name="Read"
 outputMimeType="application/csv" />
<ee:transform doc:name="Transform Message">
<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 config-ref="FTP_Config" doc:name="Write" path="demo.txt">
    <ftp:content >#[content]</ftp:content>
  </ftp:write>
</foreach>
<set-payload doc:name="Set Payload" value="Multicast OK"/>
</flow>
  • This sample application defines an FTP config in which the host, username, and password are expressions.
  • It uses a flow in which a random content is posted.
  • It uses the file connector to load a recipients file, which is a CSV file containing a random set of FTP destinations.
  • There’s a DataWeave transformation that splits a CSV file.
  • The application uses a foreach element to write the contents into each of the FTP destinations.
  • On each foreach iteration, each of the expressions in the FTP config resolves to a different value, establishing different connections to each of the servers

To get in-depth knowledge, enroll for a live free demo on Mulesoft Training

Leave a comment

Design a site like this with WordPress.com
Get started