Modify input file name while moving to another directory using Mule ESB

In this tutorial I am going to show you how we can move files from one directory to another directory in Mule ESB. We will use File Connector to take an Excel file as input  and this Excel file will be moved to another directory but with modified name.

You can see also File transfer from source directory to destination directory in Mule ESB

Prerequisites

Mule Studio 3.x(Anypoint Studio) (Download from https://www.mulesoft.com/platform/studio)
Maven 3.2.1 (Download from https://maven.apache.org/download.cgi?Preferred=ftp://mirror.reverse.net/pub/apache/)
JDK 1.7 (Download from http://www.oracle.com/technetwork/java/javase/downloads/index.html)
Configure JDK, Maven and Mule Studio

Step 1. First install JDK
Step 2. Add the Java_Home/bin directory to your system’s PATH.
Step 3. After downloading Maven, extract it to a drive
Step 4. Add the M2_Home/bin directory to your system’s PATH.
Step 5. Download and extract Mule Studio to a drive
Step 6. Now start Mule Studio by clicking on AnypointStudio exe icon in the folder <physical drive>/AnypointStudio
Step 7. Once started, close the startup page
Step 8. In Mule Studio, go to Window -> Preferences. Expand Java, then click on Installed JREs. Add JDK 1.7 and select it. In expanded Java, click on Compiler and select the compiler level as 1.7
Step 9. Now expand Anypoint Studio and click on Maven Settings. Then select appropriate Maven installation home directory using Browse button.
Step 10. If you want you can input Default groupId for new projects, it will save your time every time when you want to create a new project.

Create Mule project in Mule Studio
Now we will see how to create a new project in Mule Studio(Anypoint Studio).

Step 1. In Anypoint Studio, go to File -> New -> Mule Project
Step 2. Input Project Name: mule, Runtime is by default selected, tick on Use Maven; here the artifactId is automatically picked up from the Project Name:, the Group Id is picked up from the Default groupId for new projects and version is also a default value.
Step 3. Click Next and verify the JDK, mainly select Use default JRE(currently ‘jdk1.7.0_x’)
Step 4. Click on Next and click on Finish.

Example

The below example modifies the file name while moving from one directory to another directory in the following pattern

ExcelReport-dd-MM-yy_HH-mm-ss.SSS-<original file name>

The below screen-shot shows the graphical representation of the Mule flow

Step 1. Create the copy-file-data.xml file under src/main/app directory and put the below file connector. While you create the xml file you will see on red mark on each file connector. Do not worry, red mark will be disappeared once you modify the xml file as given in Step 3.


mule move file

Step 2. Open the copy-file-data.xml file and click on Configuration XML view in the Editor
Step 3. Modify the copy-file-data.xml file as shown below

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
	xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.5.1"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
	<flow name="copy-file-data-flow" doc:name="copy-file-data-flow">
		<file:inbound-endpoint path="D:mule_workspaceinput_files"
			responseTimeout="10000" doc:name="File" pollingFrequency="10000">
			<file:filename-regex-filter pattern=".*xlsx$"
				caseSensitive="false" />
		</file:inbound-endpoint>
		<file:outbound-endpoint path="D:mule_workspaceoutput_files"
			outputPattern="ExcelReport-#[org.mule.util.DateUtils.getTimeStamp('dd-MM-yy_HH-mm-ss.SSS')]-#[message.inboundProperties.originalFilename]"
			responseTimeout="10000" doc:name="File" />
	</flow>
</mule>

In the above configuration we a Mule flow named as copy-file-data-flow and inside this flow there are two file connectors – one acting as inboud-endpoint and another one acting as outbound-endpoint.

inbound-endpoint takes a file as an input from the given “path”. It also specifies the timeout for a response if making a synchronous endpoint call. It also polling the directory every 10000 milliseconds to check if any file is there in the given “path”. We have used pattern in <file:filename-regex-filter/> to filter only files which we are interested in. caseSensitive=”false” means, it will ignore the file name case sensitivity while comparing with the given pattern.

outbound-endpoint will put the file which is coming from inbound-endpoint with the modification of the input file name in the below pattern

ExcelReport-dd-MM-yy_HH-mm-ss.SSS-<original file name>

For example, if the input file name is data.xlsx then the output file name will be ExcelReport-24-09-16_08-52-40.243-data.xlsx

Running the application

Now do a right-click on the copy-file-data.xml file and click on Run As -> Mule Application. Then you will see something like below in Console when the application runs

INFO  2016-09-23 22:06:33,317 [main] org.mule.DefaultMuleContext:
**********************************************************************
* Application: mule                                                  *
* OS encoding: Cp1252, Mule encoding: UTF-8                          *
*                                                                    *
* Agents Running:                                                    *
*   DevKit Extension Information                                     *
*   Batch module default engine                                      *
*   Clustering Agent                                                 *
*   JMX Agent                                                        *
**********************************************************************
INFO  2016-09-23 22:06:33,317 [main] org.mule.module.launcher.MuleDeploymentService:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Started app 'mule'                                       +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
INFO  2016-09-23 22:06:33,320 [main] org.mule.module.launcher.DeploymentDirectoryWatcher:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Mule is up and kicking (every 5000ms)                    +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Once the application is up and running put the data.xlsx file under D:\mule_workspace\input_files, you will receive the file as ExcelReport-24-09-16_08-52-40.243-data.xlsx in directory D:\mule_workspace\output_files

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *