Module CSC7321—CSN: Middleware and software architecture for Internet distributed applications

Portail informatique

RabbitMQ Lab — Tutorial

We use RabbitMQ broker in Docker containers. In addition, since we use Maven, the client library is going to be installed through a Maven dependency.

Introduction of RabbitMQ broker

Here follows some explanations about the commands to launch and control the RabbitMQ server; please refer to these short explanations when you read the tutorial steps:

  • The server is launched with the Docker command:
    $ docker run -itd --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.9-management
    The port 5672 is the port for the access to the broker and the port 15672 is the port for the access to management plugin of the broker.
    We use the container 3.9-management in order to include the management plugin.
  • The container just launched includes also a shell command (bash). Therefore, the server is controlled with the Docker command:
    $ docker exec rabbitmq rabbitmqctl <args>
    The utilitary command rabbitmqctl is a shell script, with the following arguments:
    • "status": to display broker status information such as the running applications, RabbitMQ and Erlang versions, OS name, memory and file descriptor statistics,
    • "stop": to stop the Erlang node on which the RabbitMQ broker is running,
    • "reset": to return the RabbitMQ broker to its virgin state (to be done after stopping the RabbitMQ broker with stop-app),
    • "stop-app": to stop the RabbitMQ application (the broker), leaving the Erlang node running,
    • "start-app": to start the RabbitMQ application (the broker) on the Erlang node,
    • "list_queues": to display queues details such their names.
    • "list_exchanges": to display exchanges details such their names.
    • "list_bindings": to display bindings details such the routing keys.
  • The RabbitMQ broker is stopped and the Docker container is remove using the following commands:
    $ docker stop rabbitmq $ docker rm rabbitmq

Follow the tutorial prepared by the RabbitMQ team

Get the source code of the tutorials

The source code of the tutorials is provided in Maven modules in the project csc-mw-examples on the platform https://gitlabens.imtbs-tsp.eu. If you cannot or do not want to use Git, the examples are also available in this archive. Then, you should have the following directories:

$ # e.g. content extracted from the archive $ mkdir ExemplesRabbitMQ $ cd ExemplesRabbitMQ $ tar xfz examples-rabbitmq.tgz $ ls -1 RabbitMQ-Tutorial-Step1 RabbitMQ-Tutorial-Step2 RabbitMQ-Tutorial-Step3 RabbitMQ-Tutorial-Step4 RabbitMQ-Tutorial-Step5 RabbitMQ-Tutorial-Step6

Before starting the tutorial, compile all the examples.

$ for d in RabbitMQ-Tutorial-Step?; do (cd $d; mvn clean compile test-compile;); done ... # quite long the first time you compile the examples [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ ...

RabbitMQ tutorial, step 1 (producer, consumer, and queue)

Move to the directory RabbitMQ-Tutorial-Step1. Learn the first set of AMQP concepts (producer, consumer, queue) with the corresponding RabbitMQ tutorial page. At the end of your reading of the tutorial page, you can execute the example as follows:

$ cd RabbitMQ-Tutorial-Step1 $ mvn -q test ... ------------------------------------------------------- T E S T S ------------------------------------------------------- Running eu.telecomsudparis.rabbitmq.tutorial.TestScenario Warning: PID file not written; -detached was passed. Stopping rabbit application on node... Resetting node ... Starting node... completed with 3 plugins. [*] Waiting for messages. To exit press CTRL+C [x] Sent 'Hello World!' [x] Received 'Hello World!' Stopping rabbit application on node... Stopping and halting node... ...

The code of the example is in the directory src/main/java/eu/telecomsudparis/rabbitmq/tutorial and the code of the test is in the directory src/test/java/eu/telecomsudparis/rabbitmq/tutorial. You can also import the Maven project in your favourite JAVA IDE, e.g. Eclipse, for browsing the code of the example.

Finally, comment the last two lines of the shell script run.sh and execute again the script. Let us now observe by executing the command "rabbitmqctl list_queues" that the queue hello that has been created still exists in the server even if there is no more producer or consumer. This is why we preventively reset the server. In the next steps, we could have not remove it and it will not disappear unless you stop and restart the server.

At your convenience, you can study the JUnit tests, compile and execute them with the command "mvn clean install". In case of problem "Connection refused" in the JUnit tests of the command "mvn clean install", check that you have activated the plugin rabbitmq_management with the command rabbitmq-plugins --offline enable rabbitmq_management.

RabbitMQ tutorial, step 2 (round-robing dispatching, durable message and message acknowledgment, message durability with durable queue and persistent message, fair dispatching with a prefetch countproducer, consumer, and queue)

