5

RabbitMQ RPC Java Demo With Bootique

 3 years ago
source link: https://nixmash.com/java/rabbitmq-rpc-java-demo-with-bootique/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In this post I'm going to demonstrate using RabbitMQ RPC with the Bootique RabbitMQ Module. You can find RabbitMQ RPC examples elsewhere like in the RabbitMQ Java Examples on GitHub, but here we're going to enjoy the Bootique approach. All code in this post can be found in my NixMash Bootique RabbitMQ Demo repo on GitHub. Previous Bootique RabbitMQ-related posts are listed in the project's README.md file.

What is RabbitMQ RPC?

RPC stands for Remote Procedure Calls, or getting data back from a RabbitMQ Queue as well as send it. Up to this point we've had a one-way conversation with RabbitMQ Queues.

In our RPC Demo we're using the familiar Reservation Object and will be checking-in a guest. We have two task options:

  1. Send a String Message (the guest's name) to the RabbitMQ Server to receive a Customer object for additional local processing.
  2. Send a Reservation Object (the guest's name in {brackets}) to receive a quick summary message about the guest currently checking in.

Here is an example of both. First on the Client Side where we send a guest name of bob and receive a Customer object. Then we send a Reservation object and receive an quick summary message about our guest.

rpc0226a.png

Here is what the RabbitMQ Server receives, first a String followed by a Reservation object.

rpc0226b.png

On the Client

Below is our Client RPC method for the task of sending a Reservation object and receiving a Guest Summary message.

  1. We need to associate this RPC call with its return message so begin by creating a unique CorrelationId value.
  2. Create our Connection and Channel using Bootique then enter the CorrelationId in our AMQP Properties and the Queue Name for the returned data. Notice that the Queue Name is channel.queueDeclare().getQueue().
  3. Publish to the Queue along with our AMQP Properties and Reservation object.
  4. Setup our receipt from the Channel (channel.basicConsume() — notice the Queue Name) and handleDelivery(), looking for our unique CorrelationId.
rpc0226c.png

On the Server

On the Server end of the Queue we initialize our RabbitMQ Connection and Channel in Bootique, then

  1. Configure the Consumer for the RPC_MESSAGES_QUEUE Queue (listed at bottom of the method)
  2. Build our Reply Properties to include our unique CorrelationId.
  3. After performing our Reservation work we publish back to the Queue along with an Acknowledgment.
rpc0226d.png

Source Code Notes for this Post

Source code discussed in our Bootique RabbitMQ Series is found in my NixMash Bootique RabbitMQ Demo on GitHub.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK