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.
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.
- Download the Java client from download page
- Extract the downloaded rabbitmq-java-client-bin-3.1.5.zip
- Copy all *.jar files from the extracted folder to C:\RabbitMQ
- Copy NewTask.java from this link (open this link and copy the code from there to C:\RabbitMQ\NewTask.java)
- Copy the Worker.java from this link (open this link and copy the code from there to C:\RabbitMQ\Worker.java)
- Make sure that JAVA_HOME/bin is part of PATH
- 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