Move to the directory RabbitMQ-Tutorial-Step2. Learn the next set of AMQP concepts (round-robing dispatching, durable message and message acknowledgment, message durability with durable queue and persistent message, fair dispatching with a prefetch countproducer, consumer, and queue) with the corresponding RabbitMQ tutorial page. At the end of your reading of the tutorial page, you can execute the example as follows:

$ cd ../RabbitMQ-Tutorial-Step2 $ mvn -q test ... ------------------------------------------------------- T E S T S ------------------------------------------------------- Warning: PID file not written; -detached was passed. Stopping rabbit application on node inf-12472@inf-12472 ... Resetting node inf-12472@inf-12472 ... Starting node inf-12472@inf-12472 ... completed with 3 plugins. [*] Waiting for messages. To exit press CTRL+C [*] Waiting for messages. To exit press CTRL+C [x] Sent 'wait one second .' [x] Sent 'wait two seconds..' [x] Sent 'wait three seconds...' [x] Sent 'wait four seconds....' [x] Sent 'wait five seconds.....' [x] Received 'wait one second .' [x] Received 'wait two seconds..' [x] Done [x] Received 'wait three seconds...' [x] Done [x] Received 'wait four seconds....' [x] Done [x] Received 'wait five seconds.....' [x] Done [x] Done Stopping rabbit application on node inf-12472@inf-12472 ... Stopping and halting node inf-12472@inf-12472 ... ...

The code of the example is in the directory src/main/java/eu/telecomsudparis/rabbitmq/tutorial and the code of the test is in the directory src/test/java/eu/telecomsudparis/rabbitmq/tutorial. You can also import the Maven project in your favourite JAVA IDE, e.g. Eclipse, for browsing the code of the example.

RabbitMQ tutorial, step 3 (exchange [of type fanout], binding, and temporary queue)

Move to the directory RabbitMQ-Tutorial-Step3. Learn the next set of AMQP concepts (exchange [of type fanout], binding, and temporary queue) with the corresponding RabbitMQ tutorial page. At the end of your reading of the tutorial page, you can execute the example as follows:

$ cd ../RabbitMQ-Tutorial-Step3 $ mvn -q test ... ------------------------------------------------------- T E S T S ------------------------------------------------------- Warning: PID file not written; -detached was passed. Stopping rabbit application on node... Resetting node... Starting node... completed with 3 plugins. [*] Waiting for messages. To exit press CTRL+C [*] Waiting for messages. To exit press CTRL+C [x] Sent 'message one' [x] Sent 'message two' [x] Sent 'message three' [x] Sent 'message four' [x] Sent 'message five' [x] Received 'message one' [x] Received 'message two' [x] Received 'message three' [x] Received 'message four' [x] Received 'message five' [x] Received 'message one' [x] Received 'message two' [x] Received 'message three' [x] Received 'message four' [x] Received 'message five' Stopping rabbit application on node... Stopping and halting node... ...

The code of the example is in the directory src/main/java/eu/telecomsudparis/rabbitmq/tutorial and the code of the test is in the directory src/test/java/eu/telecomsudparis/rabbitmq/tutorial. You can also import the Maven project in your favourite JAVA IDE, e.g. Eclipse, for browsing the code of the example.

RabbitMQ, tutorial, step 4 (binding key, exchange of type direct, routing key)

Move to the directory RabbitMQ-Tutorial-Step4. Learn the next set of AMQP concepts (binding key, exchange of type direct, routing key) with the corresponding RabbitMQ tutorial page. At the end of your reading of the tutorial page, you can execute the example as follows:

$ cd ../RabbitMQ-Tutorial-Step4 $ mvn -q test ... ------------------------------------------------------- T E S T S ------------------------------------------------------- Warning: PID file not written; -detached was passed. Stopping rabbit application on node... Resetting node... Starting node... completed with 3 plugins. [*] Waiting for messages. To exit press CTRL+C [*] Waiting for messages. To exit press CTRL+C [x] Sent 'info':'message one' [x] Sent 'debug':'message two' [x] Sent 'severe':'message three' [x] Sent 'info':'message four' [x] Sent 'debug':'message five' [x] Received ' info':'message one' [x] Received ' info':'message four' [x] Received ' info':'message one' [x] Received ' debug':'message two' [x] Received ' info':'message four' [x] Received ' debug':'message five' Stopping rabbit application on node... Stopping and halting node... ...

The code of the example is in the directory src/main/java/eu/telecomsudparis/rabbitmq/tutorial and the code of the test is in the directory src/test/java/eu/telecomsudparis/rabbitmq/tutorial. You can also import the Maven project in your favourite JAVA IDE, e.g. Eclipse, for browsing the code of the example.

