RabbitMQ (AMQP 1.0) Connector Example¶ The RabbitMQ Connector allows publishing messages to RabbitMQ queues hosted on either local brokers or managed RabbitMQ services. It supports secure communication using TLS encryption and OAuth2 authentication, and exposes the publishing functionality through a RESTful API. Users can send messages to RabbitMQ over HTTP/HTTPS, simplifying integration with external systems. What you'll build¶ This example demonstrates how to use the RabbitMQ Connector to publish messages to a RabbitMQ queue through a REST API. The API exposes the resource /publishMessage, which enables users to send messages to a RabbitMQ queue. The user sends a payload containing the necessary information, such as the queue name and message content. The integration runtime then uses the RabbitMQ Connector to publish the message to the configured RabbitMQ broker. /publishMessage: This resource accepts a request payload that includes details such as the queue name and message content. Upon invocation, the API uses the RabbitMQ Connector to connect with the RabbitMQ broker and publish the message to the specified queue. The following diagram illustrates an overview of the RabbitMQ RESTful service that you are going to build in this example. If you do not want to configure this yourself, you can simply get the project and run it. Before you begin¶ Setup RabbitMQ Broker¶ To connect with RabbitMQ using the WSO2 Integrator: MI RabbitMQ Connector, you need to first set up a running RabbitMQ Broker instance locally or on a server. In this example, we will use a RabbitMQ standalone server. Set up RabbitMQ Broker by following the instructions in Set up RabbitMQ Broker. Develop the integration logic¶ Follow these steps to set up the Integration Project using the WSO2 Integrator: MI Visual Studio Code extension. Create a new project¶ Follow the steps in the create integration project guide to set up the WSO2 MI and create the integration project with the Project Name as follows: Create the integration logic¶ Create the API¶ Under the Create an integration section, select API to create a new REST API. Then, enter the API name as PublishMessage and the Context as /publishMessage and click Create. Select the newly created PublishMessage API and click the Edit button to change the API method. Then select the POST method and click Update. This will create a new API resource with the context /publishmessage and the method POST. Create sample request payload¶ To map elements from the request payload to the configuration parameters, you can define a sample request payload. To do this, follow the steps below: Click on the resource and you will be redirected to the Design View of the API. Now, click on the Start node on the canvas and select the Add Request option. This will open a pane to create a new example payload request. In this operation, we are going to receive the following inputs from the user. queue - The name of the RabbitMQ queue to which the message will be published. message - The content of the message to be published. Therefore, provide the following JSON payload to the request. { "queue": "TestQueue", "message": "Hello World!" } Add RabbitMQ Connector to the Project¶ Now we will add the publishMessage operation of the RabbitMQ Connector to the integration flow. To do this, we need to add the RabbitMQ Connector to the integration project first. For that, open the Resource View, click on the + icon on the canvas to open the Mediator Palette and search for RabbitMQ in the Mediators section. Then, select the RabbitMQ connector and click on the Download button. Click on the publishMessage operation to add that operation to the integration flow. Create Connection¶ Create a new connection by clicking on the '+ Add new connection' button as shown below. It will open a new pop-up window. Click on the RabbitMQ tile under the RabbitMQ Connector. Enter the connection name as RabbitMQConnection and provide the following details in the RabbitMQ Connection configuration pane. RabbitMQ Broker Urls: localhost:5672 SASL Mechanism: PLAIN OAuth2 Enabled: false Server Username: guest Server Password: guest Implement the API¶ Once the RabbitMQ connection is successfully created, it will be listed in the drop-down on the Connection section of the Add PublishMessage operation window. Now we need to configure the necessary parameters of the publishMessage operation. For some of the fields, we will use Synapse expressions to map values from the sample request defined in Create Sample Request Payload. Destination Queue/Exchange Section Queue: ${payload.queue} Queue Type: QUORUM Queue Auto Declare : true Request Message Settings Content Type: TEXT Request Body: ${payload.message} Publisher Client Settings Publisher Confirms: true Publish Timeout (MS): 60000 Output Section Output Variable Name: rabbitmq_publishMessage_1 To do this, click on the Expression icon in the Message field and select the Payload option. This will open a new pop-up window where you can select the payload.message parameter from the request payload. Next, do the same for the Request Body field. Click the + icon located immediately after the Publish Message operation to open the Mediator Palette. Add an If Else Mediator to verify the acknowledgment status from the RabbitMQ broker and proceed with the integration accordingly. Under Mediators, search for if else and select the If Else mediator. You will use an expression to define the condition for the If Else mediator. This condition evaluates whether the Success field in the RabbitMQ connector response is true to determine the appropriate flow. If true, the integration generates a success message and responds to the client application; otherwise, it sends an error message to the client. In the Add If Else Mediator pane that appears, click on the expression editor icon (EX) to open the editor. Select Variables → rabbitmq_publishMessage_1 → payload → success to extract the RabbitMQ broker acknowledgment status from the Publish Message response. Finally, click Add to insert the If Else mediator into the integration flow. Click on the + icon in the Then branch on the canvas to open the Mediator Palette. This branch executes when the Success field is true. Under Mediators, select the Log mediator to log the successful message publishing response from the RabbitMQ broker. Configure the Log mediator to use the variable ${vars.rabbitmq_publishMessage_1.payload.messageId} to log the message ID from the RabbitMQ response. Then select the Payload mediator from the Mediator Palette to create a custom response payload to be sent back to the client application. Repeat the same steps for the Else branch as well. Add the Log mediator to log the failure message, the Payload mediator to create a custom error response payload. Add the Respond Mediator to respond to the response of the publishMessage operation as shown below. RabbitMQ Publish Message API Info You can view the source view by clicking on the Show Source (</>) icon located in the top right corner of the VS Code. Design ViewSource View <?xml version="1.0" encoding="UTF-8"?> <api context="/publishmessage" name="PublishMessage" xmlns="http://ws.apache.org/ns/synapse"> <resource methods="POST" uri-template="/"> <inSequence> <rabbitmq.publishMessage configKey="RabbitMQConnection"> <headers>[]</headers> <requestBodyType>TEXT</requestBodyType> <requestBodyText>${payload.message}</requestBodyText> <requestCharSet></requestCharSet> <queue>{${payload.queue}}</queue> <queueAutoDeclare>true</queueAutoDeclare> <queueType>QUORUM</queueType> <queueArguments></queueArguments> <deliveryLimit></deliveryLimit> <deadLetterStrategy></deadLetterStrategy> <queueAutoDelete>false</queueAutoDelete> <queueOverflowStrategy>DROP_HEAD</queueOverflowStrategy> <exchange></exchange> <exchangeAutoDeclare>false</exchangeAutoDeclare> <exchangeType>DIRECT</exchangeType> <routingKey></routingKey> <exchangeArguments></exchangeArguments> <exchangeAutoDelete>false</exchangeAutoDelete> <publishTimeout>60000</publishTimeout> <publisherConfirms>true</publisherConfirms> <responseVariable>rabbitmq_publishMessage_1</responseVariable> <overwriteBody>false</overwriteBody> </rabbitmq.publishMessage> <filter xpath="${vars.rabbitmq_publishMessage_1.payload.success}"> <then> <log category="INFO" logMessageID="false" logFullPayload="false"> <message>Published message Successfully with message ID: ${vars.rabbitmq_publishMessage_1.payload.messageId}</message> </log> <payloadFactory media-type="json" template-type="default"> <format>${vars.rabbitmq_publishMessage_1.payload}</format> </payloadFactory> </then> <else> <log category="INFO" logMessageID="false" logFullPayload="false"> <message>Failed to Publish Message with message ID: ${vars.rabbitmq_publishMessage_1.payload.messageId}</message> </log> <payloadFactory media-type="json" template-type="default"> <format>${vars.rabbitmq_publishMessage_1.payload}</format> </payloadFactory> </else> </filter> <respond/> </inSequence> <faultSequence></faultSequence> </resource> </api> Export the integration project as a carbon application¶ To export the project, refer to the build and export the carbon application guide. Get the project¶ You can download the ZIP file and extract the contents to get the project code. Deployment¶ To deploy and run the project, refer to the build and run guide. Test¶ Create a file called data.json with the following payload. { "queue": "TestQueue", "message": "Hello World!" } Use the following curl command to invoke the API. curl -H "Content-Type: application/json" --request POST --data @data.json http://localhost:8290/publishmessage Expected Response: You should get a 'success' response as below along with the messageId. { "success": "true" "msgid: "urn:uuid:a40ec122-e3eb-4915-aafe-5686f90871eb" } What's next¶ To customize this example for your own scenario, see RabbitMQ (AMQP 1.0) Connector Reference documentation.