Contact Free trial Login

Example: To Send Messages across Different Apps - Mule 4

The following examples show two ways to send messages across different apps:

  • Use a Listener to receive the messages from the first app and a Publish operation to feed the received messages to a second app.

  • Use a Publish operation in a second app to push messages to the queue of messages, and a Consume operation to consume the messages.

In both cases, the two apps must be in the same domain.

Publish Messages to a Second App

  1. Create a VM configuration in a domain, like this:

    <mule-domain xmlns="http://www.mulesoft.org/schema/mule/domain"
      xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/vm
      http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
      http://www.mulesoft.org/schema/mule/domain
      http://www.mulesoft.org/schema/mule/domain/current/mule-domain.xsd">
      <vm:config name="sharedVMConfig">
        <vm:queue queueName="sharedQueue" queueType="PERSISTENT" />
      </vm:config>
    </mule-domain>
  2. Place a listener in an app in the same domain:

    <flow name="crossAppListener">
    	<vm:listener queueName="sharedQueue" config-ref="sharedVMConfig" />
    	<logger />
    </flow>
  3. Publish messages from a second app in the same domain:

    <flow name="crossAppRequester">
      <vm:publish queueName="sharedQueue" config-ref="sharedVMConfig" />
    </flow>

In this example, the Listener and Publish operation point to the same queue (sharedQueue) and the same configuration (sharedVMConfig).

Pull Published Messages from a Queue

Instead of using the <vm:listener> message source, you can consume messages in a VM queue through the <vm:consume> operation. This operation can consume messages on demand, allowing you to:

  • Dynamically consume from different queues depending on a certain condition.

  • Have a tighter control on the rhythm at which the app consumes messages.

To consume published messages from a queue:

  1. Create a VM configuration in a domain, like this:

    <mule-domain xmlns="http://www.mulesoft.org/schema/mule/domain"
      xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/vm
      http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
      http://www.mulesoft.org/schema/mule/domain
      http://www.mulesoft.org/schema/mule/domain/current/mule-domain.xsd">
    
      <vm:config name="sharedVMConfig">
        <vm:queue queueName="sharedQueue" queueType="PERSISTENT" />
      </vm:config>
    
    </mule-domain>
  2. Publish messages from a second app in the same domain:

    <flow name="crossAppRequester">
      <vm:publish queueName="sharedQueue" config-ref="sharedVMConfig" />
    </flow>
  3. Pull messages from the queue after they are published.

    <flow name="queueConsume">
      <vm:consume queueName="sharedQueue" config-ref="sharedVMConfig" />
      <logger />
    </flow>

In this example, the Publish and Consume operations point to the same queue (sharedQueue) and the same configuration (sharedVMConfig).