RabbitMQ tutorial, step 5 (exchange of type topic, a word in a binding key, a star in a binding key, a hash in a binding key)

Move to the directory RabbitMQ-Tutorial-Step5. Learn the next set of AMQP concepts (exchange of type topic, a word in a binding key, a star in a binding key, a hash in a binding key) with the corresponding RabbitMQ tutorial page. At the end of your reading of the tutorial page, you can execute the example as follows:

$ cd ../RabbitMQ-Tutorial-Step5 $ mvn -q test ... ------------------------------------------------------- T E S T S ------------------------------------------------------- Warning: PID file not written; -detached was passed. Stopping rabbit application on node inf-12472@inf-12472 ... Resetting node inf-12472@inf-12472 ... Starting node inf-12472@inf-12472 ... completed with 3 plugins. [*] Waiting for messages. To exit press CTRL+C [*] Waiting for messages. To exit press CTRL+C [*] Waiting for messages. To exit press CTRL+C [x] Sent 'quick.orange.rabbit':'message one' [x] Sent 'lazy.orange.elephant':'message two' [x] Sent 'quick.orange.fox':'message three' [x] Sent 'lazy.brown.fox':'message four' [x] Sent 'lazy.pink.rabbit':'message five' [x] Sent 'lquick.brown.fox':'message six' [x] Sent 'orange':'message seven' [x] Sent 'quick.orange.male.rabbit':'message height' [x] Sent 'lazy.orange.male.rabbit':'message nine' [x] Received '0 quick.orange.rabbit':'message one' [x] Received '0 lazy.orange.elephant':'message two' [x] Received '0 quick.orange.fox':'message three' [x] Received '0 lazy.brown.fox':'message four' [x] Received '0 lazy.pink.rabbit':'message five' [x] Received '0 lquick.brown.fox':'message six' [x] Received '0 orange':'message seven' [x] Received '0 quick.orange.male.rabbit':'message height' [x] Received '0 lazy.orange.male.rabbit':'message nine' [x] Received '1 quick.orange.rabbit':'message one' [x] Received '1 lazy.orange.elephant':'message two' [x] Received '1 quick.orange.fox':'message three' [x] Received '2 quick.orange.rabbit':'message one' [x] Received '2 lazy.orange.elephant':'message two' [x] Received '2 lazy.brown.fox':'message four' [x] Received '2 lazy.pink.rabbit':'message five' [x] Received '2 lazy.orange.male.rabbit':'message nine' Stopping rabbit application on node inf-12472@inf-12472 ... Stopping and halting node inf-12472@inf-12472 ... ...

The code of the example is in the directory src/main/java/eu/telecomsudparis/rabbitmq/tutorial and the code of the test is in the directory src/test/java/eu/telecomsudparis/rabbitmq/tutorial. You can also import the Maven project in your favourite JAVA IDE, e.g. Eclipse, for browsing the code of the example.

RabbitMQ tutorial, step 6 (How to realize RPC communication over topic-based DEBS with RabbitMQ)

Move to the the directory RabbitMQ-Tutorial-Step6. Learn the last set of AMQP concepts (How to realize RPC communication over topic-based DEBS with RabbitMQ) with the corresponding RabbitMQ tutorial page.

$ cd ../RabbitMQ-Tutorial-Step6 $ mvn -q test ... ------------------------------------------------------- T E S T S ------------------------------------------------------- Stopping rabbit application on node... Resetting node... Starting node... completed with 3 plugins. [x] Requesting fib(0) [.] Got '0' [x] Requesting fib(1) [.] Got '1' [x] Requesting fib(2) [.] Got '1' [x] Requesting fib(3) [.] Got '2' [x] Requesting fib(4) [.] Got '3' [x] Requesting fib(5) [.] Got '5' [x] Requesting fib(6) [.] Got '8' Stopping rabbit application on node... Stopping and halting node... ...

The code of the example is in the directory src/main/java/eu/telecomsudparis/rabbitmq/tutorial and the code of the test is in the directory src/test/java/eu/telecomsudparis/rabbitmq/tutorial. You can also import the Maven project in your favourite JAVA IDE, e.g. Eclipse, for browsing the code of the example.

Contrary to what is proposed into the tutorial page, in our code, we propose three different versions:

  1. using the JAVA client library with the standard AMQP calls (this is the version that is presented in the tutorial page),
  2. using the RabbitMQ-specific class StringRpcServer,
  3. using the RabbitMQ-specific classes of the package com.rabbitmq.tools.jsonrpc.
For the sake of completeness, study the three implementations.

 

 

 


$Date: 2020-09-23 09:16:13 +0200 (mer. 23 sept. 2020) $