Tuesday, September 24, 2013

RabbitMQ Worker Example

After RabbitMQ installation, tried simple programs to send and receive messages from a named queue.

In this example, we'll create a Work Queue that will be used to distribute time-consuming tasks among multiple workers to avoid doing a resource-intensive task immediately and having to wait for it to complete by scheduling the task to be done later.



We encapsulate a task as a message and send it to a queue. A worker process running in the background will pop the tasks and eventually execute the job. When you run many workers the tasks will be shared between them.


  1. Download the Java client from download page
  2. Extract the downloaded rabbitmq-java-client-bin-3.1.5.zip 
  3. Copy all *.jar files from the extracted folder to C:\RabbitMQ
  4. Copy NewTask.java from this link (open this link and copy the code from there to C:\RabbitMQ\NewTask.java)
  5. Copy the Worker.java from this link (open this link and copy the code from there to C:\RabbitMQ\Worker.java)
  6. Make sure that JAVA_HOME/bin is part of PATH
  7. Run the following commands to compile NewTask.java and Worker.java

cd C:\RabbitMQ
javac -cp rabbitmq-client.jar NewTask.java Worker.java


     8. You need two consoles to run the Workers that represents the two consumers, run the following command in both consoles to start the workers:

java -cp .;commons-io-1.2.jar;commons-cli-1.1.jar;rabbitmq-client.jar Worker

     9. Run the following command to start publishing new tasks

java -cp .;commons-io-1.2.jar;commons-cli-1.1.jar;rabbitmq-client.jar NewTask



More technical details are available here.


Monday, September 23, 2013

RabbitMQ Installation

In this tutorial, we will go through the RabbitMQ installation and testing steps on local Windows 7 64-bit machine.
The steps are based on RabbitMQ 3.1.5 that was the latest release at the time of writing this tutorial.

Erlang Installation


As per described on Erlang Versions page, you will need Erlang R15B or later to run RabbitMQ on a 64bit Windows machine.


  1. Download R15B02 Windows 64 Bit Binary File from Erlang R15B02 download page.
  2. Double click the executable otp_win64_R15B02_with_MSVCR100_installer_fix.exe
  3. Follow the wizard steps (click the screen shots to see further details).



RabbitMQ Installation


  1. Download the latest available release of RabbitMQ 3.1.5 from download page
  2. Double click the executable rabbitmq-server-3.1.5.exe
  3. Follow the wizard steps (click the screen shots to see further details).


After the installation is completed, you will have RabbitMQ running on default port 5672
You can control RabbitMQ using the shortcuts in start menu:


Java Clients

RabbitMQ is a message broker. The principal idea is pretty simple: it accepts and forwards messages that are binary blobs of data.

In this part of the tutorial (details on how this works is available here) we'll write two programs in Java; a producer that sends a single message, and a consumer that receives messages and prints them out. It's a "Hello World" of messaging.
  1. Download the Java client from download page
  2. Extract the downloaded rabbitmq-java-client-bin-3.1.5.zip 
  3. Copy all *.jar files from the extracted folder to C:\RabbitMQ
  4. Copy the Send.java from this link (open this link and copy the code from there to C:\RabbitMQ\Send.java)
  5. Copy the Recv.java from this link (open this link and copy the code from there to C:\RabbitMQ\Recv.java)
  6. Make sure that JAVA_HOME/bin is part of PATH
  7. Run the following command to compile Send.java and Recv.java
cd C:\RabbitMQ
javac -cp rabbitmq-client.jar Send.java Recv.java

     8. Run the following command to start the consumer:

java -cp .;commons-io-1.2.jar;commons-cli-1.1.jar;rabbitmq-client.jar Recv
    
     9. Run the following command to start the producer:

java -cp .;commons-io-1.2.jar;commons-cli-1.1.jar;rabbitmq-client.jar Send



Saturday, September 14, 2013

RabbitMQ


RabbitMQ



RabbitMQ is a message broker. In essence, it accepts messages from producers, and delivers them to consumers. In-between, it can route, buffer, and persist the messages according to rules you give it.

The RabbitMQ server is an implementation of an Advanced Message Queuing Protocol (AMQP) broker. It is written on the widely-used Erlang/Open Telecom Platform (OTP), an "always available‟ platform that has been in production use in carrier grade telcos since the late 1990s. The core server is unusually compact, with only 12,000 lines of code. Because RabbitMQ implements the open AMQP protocol, it is not necessary to know Erlang, and many users prefer to manage the broker using Java or Ruby.

RabbitMQ, and messaging in general, uses these terms:


  • Producing: means nothing more than sending
  • Producer "P": A program that sends messages
  • Queue: is the mailbox that lives inside RabbitMQ. The messages that flow through RabbitMQ and your applications can be stored only inside a queue. A queue is an infinite buffer where you can store as many messages as you like

Many producers can send messages that go to one queue - many consumers can try to receive data from one queue.

  • Consuming: is receiving messages
  • Consumer "C": is a program that mostly waits to receive messages

Producer, consumer, and broker do not have to reside on the same machine; indeed in most applications they don't.