Recently I learned about micro services and here is how Spring Boot integrates RabbitMQ to send messages to other micro services.


Getting started

1.Start by creating an empty Spring Boot project and add the Maven dependency.


2.Add the RabbitMQ server configuration information in application.yml.

    host: localhost
    port: 5672
    username: guest
    password: guest

3.Create a new message consumer:

package com.example.springbootmq.message;

import com.example.springbootmq.constant.QueueNameConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

 * @author
 * @version 1.0.0
 * @date 2020/11/4 15:40
public class MessageReceiver {

    @RabbitListener(queuesToDeclare = @Queue(QueueNameConstant.QUEUE_NAME))
    public void process(String message) {"The receiver receives the message: {}", message);

This is a simple example, in actual development MQ also needs to be bound with exchange, routing-key, etc.

4.Create a test class to act as a message producer:

package com.example.springbootmq;

import com.example.springbootmq.constant.QueueNameConstant;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

 * @author
 * @version 1.0.0
 * @date 2020/11/4 15:53
public class RabbitMqTest extends SpringbootMqApplicationTests {

    private AmqpTemplate amqpTemplate;

    public void testSendMessage() {
        String msg = "now time:" + new Date();"Send message 【{}】 to {} message queue.", msg, QueueNameConstant.QUEUE_NAME);
        amqpTemplate.convertAndSend(QueueNameConstant.QUEUE_NAME, "now time:" + new Date());

5.Start the project (message consumer) first, and then execute the test class (message producer):

Message producer output log:

com.example.springbootmq.RabbitMqTest: Send message 【now time:Wed Nov 04 16:33:46 CST 2020】 to test-mq message queue.

Message consumer output log:

c.e.s.message.MessageReceiver: The receiver receives the message: now time:Wed Nov 04 16:33:46 CST 2020

This was a simple tutorial on Spring Boot integrated RabbitMQ. For more information, please check those RabbitMQ tutorials.

Article Photo by Burst on Unsplash


Xianding Wang

Java Engineer

You may also like


PicoRuby (f/k/a mmruby) is an alternative Ruby implementation dedicated to one-chip microcontrollers. The binary fits into less than 256 KB ROM and runs on less than 64 KB RAM. This development was supported by the Ruby Association (RA) Grant program 2020 and mentored by Matz himself! Monstarlab is one of...

Using Policy As Code to manage permissions in REST APIs

Authorization and Authentication (AuthZ and AuthN) are very critical parts of any system for identity and permissions validation and enforcement, AuthN is basically the process to verify who the user is, while AuthZ is the process of validating the user’s permissions to access or perform certain functions/actions.