Saturday 19 September 2015

RabbitMQ Hello world application

In this post, I am going to explain, simple producer-consumer scenario.

Producer.java : Produce messages and send them to RabbitMQ.
Receiver.java : Receive message from RabbitMQ and print to console.

Step 1: Download java client libraries for RabbitMQ.

You can download java client libraries from following location.


Following is the maven dependencies I used.

<dependency>
         <groupId>com.rabbitmq</groupId>
         <artifactId>amqp-client</artifactId>
         <version>3.5.4</version>
</dependency>


Step 2: Create Producer.java, which produce simple Hello World! Message and send to queue ‘hello’.
import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Producer {

 private final static String QUEUE_NAME = "hello";
 private static final String HOST_NAME = "127.0.0.1";

 /* get connection instance to connect to RabbitMQ broker */
 private static Connection getConnection() throws IOException,
   TimeoutException {
  /* Get factory instance to open connection to RabbitMQ broker */
  ConnectionFactory factory = new ConnectionFactory();

  factory.setHost(HOST_NAME);

  /* Create new broker connection */
  Connection connection = factory.newConnection();

  return connection;
 }

 public static void main(String[] argv) throws Exception {
  Connection connection = getConnection();

  Channel channel = connection.createChannel();

  /* Create queue if not exist */
  channel.queueDeclare(QUEUE_NAME, false, false, false, null);

  String message = "Hello World!";

  /* Public message to queue */
  channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));

  System.out.println("Sent '" + message + "'");

  /* Close channel and connection */
  channel.close();
  connection.close();
 }
}

Comments in the program are self explanatory.


Step 3: Create Consumer.java, which receives message from queue ‘hello’ and consume it.
import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Receiver {
 private final static String QUEUE_NAME = "hello";
 private static final String HOST_NAME = "127.0.0.1";

 /* get connection instance to connect to RabbitMQ broker */
 private static Connection getConnection() throws IOException,
   TimeoutException {
  /* Get factory instance to open connection to RabbitMQ broker */
  ConnectionFactory factory = new ConnectionFactory();

  factory.setHost(HOST_NAME);

  /* Create new broker connection */
  Connection connection = factory.newConnection();

  return connection;
 }

 public static void main(String[] argv) throws Exception {
  Connection connection = getConnection();
  Channel channel = connection.createChannel();

  /* Create queue if not exist */
  channel.queueDeclare(QUEUE_NAME, false, false, false, null);

  System.out.println("Waiting for messages");

  Consumer consumer = new DefaultConsumer(channel) {
   @Override
   public void handleDelivery(String consumerTag, Envelope envelope,
     AMQP.BasicProperties properties, byte[] body)
     throws IOException {
    String message = new String(body, "UTF-8");
    System.out.println(" [x] Received '" + message + "'");
   }
  };
  channel.basicConsume(QUEUE_NAME, true, consumer);
 }
}


Comments in the program are self explanatory.

Step 4: Compile Producer and Receiver.
$ javac -cp rabbitmq-client.jar Receiver.java Producer.java

Step 5: Run producer using following command.
$ java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Producer
Sent 'Hello World!'

Step 6: Use following command to list all queues
$ rabbitmqctl list_queues
Listing queues ...
hello  1

Step 7: Run Receiver using following command.

$ java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Receiver
Waiting for messages
 [x] Received 'Hello World!'

Queue deletes messages, once message delivered to consumer.  You can check using rabbitmqctl command.

$ rabbitmqctl list_queues
Listing queues ...
hello  0

That’s it…. You are done with first RabbitMQ application.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment