How to use WSO2 API Manager as RabbitMQ message producer

Introduction

In integration world, you often got some not usual scenarios. In one hand you would like have benefits witch WSO2 API Manager offers. On the other hand you look for benefits of asynchronous communication. In this scenario I show, how to configure WSO2 API Manager, to expose REST API to act as data feeder to RabittMQ.

Solution

  1. Firstly, we need to enable RabbitMQ sender in the API Manager configuration file.
    <API-M_HOME>\repository\conf\axis2\ axis2.xml
    We do this by adding the following configuration. In other words you must set your RabbitMQ IP address, user and password.
<transportSender name="rabbitmq" class="org.apache.axis2.transport.rabbitmq.RabbitMQSender"> 
    <parameter name="CachedRabbitMQConnectionFactory" locked="false">
    <parameter name="rabbitmq.server.host.name" locked="false">RabbitMQIp</parameter>
	<parameter name="rabbitmq.server.port" locked="false">5672</parameter>
	<parameter name="rabbitmq.server.user.name" locked="false">guest</parameter>
	<parameter name="rabbitmq.server.password" locked="false">guest</parameter>
    </parameter>
</transportSender>

2. Copy RabbitMQ client library files to location:
<API-M_HOME>\repository\components\lib
I was using: amqp-client-5.8.0.jar

3. After the above steps are done, start WSO2 API Manger and RabbitMQ.

4. For passing messages to RabbitMQ, we need to create mediation extension. In this extension we set asynchronous behavior. API Manager will response to client without waiting for response from backend and push message to RabbitMQ exchange. For this we need to set two properties, and use send to RabbitMQ endpoint. In the URI we set connectionstring parameters. We can also add rabbitmq.queue.delivery.mode=2, if you want to mark message as persistent in RabbitMQ. After that, the sequence should look like the following.

<sequence xmlns="http://ws.apache.org/ns/synapse" name="pass.to.rabbitmq">  
   <property name="OUT_ONLY" value="true"/>
   <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2"/>
   <send>
     <endpoint>
       <address uri="rabbitmq:/?rabbitmq.connection.factory=CachedRabbitMQConnectionFactory&rabbitmq.queue.route.key=route&rabbitmq.exchange.name=exchange"/>
     </endpoint>
   </send>
</sequence>

5. Now we need to create an API with API Manager. I suggest to create PUT or POST HTTP verb when defining resources.

6. Next in API Manager Publisher, in Runtime Configuration, we upload Message Mediation as Custom Policies that we created in step 4. and use our created pass.to.rabbitmq sequence.

After this is done, next steps are straight forward for API Manager APIs. Publish this API to the API Store. Now you can subscribe from API Store, and use it as regular one.

Summary

Inspiration for this post was this article, which describes passing data to JMS from WSO2 API Manager.