Publish and Subscribe Messages using VM Connector in MuleSoft

This article is the continued part in the MuleSoft articles series; if you would like to understand the basics of MuleSoft, refer to these articles before going next:

What VM Connector and Why?

VM Connector, known as Virtual Machine connector, is an Anypoint Connector which used to manage intra-app (means within the app) and inter-app (means outside of the app) communication using two built Mulesoft queues, transient or persistent asynchronous queues.

  • Transient Queues: Transient queues performance is faster than persisted queues, but they are not reliable. Data will be lost if the system crashes or restarted.
  • Persistent Queues: Persistent queues are slow but more reliable. Data will be persisted in case the system crashes or restarted.

Uses Cases

  • Used to perform simple queuing that doesn’t justify the use of a full JMS broker (Java Message Service).
  • Used to send the message from one mule flow to another mule flow through the queuing mechanism.
  • Used to distribute workloads across a cluster.
  • Used to communicate with different applications that are running in the same Mule domain.

Getting Started with Anypoint Studio

In this sample, we will exchange the message from one mule flow to another mule flow, and the message will be passed from the postman.

Let's do a quick project setup and include the required global config files; these are the quick steps:

First, open Anypoint Studio and Create a New Mule Project.

Create new project

Enter the project name, select runtime and project location, and click the Finish button.

Project setting

Next, add a New Mule Configuration file for global settings.

Global setting

Give the file name global and click Finish.

Configuration file

Next, go to the global.xml file, click on the Global Elements tab, click the Create button, select HTTP Listener config, and click OK.

Global type

Enter the required details like listener name, protocol, host, and port. I am keeping everything as default and clicking OK.

HTTP Listener

Drag and drop HTTP Listener from HTTP Palette to Message Flow, provide the display name, select global connector config, provide endpoint path, and change the name of flow for better understanding. This is the first flow for publishing the message.

Publish flow

Let’s add a logger for logging purposes.

Logger

Next, drag and drop the VM>Publish component from Mule Palette.

Publish

Enter the display name and click in Add + icon to add a connector configuration, message content will the payload which will be passed from the endpoint through the postman.

Click on + icon to add connector configuration.

VM Config

Enter the VM Config name, select Queues, and click on the + icon to add a new queue.

Queue

Enter the Queue name and select Queue type; I select default type transient and click finish.

Queue name

Once you are done with Queue then, you need to hit the refresh button to select the queue on the Publish component.

Next, add a new logger to log the steps.

Generics

Enter the log message and click save.

Next, drag and drop VM > Listener from Mule Palette.

Listener Flow

Enter the listener’s name and number of consumers and select the Queue name, which we have created and used in the publishing component.

At last, drag and drop one more logger for capturing the log activity after listening to the message.

Logger

Let's run the application to see the result; right-click on the file and click Run as Mule Application. Once you see the message Deployed, everything is good, and the application is deployed.

Console

Open Postman to see the result and enter the request endpoint to see the result.

Raw

Go back to the Mule app and see the logger messages.

cmd

Message

Conclusion

In this article, we learned how to publish and subscribe to messages using VM Connector in the MuleSoft application.


Similar Articles