Contact Us 1-800-596-4880

VM Connector Publish Messages and Get a Response Back Example - Mule 4

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:

<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:

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

View on GitHub