Sunday, March 22, 2020

SpringBoot RabbitMQ Example


RabbitMQ is a widely used AMQP broker. In this article, we will learn how to integrate RabbitMQ with Spring Boot and develop a message producer and consumer example app with RabbitMQ and spring boot.  We will be building a simple notification system and we will be testing the app with CommandLineRunner. The producer will publish the message to the direct exchange with routing key and the consumer consumes this message asynchronously.

RabbitMQ is an AMQP (Advanced Message Queuing Protocol) broker and is different from JMS(Java Messaging Service). You can visit my previous articles for spring boot JMS integration here.

Installing RabbitMQ on Windows

Let us first start with RabbitMQ installation on our local system. Below are the steps.
  • First download and install Erlang depending upon Windows-32 or Windows-64 bit of your OS from the url https://www.erlang.org/downloads. The erlang version that I have is OTP 22.0
  • Next, download the windows installer from https://www.rabbitmq.com/install-windows.html and follow the window installment instruction.
  • Once the installation process is done, you can find the installation directory here C:\Program Files\RabbitMQ Server.
  • To enable the management console on Windows, you can traverse to the sbin directory and execute below command:

    C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.17\sbin>rabbitmq-plugins.bat enable rabbitmq_management


After this the RabbitMQ management console can be accessed at http://localhost:15672/ and the default username/password would be guest/guest.

Below are some of the useful RabbitMQ commands.

rabbitmqctl.bat stop
rabbitmqctl.bat status
rabbitmq-service.bat start
spring-boot-rabbitmq-example


RabbitMQ Essentials

RabbitMQ is a message broker that implements Advanced Message Queing Protocol(AMQP). It increases loose coupling and scalability.

In any messaging system, there are 3 components involved - Producer, Consumer, and Queue or Topic. The producer produces the messages in the Queue and the consumer consumes that message asynchronously from the queue or topic. But it is a little different in case of AMQP.

For a single exchange and queue, the process is very simple. The producer publishes a message to the exchange and the exchange sends the message to the queue and the consumer consumes the message from the queue.

With a complex system, we will have multiple queues and multiple consumers. In that case, the producer sends message to the exchange with a routing key and the exchange connects with the Queue only with binding key and then the messages are distributed to all the queues. 

There are different types of exchange.
  • Direct Exchange - It routes messages to a queue by matching routing key equal to binding key.
  • Fanout Exchange - It ignores the routing key and sends message to all the available queues.
  • Topic Exchange - It routes messages to multiple queues by a partial matching of a routing key. It uses patterns to match the routing and binding key.
  • Headers Exchange - It uses message header instead of routing key.
  • Default (Nameless) Exchange - It routes the message to queue name that exactly matches with the routing key.


Spring Boot RabbitMQ Project Setup

Head over to https://start.spring.io to download the sample spring boot project with spring-boot-starter-amqp artifact.

Below is the project structure.



Spring Boot RabbitMQ Configuration

There are a couple of beans that are required to configure in spring boot to integrate RabbitMQ with it.

Queue - There are two types of Queue - durable and non-durable. Durable queue survives a server restart. The binding() method binds these two together, defining the behavior that occurs when RabbitTemplate publishes to an exchange.

We are using TopicExchange here but Direct exchange can also be used and it depends on the requirement. Topic Exchange routes messages to multiple queues by a partial matching of a routing key. It uses patterns to match the routing and binding key whereas direct exchange routes messages to a queue by matching routing key equal to binding key.

The bean defined in the listenerAdapter() method is registered as a message listener in the container defined in container(). It will listen for messages on the "devglan.queue" queue. Because the RabbitMqListener class is a POJO, it needs to be wrapped in the MessageListenerAdapter, where you specify it to invoke listen().

We can also directly use the annotation @RabbitListener in the RabbitMqListener class.

By default Spring Boot uses org.springframework.amqp.support.converter.SimpleMessageConverter and serialize the object into byte[]. Hence, we have Jackson2JsonMessageConverter to send the message in a JSON format.

AMQPConfig.java
@Configuration
public class AMQPConfig {

    @Autowired
    private RabbitMQProperties rabbitMQProperties;

    @Bean
    Queue queue() {
        return new Queue(rabbitMQProperties.getQueueName(), false);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange(rabbitMQProperties.getExchangeName());
    }

    /*@Bean
    DirectExchange exchange() {
        return new DirectExchange(rabbitMQProperties().getExchangeName());
    }*/

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(rabbitMQProperties.getRoutingKey());
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
                                             MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(rabbitMQProperties.getQueueName());
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
        return new MappingJackson2MessageConverter();
    }

    @Bean
    public RabbitTemplate amqpTemplate(ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter());
        return rabbitTemplate;
    }

    @Bean
    public Jackson2JsonMessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    MessageListenerAdapter listenerAdapter(RabbitMqListener listener) {
        return new MessageListenerAdapter(listener, "listen");
    }

}

