Nav

Example: To Publish and Get a Response in the VM Connector

In certain cases, you might want to get a response back. The publish-consume operation handles this by waiting for the message to be processed on the other end and providing the response. The operation is otherwise exactly the same as publish.

Building on the publish and listen example, suppose you want to send the response back. You can modify the example like this:


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

In the next example, notice that the <vm:listener> source embeds a DataWeave expression that builds the response.

Suppose that you want a response in in a predefined JSON format, such as simple one with a body and origin value:


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<flow name="listener">
  <vm:listener queueName="queue" config-ref="vm">
    <vm:response>
      <vm:content><![CDATA[#[
                    %dw 2.0
                    output application/json
                    ---
                    {
                            "origin": "Awesome Mule 4 VM connector",
                      "body" : payload
                    }
                 ]]>
      </vm:content>
    </vm:response>
  </vm:listener>
  <flow-ref name="theIntegrationLogic" />
</flow>

Error Handling

If the receiving flow fails, this operation will raise a VM:PUBLISH_CONSUMER_FLOW_ERROR error. This error’s description will be that of the original error raised by the flow.

Additionally, a VM:QUEUE_TIMEOUT might also be thrown if the receiving flow doesn’t respond in the expected timeframe.

In this topic: