Two RabbitMQ Data Creation Examples in Spring AMQP

In this post we're going to look at two different approaches to creating data objects with RabbitMQ in Spring AMQP. In each example we'll be creating an H2 Reservation Data Record in a RabbitMQ Handler, but in the first example we're going to send the result to another queue with @SendTo. In the second example we'll create the Reservation and return it in the same queue.

We'll jump to the output which outlines the process for us. We send a Reservation object to RabbitMQ queues and receive back Reservation objects complete with a Reservation ID.

Creating a Reservation With @SendTo

We use two queues in our first example, sending the "Waldo" Reservation from createQueue to showQueue with @SendTo. First the DataSender() code. We send our "Waldo" Reservation to our createQueue and retrieve it from showQueue.

The two queues handling the process look like this. We create the Reservation in createReservation() and send it to showReservation(). Notice we're sending a Message<T> to the reservationShow queue. We can send the Reservation object itself, but with a Message object we can add headers and properties for additional processing if needed.

Using a Single Queue to Create and Return Data Objects

In our second example we use a single queue to send a Reservation to be created. No @SendTo here, just returning the Serialized Java Object. Real simple. First the DataSender().

And the CreateAndShow Queue Handler.

A @Test

Here's another way of looking at the process using the Single Queue Handler approach. We create the Reservation and its ID is null. We send it to the createAndShow queue for processing and receive a fully populated Reservation object with an ID value.

Source Code Notes for this Post

Source code discussed in our Spring AMQP Series is found in NixMash RabbitMQ on GitHub. Code for this particular post is found in branch nixmashmq-v0.2.0 – Simple Producing and Consuming.