Below is our application.properties. Spring boot has default configuration of spring.rabbitmq.port as 5672 and spring.rabbitmq.host as localhost. Hence, no need to specify those configurations here.

rabbitmq.queueName=devglan.queue
rabbitmq.exchangeName=devglan-exchange
rabbitmq.routingKey=devglan.routingkey

RabbitMQ Message Producer

In the producer class, we have injected our RabbitTemplate bean that we defined in our config class. convertAndSend() method publishes the method to the exchange with specified routing key.

The message will be published in a JSON format as we have set the bean Jackson2JsonMessageConverter as message converter in our RabbitTemplate bean definition.

AMQPProducer.java
@Component
public class AMQPProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    RabbitMQProperties rabbitMQProperties;

    public void sendMessage(Notification msg){
        System.out.println("Send msg = " + msg.toString());
        rabbitTemplate.convertAndSend(rabbitMQProperties.getExchangeName(), rabbitMQProperties.getRoutingKey(), msg);
    }
}

Notification.java
public class Notification implements Serializable {

    private String notificationType;
    private String msg;

    public Notification() {
    }
}

RabbitMQ Message Consumer

We can also define a consumer by annotating the method with @RabbitListener:

@Component
public class RabbitMqListener {

    //@RabbitListener(queues="${rabbitmq.queueName}")
    public void listen(byte[] message) {
        String msg = new String(message);
        Notification not = new Gson().fromJson(msg, Notification.class);
        System.out.println("Received a new notification...");
        System.out.println(not.toString());
    }
}

Spring Boot Configuration Properties

Below is the helper class to read our environment properties.

@Configuration
@ConfigurationProperties(prefix = "rabbitmq")
public class RabbitMQProperties {

    private String queueName;
    private String exchangeName;
    private String routingKey;

    public String getQueueName() {
        return queueName;
    }

    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }

    public String getExchangeName() {
        return exchangeName;
    }

    public void setExchangeName(String exchangeName) {
        this.exchangeName = exchangeName;
    }

    public String getRoutingKey() {
        return routingKey;
    }

    public void setRoutingKey(String routingKey) {
        this.routingKey = routingKey;
    }
}

Testing Message Producer and Consumer

We have below CommandLineRunner implementation to test our asynchronous messaging system. This will invoke the sendMessage() of producer class. On running the SpringBootRabbitmqApplication.java we have below logs.



You can also use the management console to produce and consume the messages.


Conclusion

In this article, we learned about integrating RabbitMQ with Spring Boot and develop a message producer and consumer example app with RabbitMQ and spring boot.


References:


Simple Java + RabbitMQ Tutorial


Introduction

RabbitMQ is a message broker: it accepts and forwards messages. You can think about it as a post office: when you put the mail that you want posting in a post box, you can be sure that Mr. or Ms. Mailperson will eventually deliver the mail to your recipient. In this analogy, RabbitMQ is a post box, a post office and a postman.
The major difference between RabbitMQ and the post office is that it doesn't deal with paper, instead it accepts, stores and forwards binary blobs of data ‒ messages.

RabbitMQ, and messaging in general, uses some jargon.
  • Producing means nothing more than sending. A program that sends messages is a producer :
  • A queue is the name for a post box which lives inside RabbitMQ. Although messages flow through RabbitMQ and your applications, they can only be stored inside a queue. A queue is only bound by the host's memory & disk limits, it's essentially a large message buffer. Many producers can send messages that go to one queue, and many consumers can try to receive data from one queue. This is how we represent a queue:
  • Consuming has a similar meaning to receiving. A consumer is a program that mostly waits to receive messages:
Note that the producer, consumer, and broker do not have to reside on the same host; indeed in most applications they don't. An application can be both a producer and consumer, too.

"Hello World"

(using the Java Client)

In this part of the tutorial we'll write two programs in Java; a producer that sends a single message, and a consumer that receives messages and prints them out. We'll gloss over some of the detail in the Java API, concentrating on this very simple thing just to get started. It's a "Hello World" of messaging.
In the diagram below, "P" is our producer and "C" is our consumer. The box in the middle is a queue - a message buffer that RabbitMQ keeps on behalf of the consumer.
(P) -> [|||] -> (C)

The Java client library

RabbitMQ speaks multiple protocols. This tutorial uses AMQP 0-9-1, which is an open, general-purpose protocol for messaging. There are a number of clients for RabbitMQ in many different languages. We'll use the Java client provided by RabbitMQ.
Download the client library and its dependencies (SLF4J API and SLF4J Simple). Copy those files in your working directory, along the tutorials Java files.
Please note SLF4J Simple is enough for tutorials but you should use a full-blown logging library like Logback in production.
(The RabbitMQ Java client is also in the central Maven repository, with the groupId com.rabbitmq and the artifactId amqp-client.)
Now we have the Java client and its dependencies, we can write some code.

Sending

