Contact Us 1-800-596-4880

Trigger a Flow When a New File Is Created or Modified Example

Anypoint Connectors for File, FTP, and SFTP provide an On New or Updated File operation that polls a directory for files that have been created or updated. A message is generated for each file that is found.

The following strategies determine that a file is new:

  • Set the Auto delete field to True to delete each file after it is processed, which causes all files obtained in the next poll to be considered new.

  • Set the Move to directory field to move each file to a different directory after it is processed, which achieves the same effect as Auto delete but without erasing the file.

  • Set the Watermark field to pick only files that have been created or updated after the last poll was executed.

You can also use a matcher for additional filtering of files.

To add and configure the On New or Updated File operation in Studio, follow these steps:

  1. In the Mule Palette view, search for ftp and select the On New or Updated File operation.

  2. Drag the On New or Updated File operation onto the Studio canvas.

  3. In the General tab of the operation configuration screen, click the plus sign (+) next to the Connector configuration field to access the global element configuration fields.

  4. Specify the connection information and click OK.

  5. Set Directory to the directory path on which pooled files are contained, for example, test-data/in.

  6. Set Auto delete to True.

  7. Select the Watermark field to enable watermarking.

  8. Optionally, on the Advanced tab, set Time Between Size Check and Time Between Size Check Time Unit to wait until a file is completely written before it is processed.

In a cluster, including CloudHub 2.0, the source polls only on the primary node by default. In FTP Connector 2.x, every worker polled the directory. To poll on every node, set Primary Node Only (primaryNodeOnly) to false.

When a matcher rejects a file, the source skips that file and continues with the rest of the poll. The source returns the polling connection before it dispatches files. Each file stream and each post action (Auto delete, Move to directory, and Rename to) uses its own connection. If the directory doesn’t exist when the source starts, the source fails with FTP:ILLEGAL_PATH.

On New or Updated File operation configuration in Studio

In the Configuration XML editor, the <ftp:listener> configuration looks like this:

<flow name="onNewFile">
    <ftp:listener config-ref="file" directory="test-data/in" autoDelete="true">
      <scheduling-strategy>
        <fixed-frequency frequency="1000"/>
      </scheduling-strategy>
    </ftp:listener>

    <flow-ref name="processFile" />
</flow>