Thanksgiving Week Special – Get 50% OFF on FileTranz

Claim Your Offer

Queueing Systems in MuleSoft: Understanding VM Queues

Understand the queueing systems in MuleSoft with VM queues. Learn the working principle, key use cases, and how to build asynchronous flows in MuleSoft.

By Prasanna Saravanan
Assistant Software Developer

Queueing Systems in MuleSoft: Understanding VM Queues

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."

What is 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.

 

Why Queues Feel "Difficult" at First:

  •  No instant response: Unlike an HTTP request, we are placing a message in the queue, and it will not give an actual complete or success message to a sender. You need to refer a log to find out information on how the message was processed.
  • Harder to debug: The messages are sent in one flow and published, then received in another flow, tracing everything that has occurred during the process is difficult.
  • Handling errors takes extra care: If there is an error in the receiving flow when processing a message, and no maximum number of retries is given, then the message may be retried infinitely.
  • Speed vs. safety: If messages are stored in memory, processing will be very quick, however if the server is restarted then data will be lost. If messages are stored on Disk, they will be safe but will be slightly slower.

How we achieve Queueing in MuleSoft:

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.

  1. VM (Virtual Machine) Queues — It is used to communicate between the components in the same Mule application or the projects.
  2. JMS (Java Message Service) — To connect with the common enterprise message brokers such as ActiveMQ and IBM. It is useful when we connect with java applications.
  3. AMQP (Advanced Message Queuing Protocol) — It is used to make the process much faster and make message across multiple platforms using message brokers such as RabbitMQ.
  4. Anypoint MQ — This is a service provided by MuleSoft. It is available in Anypoint Platform with the paid version.

In this blog, we will focus purely on VM—the easiest and fastest way to start using queues inside your MuleSoft project.

Understanding the MuleSoft VM Connector:

What is VM Connector?

  • This connector can be used in the form of asynchronous messaging between different flows or applications.
  • A VM Queue is essentially a location where messages can be temporarily published by one flow and then it is processed by another flow.
  • An example use case could be an onboarding application. Once a new employee has been added, there may be several operations performed such as:
    • Save employee details.
    • Send a welcome email.
    • Generate an employee ID.
    • Maintain an audit log.

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

How does VM Connector work?

The basic working process is:

  1. The Calling flow receives a request.
  2. It processes the incoming data (from request).
  3. The VM Publish operation sends the message to a created queue like (orderQueue, employeeQueue).
  4. Now the published message waits in the sent queue to receive
  5. Next operation flow uses VM Listener or Consume to receive the message, which is in the same queue.
  6. The receiving flow performs the required business operation.

Note: The important point is that both the publishing and receiving components must refer to the same VM configuration and queue name.

VM Connector Components:

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.

1. Publish:

  • The Publish component is used to send a message/payload into a VM queue.
  • Publishes the content in the queue. The message is published asynchronously (i.e. the current flow continues the execution immediately after publishing and it does not wait for the message to be processed by the receiving flow).
Example:

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"
}
When to use it?

Use this connector when you want to send a message to another flow without waiting for its processing result.

 
General Configuration – VM Publish
  • Display Name: Used to provide a custom name to identify the VM Publish component in the Mule flow.
  • Connector Configuration: Used to select the VM Global Configuration that contains the connector settings.
  • Content: It will specifies the data or payload that needs to be sent into the VM queue we created.
  • Send Correlation ID: We can send a UUID to identify and track our queue data(unique ID). Leave It as Empty or Set to Auto.
  • Correlation ID: Used to provide a custom or manual correlation ID to identify and also, we can track the message.
  • Queue Name: Here we need to mention the queue name which is to publish or consume.
  • Timeout: Defines how long the Publish operation should wait for the queue to accept the message.
  • Timeout Unit: Here we can mention our time unit, such as milliseconds, seconds, minutes, or hours.

 2.Listener:

  • The VM Listener is used to receive messages from a particular VM queue.
  • It continuously listens the mentioned queue. Whenever a message is available, it receives that message and start processing the flow.
Example:
  • In Flow 1, employee data is added to employeeQueue.
  • Flow 2 contains a VM Listener, which listens on this queue.
  • As soon as employee data has been added to the queue, processing begins by the VM Listener in Flow 2.
When to use it?

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

 

