RabbitMQ implements message queue delay function

To implement RabbitMQ's message queue delay function, the official plug-in "rabbitmq_delayed_message_exchange" is generally used to implement it. But RabbitMQ version must be 3.5.8 or higher to support this plug-in, otherwise you have to use its "dead letter" function.

1. Install RabbitMQ delay plugin

(1) Check the plug-in

Use the rabbitmq-plugins list command to view the plug-ins installed by RabbitMQ.

Open a new cmd window (press the shortcut key "Win+R", enter "cmd").

Run the command: rabbitmq-plugins list

Check the installation of RabbitMQ plug-in:

(2) Download the plug-in

If the plug-in is not installed, go directly to the official website to proceed.

Official download address: https://www.rabbitmq.com/community-plugins.html

(3) Install the plug-in

After the download is complete, unzip it to the plugins directory of RabbitMQ. For example, my directory path is: D:\Program Files\RabbitMQ Server\rabbitmq_server-3.8.9\plugins

Open a new cmd window (press the shortcut key "Win+R", enter "cmd").

If the system has been configured with RabbitMQ environment variables, execute the following command to install it.

运行命令:rabbitmq-plugins enable rabbitmq_delayed_message_exchange

 

2. Implement RabbitMQ message queue delay function

[Example] Create a SpringBoot project to implement RabbitMQ message queue delay function.

(1) Use Maven to add dependent files

In the pom.xml configuration information file, add related dependent files:

<!-- AMQP客户端 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
    <version>2.4.1</version>
</dependency>

(2) RabbitMQ configuration

Configure RabbitMQ information in the application.yml configuration file:

spring:
  # 项目名称
  application:
    name: rabbitmq-delayed
  # RabbitMQ服务配置
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest

(3) RabbitMQ configuration class

Create the com.pjb.config package, and create the RabbitMqConfig class (RabbitMQ configuration class) to configure the switch.

package com.pjb.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * RabbitMQ配置类
 * @author pan_junbiao
 **/
@Configuration
public class RabbitMqConfig
{
    public static final String DELAY_EXCHANGE_NAME = "delayed_exchange";
    public static final String DELAY_QUEUE_NAME = "delay_queue_name";
    public static final String DELAY_ROUTING_KEY = "delay_routing_key";

    @Bean
    public CustomExchange delayExchange()
    {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAY_EXCHANGE_NAME, "x-delayed-message", true, false, args);
    }

    @Bean
    public Queue queue()
    {
        Queue queue = new Queue(DELAY_QUEUE_NAME, true);
        return queue;
    }

    @Bean
    public Binding binding(Queue queue, CustomExchange delayExchange)
    {
        return BindingBuilder.bind(queue).to(delayExchange).with(DELAY_ROUTING_KEY).noargs();
    }
}

(4) Realize message sending

Realize message sending, here set the message delay 5s.

package com.pjb.mq;

import com.pjb.config.RabbitMqConfig;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 消息发送者
 * @author pan_junbiao
 **/
@Service
public class CustomSender
{
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMsg(String msg)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("消息发送时间:" + sdf.format(new Date()));
        rabbitTemplate.convertAndSend(RabbitMqConfig.DELAY_EXCHANGE_NAME, RabbitMqConfig.DELAY_ROUTING_KEY, msg, new MessagePostProcessor()
        {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException
            {
                //消息延迟5秒
                message.getMessageProperties().setHeader("x-delay", 5000);
                return message;
            }
        });
    }
}

(5) Realize message reception

package com.pjb.mq;

import com.pjb.config.RabbitMqConfig;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 消息接收者
 * @author pan_junbiao
 **/
@Component
public class CustomReceiver
{
    @RabbitListener(queues = RabbitMqConfig.DELAY_QUEUE_NAME)
    public void receive(String msg)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(new Date())+msg);
        System.out.println("Receiver:执行取消订单");
    }
}

(6) Test sending delayed message

package com.pjb;

import com.pjb.mq.CustomSender;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * RabbitMQ测试类
 * @author pan_junbiao
 **/
@SpringBootTest
public class MQTest
{
    @Autowired
    private CustomSender customSender;

    @Test
    public void send() throws Exception
    {
        //发送消息
        customSender.sendMsg("支付超时,取消订单通知!");

        //程序延时15秒,否则程序立即执行完毕,则控制台无法看到消息队列延迟的结果
        Thread.sleep(15000);
    }
}

Results of the:

 

Guess you like

Origin blog.csdn.net/pan_junbiao/article/details/112711560