The Rabbit
Enterprise uses RabbitMQ extensively as a messaging solution. RabbitMQ is cloud ready and supports distributed deployment. It is also used for asynchronous messaging to create event driven applications. It is the most used open source message broker.
For this blog, I will be creating a RabbitMQ queue and exchange, and will use Micronaut to post data to it. This is a beginner guide that may help in getting started with these frameworks. Before we start installing, there are some definitions we will take care of.

RabbitMQ broker primarily consists of Exchange and Queues. Bindings define the message flow between these components. Components of interest are provided below:
- Producer/ Publisher: This is the process that sends messages
- Consumer/ Subscriber: This is the process that reads messages
- Exchange: This will receive messages from the publisher and send them to the Queues. There are different types of Exchanges:
- Direct: This is a one to one message flow where we have binding exactly map
- Topic: This will route messages based on routing key patterns
- Fanout: This will route messages to all consumers bound to this exchange
- Headers: The messages are routed based on headers
- Binding: This is a link between exchange and queue and transfers messages
- Queue: This is the bucket where messages accumulate. Consumers pick up messages from these buckets
- Virtual Hosts: This is used to segregate processes. Separate access and queues can be provided for each vhost making it easier to contain access
Next up we will install RabbitMQ.
Get the Rabbit
RabbitMQ is based on Erlang, so we have to install Erlang as the first step. You can find detailed steps for installation on Debian Linux here. Apt Repository on Bintray has the installation files for Erlang. After setting up this repository as trusted, we can use apt-get to install Erlang files.
$ sudo apt-get install -y erlang-base \ erlang-asn1 erlang-crypto erlang-eldap erlang-ftp erlang-inets \ erlang-mnesia erlang-os-mon erlang-parsetools erlang-public-key \ erlang-runtime-tools erlang-snmp erlang-ssl \ erlang-syntax-tools erlang-tftp erlang-tools erlang-xmerl
Finally, install rabbit-mq server also using apt-get.
$ sudo apt-get install -y rabbitmq-server
On successful RabbitMQ install, you can view status using following command. I am also adding commands to Start and Stop the server.
# Show RabbitMQ Status $ sudo rabbitmqctl status # Stop RabbitMQ server $ sudo rabbitmqctl shutdown # Start RabbitMQ server $ sudo rabbitmqctl startup
Last but not the least, we will enable the management console for RabbitMQ. This plugin provides a web based management and monitoring tool as well as access to a command line administration tool, rabbitmqadmin.
$ sudo rabbitmq-plugins enable rabbitmq_management
Taming the Rabbit
As mentioned before, we will create a very simple ‘direct’ route. This involves creating an exchange and queue. We will also create a binding to transfer messages from this exchange to the queue created. While at it, we will also install the rabbitmqadmin tool.
RabbitMQ by default provides a guest user with password guest. First and foremost, let’s change the password for this user to make it secure.
$ sudo rabbitmqctl change_password guest Pa$$word
There you have it! We have changed the password for guest from guest to a more secure Pa$$word. Of course, you should change it to something better in your systems. Now we will create a VHost (virtual host) and also some additional users.
$ sudo rabbitmqctl add_vhost simpson (1) $ sudo rabbitmqctl add_user homer Pa$$word (2) $ sudo rabbitmqctl set_user_tags homer management (3) $ sudo rabbitmqctl set_permissions -p / homer ".*" ".*" ".*" (4) $ sudo rabbitmqctl set_permissions -p simpson homer ".*" ".*" ".*" (5) $ sudo rabbitmqctl add_user marge Pa$$word (6) $ sudo rabbitmqctl set_user_tags marge administrator (7) $ sudo rabbitmqctl set_permissions -p simpson marge ".*" ".*" ".*" (8)
- We create a virtual host on line 1 called simpson (line 1)
- Next we create a new user called homer (line 2)
- We assign this user to management role (line 3). This allows the user to access web console running on default port 15672
- Line 4 and 5, we add this user to vhost / and simpson
- Subsequently we also add a new user marge and provide this user administrator access to simpson vhost
For subsequent commands we will need to use rabbitmqadmin command. So, we will install this next. This next command needs the management plugin to be enabled.
$ curl -i -u lisa:Pa$$word http://localhost:15672/cli/rabbitmqadmin > \ rabbitmqadmin.py
Command above will download a python file for rabbitmqadmin. User lisa used above has administrator access to root vhost.
Now that we have everything setup, let us create an exchange, a queue and a binding.
$ python3 rabbitmqadmin.py declare exchange --vhost=simpson name=testpub \ type=direct --username=marge --password=Pa$$word $ python3 rabbitmqadmin.py declare queue --vhost=simpson name=testsub \ durable=true --username=marge --password=Pa$$word $ python3 rabbitmqadmin.py declare binding --vhost=simpson source=testpub \ destination_type=queue destination=testsub routing_key=testkey \ --username=marge --password=Pa$$word
We have created an exchange named testpub, a queue named testsub and finally creating a binding joining these two.
I use nginx, so to access the management UI, I added a reverse proxy as below. So, now I can access management console as https://mycoolweb.none/rabbit.
location /rabbit/ { proxy_pass http://localhost:15672/; }

Micronaut ‘Basic’ Subscriber
Now that we have the queue configured, let us start subscribing for messages from this. Micronaut supports RabbitMQ directly and provides a command to generate a service.
$ mn create-app com.suturf.rabbit-subscribe --profile rabbitmq -b maven $ cd rabbit-subscribe $ mn create-rabbitmq-listener com.suturf.EchoSub
This creates a skeleton service template that adds the following dependency to your code.
<dependency> <groupId>io.micronaut.configuration</groupId> <artifactId>micronaut-rabbitmq</artifactId> <scope>compile</scope> </dependency>
Let’s now add the correct rabbitmq endpoint in application.yaml.
rabbitmq: uri: amqp://mycoolweb.none:5672 username: marge password: Pa$$word virtual-host: simpson
The only other change for a ‘basic’ subscriber is to fill in the listener code. Following shows how we dump listener messages to console. This class below will listen to the queue and start processing messages.
@RabbitListener public class EchoSubListener { private static final Logger LOG = LoggerFactory.getLogger(EchoSubListener.class); @Queue("testsub") public void receive(byte[] data) { LOG.info("Got Message value: {}", new String(data, StandardCharsets.UTF_8)); } }
Micronaut ‘Basic’ Publisher
Micronaut also makes it easy to create a simple publisher service. We will use the existing template classes provided to generate a basic publisher.
$ mn create-app com.suturf.rabbit-publish --profile rabbitmq -b maven $ cd rabbit-publish $ mn create-rabbitmq-producer com.suturf.EchoPub
This will create the publisher code and add needed dependencies. You will also need to specify proper RabbitMQ connection in application.yaml. In this case however, a Java interface is created for us as given below (where you go and change exchange names).
@RabbitClient("testpub") public interface EchoPubProducer { @Binding("testkey") void send(byte[] data); }
@Singleton public class TimePublisher { @Inject EchoPubProducer epub; private static final Logger LOG = LoggerFactory.getLogger(TimePublisher.class); private static final DateTimeFormatter FMT = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); @Scheduled(fixedDelay = "5s", initialDelay = "10s") void publishTime() { final String tm = ZonedDateTime.now().format(FMT); LOG.info("Publishing a message: {}", tm); epub.send(tm.getBytes(StandardCharsets.UTF_8)); } }
The second block of code gets an object for Publisher. See @Inject that will inject this value. Micronaut will instantiate an object and assign it to epub variable. To generate a dummy message, I have create a schedule to send the time every 5 seconds.
That’s all. What remains now is to compile and startup both the processes and see messages being generated and received by publisher and subscriber respectively.
Conclusion
That’s all for now. This blog just skimmed the top of what can be done from RabbitMQ as well as Micronaut perspective. You can find more details from individual web sites. All code will be found at https://github.com/chakrab/suturf-com.ibrave.host/. Hope this helps you get started on the journey to RabbitMQs powerful broker. Ciao for now!