3. Consume:

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.

Example:
  • There are 5 messages in the Employee Queue.
  • When the consume flow runs the VM Consume operation will remove a message from the queue.
  • This can be useful if you wish to control exactly when messages are taken from the queue.
When to use it?

We can Use Consume component when you want to retrieve messages continuously listening to a queue.

 

4. Publish Consume:

This operation allows you to Publish information in a VM queue and Wait for a response from the target flow.

Example:

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.

When to use it?
  • Use Publish Consume when you need to send a message and receive the processing response.
  • Unlike normal Publish, this operation does not simply continue without waiting for the response.

Understanding VM Queue Types

Before creating a VM configuration, we should understand the two available queue types.

Transient Queue:
  • Messages are stored in memory.
    • Faster processing of messages.
    • Messages may not be persisted, if application/system fails.
    • Use this queue if messages do not need to be persisted.
Persistent Queue:
  • Persistent queues save messages in persistent storage
    • Better reliability of messages.
    • Slower than a temporary queue.
    • Messages must be serializable.

Important: Persistent VM queues are unavailable in CloudHub 2.0 and Runtime Fabric. Therefore, check your deployment environment before choosing this option.

How to Create a VM Configuration in Anypoint Studio(Global Configuration):

Now we can create a VM global configuration and create a Queues inside the global config. To do this follow these steps:

Step 1: Open the Mule Palette

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.

Step 2: Add a VM Component

Drag the VM Publish component into your flow.

Step 3: Create a Global Configuration

  • Open the Publish configuration. (Double click the Publish Component)
  • In general configuration you can see + icon. click it.
  • This opens the VM Config global element window.

Step 4: Configure the VM Global Element

In the VM Config window:

  • Provide a configuration name, such as VM_Config.
  • Navigate to the General  Queues and select Edit inline.
  • Click the + icon to add a new queue.

Step 5: Configure the Queue

 

  • Queue Name: Defines the name of the queue. Publisher and receiver components must use the same queue name which we created.
  • Queue Type: Defines whether the queue is Transient or Persistent. You can select based on the working process.
  • Max Outstanding Messages: Defines the maximum number of messages that can remain outstanding in the queue (you can keep it like 1000 or 2000).

Step 6: Save the Configuration:

  • Click Finish and then click OK.
  • Now your VM global configuration is ready.
  • You can reuse this configuration in other VM components within the application.

Hands-On Challenge: Build This VM Queue Flow Yourself:

Using the Mule 4 VM Connector, build an application with 3 queues: orderQueue, employeeQueue and publishConsumeQueue.

  1. Order (Publish + Listener):
    • Create an endpoint POST /order (Listener). Send a message to orderQueue and return message. Create separate flow and listen to messages from the queue using VM listener. Use Choice Router. If the price in the payload is greater than 1000, log message “Price is greater than 1000 - ELIGIBLE for discount”. Else log message “Price is less than or equal to 1000- NO discount” (default).
  2. Employee (Publish + Consume):
    • Create endpoint POST /employee (Listener). Send message to employeeQueue. In the same flow consume the message using VM consume. Add Choice Router. If the salary in the payload is greater than 100000, log message “Salary greater than 100000 - You are FIT“. Else log message “Salary less than or equal to 100000 - You are NOT FIT”.
  3. Publish Consume:  
          Create endpoint POST /publishConsume (Listener). Use VM Publish Consume operation with queue name publishConsumeQueue. Return response as {“Message”:”Publish Consume Process is Completed”}. In another         flow, create VM listener and add Choice Router. Based on salary value log message as “You Are Fit or Not Fit”.

Sample Test Input:

InputEligibleNot Eligible
orderQueue
{
  "orderId": 101,
  "item": "Laptop",
  "price": 1500
}
{
  "orderId": 102,
  "item": "Mouse",
  "price": 500
}
employeeQueue
{
  "name": "Ravi",
  "salary": 1200000
}
{
  "name": "Meena",
  "salary": 50000
}

 

XML code to do hands-on:

<?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 &gt; 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='#[{&#10;	"Message": "Demo"&#10;}]' />
		<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 &gt; 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 &gt; 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.

Anypoint Studio Flow: 

 

Watch & Learn:

 

Conclusion:

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!
 

 


free-consultation