The last article explained what RabbitMQ is and how to integrate it with Springboot, as well as how to integrate mail with Springboot.

Integration of the three

Based on the introduction of springboot integrated mail registration in the previous article, we only need to make the following changes to complete the three-way integration.

Implementation steps

  1. Add rabbitMQ dependencies

    <dependency>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-amqp</artifactId>

    </dependency>

    Copy the code
  2. Add the RabbitMQ configuration

    spring:

     rabbitmq:

       addresses: 127. 0. 01.

       usernameguest

       passwordguest

       port: 5672

    Copy the code
  3. Modify 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;



    @RestController

    @RequestMapping("user")

    public class UserController {

       @Autowired

       UserService userService;

       @Autowired

       RabbitTemplate rabbitTemplate;



       @PostMapping("save")

       public void save(@RequestBody TbUser user) {

           userService.save(user);

           rabbitTemplate.convertAndSend("email.direct","email.direct",user);

       }



    }

    Copy the code
  4. Modify the Service implementation class

    public void save(TbUser user{

           long startTime = System.currentTimeMillis();

           userMapper.insert(user);

           long endTime = System.currentTimeMillis();

           System.out.println("Time:"+(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");

           MailUtil.sendHtmlTemplateMail(user.getEmail(),user.getName(),fileList);

       }

    Copy the code

    Queues are shared with rabbitMQ. Queues are shared with rabbitMQ and queues are shared with rabbitMQ. Queues are shared with RabbitListener(Queues = “emp.direct “). Note that @enablerabbit should be added to the main startup class to EnableRabbit. When listening to the user’s registration, the page can be returned to the user without waiting for the email to be sent successfully, rather than waiting for the page to be unable to load (the browser keeps turning around), resulting in poor user experience. This is the simplest way to use rabbitMQ for asynchronous operations.

    test

    Here is a direct screenshot, you can compare the previous article synchronous serial user registration send mail time.

    email_ceshi2
email_ceshi2jieguo

I have uploaded the specific code to Github. If you have any questions, please refer to it.To travel to