VM Connector in Mule 4 example
Why we use VM?
We use VM because of the following points:
1) The Virtual Machine (VM) connector handles intra-app and inter-app communication through asynchronous queues that can be transient or persistent.
2) Transient queues are faster than persistent queues, but they are not reliable in the case of a system crash.
3) Persistent queues are slower but reliable.
4) When running on a single instance, persistent queues work by serializing and storing the contents on the disk.
When to Use the VM Connector:
1) When you want to pass messages from one flow to another through a queuing mechanism, instead of using <flow-ref /> directly.
2) When you want to distribute work across a cluster.
3) When you want to communicate with different apps that are running in the same Mule domain.
4) When you need simple queuing that does not justify a full JMS broker.
Components in VM:
1. Publish: Publish the content to the queue
2. Listen: It listens to the VM queues
3. Consume: Pull one message from a queue. If the message is not immediately available, a message will wait up to configured queue timeout.
4. Publish Consume: Publishes the given content into a queue and then awaits up to the queue timeout for a response to be supply on a temporal reply- to queue that this operation automatically creates.
The flow of Publish:
Publish component configuration:
Connector configuration:
Flow of listener:
Method: GET
Input:
Output:
So, you can see clearly that the process is asynchronous and separate threads are executing for both the flows.
The first thread is executing loggers before VM and after VM and the second thread is executing payload logger that is Hello.
The flow of publish consume:
The flow of listener:
Method: GET
Output:
Here you can see the same thread is executing all the loggers one after one.
XML Project Code:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:sockets="http://www.mulesoft.org/schema/mule/sockets" 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/sockets http://www.mulesoft.org/schema/mule/sockets/current/mule-sockets.xsd">
<vm:config name="VM_Config" doc:name="VM Config" doc:id="162fd81e-d7e3-4443-b95c-068f452be47b">
<vm:queues >
<vm:queue queueName="vm" />
</vm:queues>
</vm:config>
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="f34f130a-ff45-41df-a8b0-e5a1192329a4" >
<http:listener-connection host="0.0.0.0" port="8085" />
</http:listener-config>
<flow name="test_vmFlow" doc:id="7cd43a2d-0bec-400a-93e7-92f1fd6bf145" >
<http:listener doc:name="Listener" doc:id="f941a0ea-db90-471d-97b4-b94002d4c608" config-ref="HTTP_Listener_config" path="/vm"/>
<logger level="INFO" doc:name="Logger" doc:id="9675b9e8-d6b2-4720-9f68-3ab0094ea9e9" message="before vm"/>
<vm:publish queueName="vm" doc:name="Publish" doc:id="9df15d7a-792a-40a8-ad13-96ed7108475e" config-ref="VM_Config" sendCorrelationId="ALWAYS">
</vm:publish>
<logger level="INFO" doc:name="Logger" doc:id="cbcf0b10-3a48-43d5-ba7b-c5dec877a457" message="after vm"/>
</flow>
<flow name="test_vmFlow1" doc:id="69cd8eb2-0fd5-408c-bc25-251c40249198" >
<vm:listener queueName="vm" doc:name="Listener" doc:id="30fd3738-201b-4428-8e90-42140ec82688" config-ref="VM_Config"/>
<logger level="INFO" doc:name="Logger" doc:id="106b97e9-9be5-462c-8303-dffc60777e85" message="#[payload]"/>
</flow>
<flow name="test_vmFlow2" doc:id="46726825-5875-41b8-b498-47a3622c00fa" >
<http:listener doc:name="Listener" doc:id="34fdd984-c2db-4235-852a-e052a542ec8b" config-ref="HTTP_Listener_config" path="/vm1"/>
<logger level="INFO" doc:name="Logger" doc:id="3c4cd112-9d94-4e1f-9316-2f9aca97df8d" message="before vm"/>
<vm:publish-consume queueName="vm" doc:name="Publish consume" doc:id="23ca2046-49d1-4786-b6b4-e412296d031b" config-ref="VM_Config"/>
<logger level="INFO" doc:name="Logger" doc:id="76f7a5a4-b3e2-4639-a6f1-882c3c08a418" message="after vm"/>
</flow>
<flow name="test_vmFlow3" doc:id="46cf614a-3464-49ee-b378-323d93dbe09f" >
<vm:listener doc:name="Listener" doc:id="26f18453-7287-46ea-bd4e-a47bce7af9b5" config-ref="VM_Config" queueName="vm"/>
<logger level="INFO" doc:name="Logger" doc:id="149cf101-6bb7-4f1d-840e-150ba1e6a780" message="#[payload]"/>
</flow>
<!-- <flow name="test_vmFlow1" doc:id="d2781c31-b6e2-4394-883c-f7afc844ecd1" >
<vm:listener queueName="vm" doc:name="Listener" doc:id="62ab05f9-3adc-4ff4-a646-58603c2b719f" config-ref="VM_Config"/>
<logger level="INFO" doc:name="Logger" doc:id="9c30c72f-51a6-49f4-8d63-9d1be38cc32b" message="after vm #[payload]"/>
</flow> -->
</mule>
No comments:
Post a Comment