Producing messages

In this recipe we are learning how to send a message to an AMQP queue. We will be introduced to the building blocks of AMQP messaging: messages, queues, and exchanges.

You can find the source at Chapter01/Recipe02/src/rmqexample.

Getting ready

To use this recipe we need to set up the Java development environment as indicated in the Introduction section.

How to do it…

After connecting to the broker, as seen in the previous recipe, you can start sending messages performing the following steps:

  1. Declare the queue, calling the queueDeclare() method on com.rabbitmq.client.Channel:
    String myQueue = "myFirstQueue";
    channel.queueDeclare(myQueue, true, false, false, null);
  2. Send the very first message to the RabbitMQ broker:
    String message = "My ...

Get RabbitMQ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.