Skip to content

How to Implement Dual Channel Scenario with RabbitMQ

This sample demonstrates how you can implement the request-reply messaging scenario (dual-channel scenario) using the RabbitMQ broker and WSO2 Integrator: MI.

As shown below, the OrderRequest proxy service in the WSO2 Integrator: MI receives an HTTP request, which it publishes to a RabbitMQ queue. This message is consumed and processed by the OrderProcessing proxy service in the WSO2 Integrator: MI, and the response is sent back to the client over HTTP.

Synapse configurations

See the instructions on how to build and run this example.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
    name="OrderRequestService"
    transports="http https"
    startOnLoad="true">
   <description/>
   <target>
    <inSequence>
      <property name="rabbitmq.message.content.type"
                value="Content-Type"
                scope="axis2"/>
      <property name="TRANSPORT_HEADERS" scope="axis2" action="remove"/>
      <call>
          <endpoint>
            <address uri="rabbitmq:/order-request?rabbitmq.server.host.name=localhost&amp;rabbitmq.server.port=5672&amp;rabbitmq.server.user.name=guest&amp;rabbitmq.server.password=guest"/>
          </endpoint>
      </call>
      <respond/>
    </inSequence>
   </target>
</proxy>
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
    name="OrderProcessingService"
    transports="rabbitmq"
    startOnLoad="true">
   <target>
    <inSequence>
      <call>
        <endpoint xmlns="http://ws.apache.org/ns/synapse">
          <http method="POST" uri-template="http://localhost:8290/orders"/>
        </endpoint>
      </call>
      <log level="full">
          <property name="Info" value="Your order has been placed successfully."/>
      </log>
      <respond/>
    </inSequence>
    <faultSequence>
      <payloadFactory media-type="xml">
          <format>
            <Error>$1</Error>
          </format>
          <args>
            <arg evaluator="xml" expression="get-property('ERROR_MESSAGE')"/>
          </args>
      </payloadFactory>
      <respond/>
    </faultSequence>
   </target>
   <parameter name="rabbitmq.queue.name">order-request</parameter>
   <parameter name="rabbitmq.connection.factory">AMQPConnectionFactory</parameter>
</proxy>

We can use the below synapse configurations to act as the mock backend called by the OrderProcessingService proxy service.

<?xml version="1.0" encoding="UTF-8"?>
<api context="/orders" name="mockOrdersBackend" xmlns="http://ws.apache.org/ns/synapse">
  <resource methods="POST">
    <inSequence>
      <property name="messageType" value="application/json" scope="axis2"/>
      <payloadFactory media-type="json">
        <format>{"message":"Order created"}</format>
          <args/>
      </payloadFactory>
      <respond/>
    </inSequence>
    <faultSequence>
      <payloadFactory media-type="json">
        <format>{"error":"Error processing order"}</format>
        <args/>
      </payloadFactory>
      <respond/>
    </faultSequence>
  </resource>
</api>

Build and run

  1. Launch Visual Studio Code with the WSO2 Integrator: MI for VS Code extension (MI for VS Code) installed.

    Info

    Follow the Install WSO2 Integrator: MI for VS Code documentation for a complete installation guide.

  2. Create an integration project.

  3. Create the proxy service with the configurations given above.
  4. Enable the RabbitMQ sender and receiver in the Micro-Integrator from the deployment.toml. Refer the configuring RabbitMQ documentation for more information.
  5. Deploy the artifacts in your WSO2 Integrator: MI.
  6. Make sure you have a RabbitMQ broker instance running.
  7. Send a message to the Order Request Proxy Service with the following payload.

    {   "orderId": "1242",
    "orderQty": 43,
    "orderDate": "2020/07/22"
    }
    

Note

Since RabbitMQ dual channel is a synchronous request-reply scenario, there can be instances where the connection pool can be exhausted. Please refer the RabbitMQ transport tuning documentation for more information on how to tune the RabbitMQ transport.