This is the second part of the article that is focused on retrieving the RabbitMQ messages manually (Simply pulling the messages from queues whenever the client application needs them).
As you can see, in the first part, we have developed a Producer application. In this part, we will be developing a Consumer application to retrieve messages from the queue manually (whenever we need them)
The full source code of this article series can be found at GitHub.
Open the source code directory called consumer-manual-pull. In there you can see the source codes related to this article.
Here is the project structure.
first make sure that you have defined the valid RabbitMQ server details in the application.properties file. These information will be used by the ConnectionFactory class to make a connection with RabbitMQ server when the RabbitTemplate is used.
This is the RabbitMQ configuration class and we have configured the RabbitTemplate here.
As you are already familiar, we have done set of configurations for RabbitTemplate including the message converters. The only strange code here is the DefaultClassMapper bean. In order to understand the reason behind this bean, please refer the message segement.
In the MessageProperties, you can see that the __TypeId__ is set to following class declaration. This __TypeId__ information is added by the message converter defined in the Producer application when the POJO is transformed to the JSON format.
When the producer convert the POJO message to the JSON, the type information is added as a header information of the Message Properties. When the consumer receive the message, the message converter tries to convert the JSON message into the given type in the header. You can see that the Consumer application has a POJO class called Car. But it is in a different package. (Not in the same package as the header information declared) So we need to do the class mapping to inform message converter that where the relevant POJO for the message type is located.
Then the message converter can convert the incoming message to the relevant POJO format.
Now lets look at our consumer service used for receiving messages.
for the demonstration purpose, we will be receiving messages from “all_cars_queue“. we are using spring scheduled method to retrieve the message from the queue for every 5 seconds.
receiveAndConvert method of the RabbitTemplate will be used to pull/retrieve messages from queue. based on the FIFO principal, we will get the messages from the queue.
Object object = rabbitTemplate.receiveAndConvert(QUEUE_NAME);
You can run and see the output with following command. This application is developed to be run as a command line application.
When you run the application You will see the following output in the terminal.
No we have developed the Consumer application that is used to retrieve/pull messages manually.
If you are looking for a way to listen to the queue and retrieve messages automatically ( whenever the queue get a message), please refer the part 3 of this article series.