Thanksgiving Week Special – Get 50% OFF on FileTranz
Claim Your OfferUnderstand the queueing systems in MuleSoft with VM queues. Learn the working principle, key use cases, and how to build asynchronous flows in MuleSoft.
In this blog, I will be discussing my understanding of Queueing Systems and how they work with relevant examples. Also, I have created this blog to explain VM connector, its elements, configuration and implementation based on what I have learned and applied.
The majority of your Mule flows will be very simple. Receive an API call from system A, manipulate using DataWeave and send on to system B. Then provide a response.But in reality, things can go wrong. Maybe system B has become slow or you’re receiving a very high volume of requests, or maybe the database in the downstream system is down for 5 mins. As a result, your API will begin to timeout. Requests have failed and data may be lost.
At this point, every Mule developer will be told: "You need to use a queue."
Messages can be placed in queues, so that they can be processed by another system at some point. This means messages will be processed in order (First In, First Out), without making the calling system wait.

Unlike Mule, there is no unified messaging queue. Messaging queues are determined by the architecture being used, whether it is with an enterprise Java based message broker or between microservices. There are 4 main technologies.
In this blog, we will focus purely on VM—the easiest and fastest way to start using queues inside your MuleSoft project.
Instead of performing every operation in the same flow, we can publish messages to VM queues and allow separate flows to process them.
This makes the application easier to organize and maintain
The basic working process is:
Note: The important point is that both the publishing and receiving components must refer to the same VM configuration and queue name.
There are four important concepts:

Note: In Anypoint Studio, search for VM in the Mule Palette to access the VM Connector components. If the VM Connector is not available, navigate to Search in Exchange, search for the VM Connector, and add it to your Mule Palette. Once added, you can use the four available components: Listener, Publish, Consume, and Publish Consume.
An employee registration flow receives the following details. The flow publishes these details into employeeQueue. Another flow will receive the message using VM Listener or VM Consume.
{
"Name": "Prasanna",
"Employee_Id": "ID-07",
"Department": "EEE"
}Use this connector when you want to send a message to another flow without waiting for its processing result.

Use this connector, when you want another flow to automatically process messages arriving in a queue.

This Component is used to consume a message from a VM queue. With the VM Listener, messages are passed down the flow automatically. With Consume you can actively pull a message from the queue.
We can Use Consume component when you want to retrieve messages continuously listening to a queue.

This operation allows you to Publish information in a VM queue and Wait for a response from the target flow.
An employee detail is published by flow 1 and passed to another flow, via a VM queue. This detail is processed by the second flow. A response is returned. Publish & Consume waits until the entire flow executes.

Before creating a VM configuration, we should understand the two available queue types.
Important: Persistent VM queues are unavailable in CloudHub 2.0 and Runtime Fabric. Therefore, check your deployment environment before choosing this option.
Now we can create a VM global configuration and create a Queues inside the global config. To do this follow these steps:
Open your Mule project in Anypoint Studio. Look into the Mule Palette and expand the VM category. If it is not there then search inside the exchange portion and add it to our project.
Drag the VM Publish component into your flow.
In the VM Config window:

Using the Mule 4 VM Connector, build an application with 3 queues: orderQueue, employeeQueue and publishConsumeQueue.
| Input | Eligible | Not Eligible |
| orderQueue | | |
| employeeQueue | | |
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/vm
http://www.mulesoft.org/schema/mule/vm/current/mule-vm.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/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<http:listener-config name="HttpListener_Config">
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<vm:config name="VM_Config" doc:name="VM Config" doc:id="e39c8513-d18f-46f6-8a57-6dd230988a59" >
<vm:connection >
<reconnection failsDeployment="true" >
<reconnect />
</reconnection>
</vm:connection>
<vm:queues >
<vm:queue queueName="publishConsumeQueue" />
<vm:queue queueName="employeeQueue" />
<vm:queue queueName="orderQueue" />
</vm:queues>
</vm:config>
<flow name="OrderPublishFlow">
<http:listener doc:name="Http Listener Order" config-ref="HttpListener_Config" path="/order" allowedMethods="POST" />
<logger doc:name="Log Order Received" level="INFO" message="Order received, publishing to orderQueue" category="vm.order.publish" />
<vm:publish doc:name="Vm Publish Order Queue" queueName="orderQueue" timeoutUnit="MINUTES" config-ref="VM_Config"/>
<ee:transform doc:name="Transform Order Response" doc:id="df997d51-7713-42e4-a6ec-3a08759185d9">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
"Message": "Order published to orderQueue"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="OrderListenerFlow">
<vm:listener doc:name="Vm Listener Order Queue" queueName="orderQueue" timeoutUnit="MINUTES" config-ref="VM_Config"/>
<logger doc:name="Log Order Consumed" level="INFO" message="Order consumed by listener" category="vm.order.listener" />
<choice doc:name="Choice Price Check">
<when expression="#[payload.price > 1000]">
<logger doc:name="Log Eligible For Discount" level="INFO" message="Price is above 1000 - ELIGIBLE for discount" category="vm.order.eligible" />
</when>
<otherwise>
<logger doc:name="Log No Discount" level="INFO" message="Price is 1000 or below - NO discount (default)" category="vm.order.not-eligible" />
</otherwise>
</choice>
</flow>
<flow name="EmployeePublishConsumeFlow">
<http:listener doc:name="Http Listener Employee" config-ref="HttpListener_Config" path="/employee" allowedMethods="POST" />
<logger doc:name="Log Employee Received" level="INFO" message="Employee received, publishing to employeeQueue" category="vm.employee.publish" />
<vm:publish doc:name="Vm Publish Employee Queue" queueName="employeeQueue" timeoutUnit="MINUTES" config-ref="VM_Config"/>
<set-variable doc:name="Set Variable Sample" doc:id="45e8a0ef-5936-4678-a06b-ca2e83e07f5e" variableName="sample" value='#["DEMO"]' />
<set-payload doc:name="Set Payload Demo" doc:id="e999ecae-0c74-4c2b-967c-50c7b51943e1" value='#[{ "Message": "Demo" }]' />
<vm:consume doc:name="Vm Consume Employee Queue" queueName="employeeQueue" timeoutUnit="MINUTES" config-ref="VM_Config"/>
<choice doc:name="Choice Salary Check">
<when expression="#[payload.salary > 100000]">
<logger doc:name="Log Fit" level="INFO" message='#["Salary above 100000 - You are FIT"]' category="vm.employee.fit" />
</when>
<otherwise>
<logger doc:name="Log Not Fit" level="INFO" message='#["Salary 100000 or below - You are NOT FIT"]' category="vm.employee.not-fit" />
</otherwise>
</choice>
</flow>
<flow name="PublishConsumeRequestFlow" doc:id="bd8c7474-7789-455f-ae1d-3d7b1b7e715c">
<http:listener doc:name="Http Listener Publish Consume" doc:id="d46f0807-b9a3-449d-82c2-4716f92d7295" config-ref="HttpListener_Config" path="/publishConsume" />
<logger doc:name="Log Flow Start" doc:id="455ed114-515b-4a19-b02a-25cc257cb716" level="INFO" message='#["Flow Start"]' category="vm.publish-consume.start" />
<vm:publish-consume doc:name="Vm Publish Consume Queue" doc:id="ea789f9d-1953-40cb-8006-c13eb756cec6" queueName="publishConsumeQueue" timeoutUnit="MINUTES" config-ref="VM_Config"/>
<ee:transform doc:name="Transform Completed Response" doc:id="881ab925-0dcd-4de0-9478-698b42446151">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
"Message": "Publish Consume Process is Completed"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="PublishConsumeListenerFlow" doc:id="a3c01f52-26d4-4fce-8ade-0cf435d929b3">
<vm:listener doc:name="Vm Listener Publish Consume Queue" doc:id="468458bf-50ef-4f8d-8d78-b6d0cb6beabd" queueName="publishConsumeQueue" timeoutUnit="MINUTES" config-ref="VM_Config"/>
<choice doc:name="Choice Salary Check" doc:id="f94ce66c-e938-4ed2-b36b-67115f1a41a8">
<when expression="#[payload.salary > 1000]">
<logger doc:name="Log Fit" doc:id="a94a3d60-5d7a-4ee1-96fa-0a0327877d98" level="INFO" message='#["You Are Fit"]' category="vm.publish-consume.fit" />
</when>
<otherwise>
<logger doc:name="Log Not Fit" doc:id="49f99a26-91ae-43f5-89f8-b9804bfdb638" level="INFO" message='#["Not Fit"]' category="vm.publish-consume.not-fit" />
</otherwise>
</choice>
</flow>
</mule>Note: You can do by yourself. In case If there is any confusion, then you can directedly use this xml code in your Anypoint Studio. After importing the code then your flow will look like this.

In this blog, we have got an understanding about VM connector in MuleSoft, and how two mule flows can communicate with each other using the queues. We discussed about the various VM components like Listener, Publish, Consume and Publish Consume. Further, we looked at a practical example to understand how messages can be published in a queue and consumed by another flow and processed further. Thus we get a basic idea of communication between flows via queues using VM. In the next blog, we will look at other queueing technologies like JMS (Java Message Service) and AMQP (Advanced Message Queuing Protocol). We will understand their basic concepts, working, and how they can be implemented in MuleSoft using practical examples. STAY TUNED!