Back to Blog Archive

Sending and Receiving Messages using RabbitMQ in Mule

Posted on: July 26, 2017
Author:
Denise

During a recent task, I needed to connect RabbitMQ to Mule so that I could send and receive messages from Mule to RabbitMQ and vice versa. RabbitMQ is a message broker software that implements Advanced Message Queueing Protocol (AMQP), and hence this connector was used in Mule to perform the necessary operations.

First we need to install the RabbitMQ Server. Follow the instructions here to install and setup the RabbitMQ Server for your desired environment. Throughout this documentation, I am using a Mac running Mac OS Sierra Version 10.12.5, and installed the RabbitMQ Server using Homebrew. If Homebrew is already installed on your computer, just run the command brew install rabbitmq

Then to run the server, open terminal and navigate to the installation directory. In my case it is  ~usr/local/sbin and run ./rabbitmq-server. You should get an output similar to that shown below:

Overview of RabbitMQ

If you have never worked with RabbitMQ or a message broker, this simple simulator will help you understand the basics. RabbitMQ sends messages from a Producer to a Consumer using queues and an exchange. Messages follow a path, defined using routing keys, where a routing key connects a queue to an exchange point.

Using RabbitMQ, we mainly use the tools rabbitmqadmin, and rabbitmqctl. rabbitmqctl provides information on the current server status, while rabbitmqadmin allows for adding new queues, exchanges, and other functionalities. More details can be obtained both by running rabbitmqadmin --help and rabbitmqctl --help in terminal, or in the documentation provided by RabbitMQ themselves.

Connecting to RabbitMQ using different Acknowledgements

The flows below were created so that we can connect Mule to RabbitMQ and then send and receive messages.

 

The flow connects to RabbitMQ using an AMQP Connector Configuration. In this case, the default settings were used and a new user was created with administration rights so as to have the necessary access permissions. Moreover, two bindings were defined using AMQP endpoints, one for queue1 and another for queue2 with the respective routing keys and binding details. The connector configuration was set to ACK_AUTO so messages were being acknowledged automatically by Mule.

Before executing the flow, the required queues, exchanges, and routing keys must be defined. To do this, open a new terminal, and run the desired commands. Below are a few commands used during the development of this task. Before executing any of these commands, make sure that the user has administrative rights. If errors for access permissions still persist and the user does have administrative rights, add -u username -p userpassword at the end of each command.

  • Creating a new exchange:./rabbitmqadmin declare queue name=name_of_new_queue
  • Creating a new queue:./rabbitmqadmin declare exchange name=name_of_new_exch type = desired_type 
  • Creating a Binding:./rabbitmqadmin declare binding source=exchange_name destination=queue_name routing_key=name_of_binding
  • Deleting an exchange (only one at a time): ./rabbitmqadmin delete exchange name=name_of_exchange
  • Deleting a queue (only one at a time): ./rabbitmqadmin delete queue name=name_of_queue
  • Listing all active queues:./rabbitmqctl list_queues
  • Listing all active exchanges:./rabbitmqctl list_exchanges
  • Listing all active bindings:./rabbitmqctl list_bindings
  • Sending Messages: ./rabbitmqadmin publish exchange=exchange_name routing_key=name_of_routing_key payload="some message"

The Mule Application shown above uses AUTO Acknowledgements for messages. The first AMQP inbound Endpoint, AMQP-MSG-FROM-QUEUE1 awaits messages sent from the server. Once a message is received, this is pushed onto the respective queue, is received by the AMQP inbound endpoint (i.e. the consumer), and is processed by the flow. The message on the queue is output to console using a Logger.

We can then send back a message to the server using Mule by loading a message using a Set Payload transformer and creating an AMPQ outbound endpoint. This message is handled by the queue2 Endpoint, where it is sent to the exchange and is subsequently received by Mule. Since it is now loaded on queue2, the 2nd flow is executed and outputs the message.

The flow was then modified such that Acknowledgements are handled manually. When a message is received by a consumer, it is acknowledged, and consequently popped off the queue. Using Manual Acknowledgements allows us to control when a message is popped off the queue.

To do this in Mule, in the Connector protocol configuration, we set the Ack Mode to MANUAL. We can then add AMPQ Acknowledge Message transformers to control when the messages are being acknowledged. In our case, we wish to retain the message and make sure it is processed before acknowledging it, so the acknowledgement is only sent after a message is sent to queue2. If the data being processed is not as important, and missing a message is not crucial, the message can be acknowledged as soon as it is received.

Note: Note: There exists a third form of AMQP Acknowledgements which is MULE_AUTO. In this form, Mule acknowledges the messages automatically when the flow has been processed, and a non-acknowledged message would represent a bug in the system.

 

Author:
Denise

One Comment for “Sending and Receiving Messages using RabbitMQ in Mule”

  1. Aman says:

    Very nice and simple article !

Comments

Contact Us

Ricston Ltd.
Triq G.F. Agius De Soldanis,
Birkirkara, BKR 4850,
Malta
MT: +356 2133 4457
UK: +44 (0)2071935107

Send our experts a message

Need Help?
Ask our Experts!