Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

Example: To Publish and Listen to Messages through the VM Connector

This example shows how to publish a message that is received by a different flow.

  1. Define a flow with a <vm:listener> as a message source, for example:

    
              
           
    1
    2
    3
    4
    
    <flow name="listener">
       <vm:listener queueName="myQueue" config-ref="vm"/>
       <flow-ref name="yourIntegrationLogic"/>
    </flow>
  2. Feed messages into the newly created queue.

    Suppose you have a flow that is triggered by an HTTP request with a JSON payload and you want to extract the body attribute of that JSON object and publish it to the queue, for example:

    
              
           
    1
    2
    3
    4
    5
    6
    7
    
        <flow name="publish">
            <http:listener path="/json-here" allowedMethods="POST" config-ref="httpListener" />
            <vm:publish queueName="myQueue">
                <vm:content>#[payload.body]</vm:content>
            </vm:publish>
            <logger/>
        </flow>

    In this example you can see:

    • The publish operation references the queue that you created in the other flow.

    • The content parameter allows you to use DataWeave to build the value to send (without producing side effects on the message). If you do not specify a content, the entire payload will be sent.

    • The publish operation is one-way. Once the message is posted into the queue, the flow continues. The processing on the listening side happen asynchronously. You do not receive notification of its outcome.

    • The queueName specified in the publish operation must have a matching <vm:listener> element that points to the same queue. If you attempt to publish to a queue for which no listener exists, an error will be raised. This behavior prevents message loss caused by sending messages into a queue that nobody is listening to.