(P) -> [|||]
We'll call our message publisher (sender) Send and our message consumer (receiver) Recv. The publisher will connect to RabbitMQ, send a single message, then exit.
In Send.java, we need some classes imported:
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
Set up the class and name the queue:
public class Send {
  private final static String QUEUE_NAME = "hello";
  public static void main(String[] argv) throws Exception {
      ...
  }
}    
then we can create a connection to the server:
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
     Channel channel = connection.createChannel()) {

}
The connection abstracts the socket connection, and takes care of protocol version negotiation and authentication and so on for us. Here we connect to a broker on the local machine - hence the localhost. If we wanted to connect to a broker on a different machine we'd simply specify its name or IP address here.
Next we create a channel, which is where most of the API for getting things done resides. Note we can use a try-with-resources statement because both Connection and Channel implement java.io.Closeable. This way we don't need to close them explicitly in our code.
To send, we must declare a queue for us to send to; then we can publish a message to the queue, all of this in the try-with-resources statement:
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
Declaring a queue is idempotent - it will only be created if it doesn't exist already. The message content is a byte array, so you can encode whatever you like there.

Sending doesn't work!

If this is your first time using RabbitMQ and you don't see the "Sent" message then you may be left scratching your head wondering what could be wrong. Maybe the broker was started without enough free disk space (by default it needs at least 200 MB free) and is therefore refusing to accept messages. Check the broker logfile to confirm and reduce the limit if necessary. The configuration file documentation will show you how to set disk_free_limit.

Receiving

That's it for our publisher. Our consumer listening for messages from RabbitMQ, so unlike the publisher which publishes a single message, we'll keep it running to listen for messages and print them out.
[|||] -> (C)
The code (in Recv.java) has almost the same imports as Send:
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
The extra DefaultConsumer is a class implementing the Consumer interface we'll use to buffer the messages pushed to us by the server.
Setting up is the same as the publisher; we open a connection and a channel, and declare the queue from which we're going to consume. Note this matches up with the queue that send publishes to.
public class Recv {

  private final static String QUEUE_NAME = "hello";

  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

  }
}

Note that we declare the queue here, as well. Because we might start the consumer before the publisher, we want to make sure the queue exists before we try to consume messages from it.
Why don't we use a try-with-resource statement to automatically close the channel and the connection? By doing so we would simply make the program move on, close everything, and exit! This would be awkward because we want the process to stay alive while the consumer is listening asynchronously for messages to arrive.
We're about to tell the server to deliver us the messages from the queue. Since it will push us messages asynchronously, we provide a callback in the form of an object that will buffer the messages until we're ready to use them. That is what a DeliverCallback subclass does.
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
    String message = new String(delivery.getBody(), "UTF-8");
    System.out.println(" [x] Received '" + message + "'");
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });

Putting it all together

You can compile both of these with just the RabbitMQ java client on the classpath:
javac -cp amqp-client-5.7.1.jar Send.java Recv.java
To run them, you'll need rabbitmq-client.jar and its dependencies on the classpath. In a terminal, run the consumer (receiver):
java -cp .:amqp-client-5.7.1.jar:slf4j-api-1.7.26.jar:slf4j-simple-1.7.26.jar Recv
then, run the publisher (sender):
java -cp .:amqp-client-5.7.1.jar:slf4j-api-1.7.26.jar:slf4j-simple-1.7.26.jar Send
On Windows, use a semicolon instead of a colon to separate items in the classpath.
The consumer will print the message it gets from the publisher via RabbitMQ. The consumer will keep running, waiting for messages (Use Ctrl-C to stop it), so try running the publisher from another terminal.

Listing queues

You may wish to see what queues RabbitMQ has and how many messages are in them. You can do it (as a privileged user) using the rabbitmqctl tool:
sudo rabbitmqctl list_queues
On Windows, omit the sudo:
rabbitmqctl.bat list_queues
Time to move on to part 2 and build a simple work queue.

Hint

To save typing, you can set an environment variable for the classpath e.g.
export CP=.:amqp-client-5.7.1.jar:slf4j-api-1.7.26.jar:slf4j-simple-1.7.26.jar
java -cp $CP Send
or on Windows:
set CP=.;amqp-client-5.7.1.jar;slf4j-api-1.7.26.jar;slf4j-simple-1.7.26.jar
java -cp %CP% Send

Production [Non-]Suitability Disclaimer

Please keep in mind that this and other tutorials are, well, tutorials. They demonstrate one new concept at a time and may intentionally oversimplify some things and leave out others. For example topics such as connection management, error handling, connection recovery, concurrency and metric collection are largely omitted for the sake of brevity. Such simplified code should not be considered production ready.
Please take a look at the rest of the documentation before going live with your app. We particularly recommend the following guides: Publisher Confirms and Consumer AcknowledgementsProduction Checklist and Monitoring.

Getting Help and Providing Feedback

If you have questions about the contents of this tutorial or any other topic related to RabbitMQ, don't hesitate to ask them on the RabbitMQ mailing list.

Help Us Improve the Docs <3

If you'd like to contribute an improvement to the site, its source is available on GitHub. Simply fork the repository and submit a pull request. Thank you!


References: