Mule VM Transport with File Connector Example

This tutorial will show you how to use VM Transport with File Connector in Mule based application.
You can see the same tutorial in youtube video https://youtu.be/OQUqruRJbrU
You may like the other tutorial Mule VM Transport with HTTP Example
What is VM Transport

The VM transport is nothing but an in-memory transport. Therefore when a message has to be passed through a VM endpoint, i.e., the message is being transferred from one endpoint to another endpoint in memory. So the VM transport can be used for intra-JVM communication between Mule flows. It can be used only in single Mule application and cannot be used to communicate between two or more Mule applications. This transport by default uses in-memory queues but can optionally be configured to use persistent queues. However, VM file persistency does not work on clusters.

By default VM transport is synchronous – messages pass through VM transport are delivered in a point-to-point fashion.

What is File Connector

The File connector allows your Mule application to exchange files with a file system. You can implement the File connector as an inbound endpoint (such as, a message source), or as an outbound endpoint. This endpoint implements a one-way exchange pattern only.

We simply put a file in a source directory and Mule will read the file from the source directory. Using Mule ESB it’s very easy to read the file from a location. If we had to read the file using manual coding then we had to write many lines of code. But using Mule ESB we just simply put a file in a directory and let the Mule know the file path and Mule does the rest of the thing. You can put any kind of file to the source for reading.

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-3, 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.

So when the project mule-3 is created in the Anypoint Studio, the project structure looks like below
mule stdio
 
VM Transport with File Connector in Anypoint Studio
The graphical view of the vm-file.xml in Anypoint Studio is given below
Vm Transport with File Connector

In this example, if you configure an outbound VM endpoint called vm://vm-file-path but do not have a corresponding inbound endpoint, Mule will raise an error and explain that this message cannot be routed.

Here the VM transport is configured as a request-response exchange-pattern.

In the above screen-shot we have two Mule flows – vm-fileFlow1 and vm-fileFlow2.

In vm-fileFlow1 we have File and VM connectors. Here the address for VM transport is given as address=”vm://vm-file-path” (equivalent to path=”vm-file-path”), see in the below vm-file.xml configuration.

Here File connector acts as an inbound endpoint and VM acts as an outbound endpoint. The <file:filename-wildcard-filter/> indicates which type of file we want to read from the directory.

When you put a text file in the directory D:AnypointWorkspace as given in path attribute of the vm-file.xml, the file content is routed to vm-file-path(or vm://vm-file-path).

In vm-fileFlow2 we have VM connector, File to String component and Logger component. Here the path of the VM transport is given as path=”vm-file-path” (equivalent to address=”vm://vm-file-path”), see in the below vm-file.xml configuration.

Here VM transport acts as an inbound endpoint. The VM transport receives the file content from vm://vm-file-path (or vm-file-path).

File to String component converts the incoming payload coming from VM transport into equivalent String.

Logger component displays the payload coming from File to String in the console.

Step 1. You can rename the src/main/app/mule-3.xml file as vm-file.xml file.
Step 2. Open the vm-file.xml file and click on Configuration XML view in the Editor
Step 3. Modify the vm-file.xml file as shown below

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file"
	xmlns:vm="http://www.mulesoft.org/schema/mule/vm" 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/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">
	<flow name="vm-fileFlow1" doc:name="vm-fileFlow1">
		<file:inbound-endpoint path="D:AnypointWorkspace"
			responseTimeout="10000" doc:name="File">
			<file:filename-wildcard-filter pattern="*.txt" />
		</file:inbound-endpoint>
		<vm:outbound-endpoint exchange-pattern="request-response"
			path="vm-file-path" doc:name="VM" />
	</flow>
	<flow name="vm-fileFlow2" doc:name="vm-fileFlow2">
		<vm:inbound-endpoint exchange-pattern="request-response"
			path="vm-file-path" doc:name="VM" />
		<file:file-to-string-transformer
			doc:name="File to String" />
		<logger message="#[message.payload]" level="INFO" doc:name="Logger" />
	</flow>
</mule>

In the above vm.xml configuration you can use either address or path or ref in VM transport.
Now put a text file (name vm-file.txt) with below content in D:AnypointWorkspace
Hello, I am going through VM Transport, then I will be converted to String, finally I will be displayed in Console.
Running the application

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

**********************************************************************
* Application: mule-3                                                *
* OS encoding: Cp1252, Mule encoding: UTF-8                          *
*                                                                    *
* Agents Running:                                                    *
*   DevKit Extension Information                                     *
*   Batch module default engine                                      *
*   Clustering Agent                                                 *
*   JMX Agent                                                        *
**********************************************************************

You will see the file content in the Console

org.mule.api.processor.LoggerMessageProcessor: Hello, I am going through VM Transport, then I will be converted to String, finally I will be displayed in Console.

Thanks for reading.

Leave a Reply

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