Springboot integrates rabbitmq to achieve user registration sample code


In the last article, we have introduced what rabbitmq is and how to integrate with springboot. We also introduced how to integrate email with springboot. If you don’t know, you can read the previous article.

Integration of the three

The last spring boot integrated mail registration has been introduced. Based on this introduction, we only need to modify the following to complete the integration of the three.

Implementation steps

Add rabbitmq dependency


Add rabbitmq configuration

  username: guest
  password: guest
  port: 5672

Modifying the controller class

package com.lytw13.demo.controller;

import com.lytw13.demo.model.TbUser;
import com.lytw13.demo.service.UserService;
import com.lytw13.demo.util.MailUtil;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class UserController {
  UserService userService;
  RabbitTemplate rabbitTemplate;

  public void save(@RequestBody TbUser user) {


Modify the service implementation class

public void save(TbUser user) {
    long startTime = System.currentTimeMillis();
    long endTime = System.currentTimeMillis();
    System.out.println (time consuming: + (Endtime starttime));

  @RabbitListener(queues = "email.direct")
  public void sendEmail(TbUser user) {
    List<String> fileList = new ArrayList<>();
    fileList.add ("E: \ others \ \ myblog \ \ new text document 1. TXT");
    fileList.add ("E: \ others \ \ myblog \ \ new text document. TXT");

Now it’s done. When the user sends the registration information, it will be pushed to rabbitmq. Then we just need to define a method to add @ rabbitlistener (queues = 0“ email.direct “) annotation for real-time monitoring,Note that @ enablerabbit needs to be added to the main boot class to enable rabbitWhen you listen to the user registration, you can return the page to the user without waiting for the e-mail to be sent successfully. Instead of waiting, the page can not be loaded all the time (the browser has been circling), which makes the user experience worse. This is the simplest use of rabbitmq to realize asynchronous operation.


Here is a direct cut of the figure, you can compare the next synchronous serial user registration to send mail time.


I have uploaded the specific code to GitHub. If you have any questions, please refer to it. go to

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

SDS of redis data structure

SDS(simple dynamic string), simple dynamic string. S and it’s called hacking string. Where hack is stored is the length of the string and the remaining space in SDS. The implementation of SDS insds.cIn the middle. C language string uses a character array of length N + 1 to represent the string of length N, and […]