
RabbitMQ is a powerful open source messaging system that implements the Advanced Message Queuing Protocol (AMQP) standard. Nowadays, there are thousand of applications that need a reliable messaging service to queue, for example, requests. Amazon offers a messaging service called Simple Queue Service (SQS) which is suitable for little project where the message load is not very heavy.
Amazon’s SQS could seem very cheap ($0.01 per 10,000 Amazon SQS Requests) but if your application is repeatedly checking for new messages from several queues, the cost starts to matter. RabbitMQ is the solution. RabbitMQ is fast, free, supports clustering, easy-to-use, easy-to-install… However, there is one point that you need to take special care: availability. High-availability comes for free with Amazon SQS. You can find more information about RabbitMQ vs SQS here.
Let’s install and configure. We need to add a new repository to download RabbitMQ, so let’s open a new terminal and run:
sudo pico /etc/apt/sources.list
This file contains the repositories sources. Move to the end of the file and write:
deb http://www.rabbitmq.com/debian/ testing main
Now we need a public key to avoid warnings, so run:
wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc sudo apt-key add rabbitmq-signing-key-public.asc
It should have replied ‘OK’ when adding the key. So, let’s update and download RabbitMQ:
sudo apt-get update sudo apt-get install rabbitmq-server
RabbitMQ comes with a handy tool called ‘rabbitmqctl’ to control the application (it must be used as root). To make our system a little more secure, we should set up a password for the ‘guest’ user. I am using ‘abcde’ as example. We can do this running:
sudo rabbitmqctl change_password guest abcde
Now everything is ready to start using it. Remember if you are using a remote server to open RabbitMQ’s default port 5672 in the firewall to accept connections. To test it, we are using the Official Java Client. You’ll need the libraries to run this code:
package com.hveiga;
import java.io.IOException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownSignalException;
public class TestRabbitMQ {
private static String QUEUE_NAME = "TestQueue";
private static ConnectionFactory factory = new ConnectionFactory();
private static void init() {
factory.setHost("localhost"); // Host will be different if you are in remotes
factory.setUsername("guest");
factory.setPassword("abcde");
}
public static void send(String message) throws IOException {
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [S] Message Sent: '" + message + "'");
channel.close();
connection.close();
}
public static void receive() throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, true, consumer);
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println(" [R] Message Received: '" + message + "'");
}
public static void main(String[] args) {
init();
try {
send("HelloWorld!");
Thread.sleep(2000);
receive();
} catch (Exception e) {
e.printStackTrace();
}
}
}
More example and different uses can be found in the official site. In addition, Spring framework contains some method to interact with RabbitMQ. More information about Spring with RabbitMQ can be found here.