Springboot整合RabbitMQ---延迟队列

延迟队列插件地址:
https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases/tag/v3.12.0

下载后复制到D:\RabbitMQ Server\rabbitmq_server-3.10.5\plugins(路径自行修改,我是测试windows linux请自行查询):

然后在sbin目录下运行:rabbitmq-plugins enable rabbitmq_delayed_message_exchange

 

安装成功!

在客户端查看显示如下:

 

 

 开始代码演示:

1:pom.xml依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2:配置文件:
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456
spring.rabbitmq.virtual-host=/

3:RabbitMQ配置类:

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

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

/**
 * @Desc 插件版延迟队列
 * @User  Aiden
 * @DateTime: 2023-11-09 16:15
 * @Project: springboottest
 */
@Configuration
public class PluginsDelayConfig {
    public static final String IMMEDIATE_QUEUE_XDELAY = "queue.xdelay.immediate";//立即消费的队列名称
    public static final String DELAYED_EXCHANGE_XDELAY = "exchange.xdelay.delayed";//延时的exchange
    public static final String DELAY_ROUTING_KEY_XDELAY = "routingkey.xdelay.delay";//路由key

    /**
     * 立即消费队列
     * @return
     */
    @Bean
    public Queue immediateQueue() {
        // 第一个参数是创建的queue的名字,第二个参数是是否支持持久化
        return new Queue(IMMEDIATE_QUEUE_XDELAY, true);
    }

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

    /**
     * 把立即消费的队列和延时消费的exchange绑定在一起
     * @return
     */
    @Bean
    public Binding bindingNotify() {
        return BindingBuilder.bind(immediateQueue()).to(delayExchange()).with(DELAY_ROUTING_KEY_XDELAY).noargs();
    }
}

2:生产和消费:

import com.gyc.springboottest.config.PluginsDelayConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @Desc 发送消息
 * @User Aiden
 * @DateTime: 2023-11-09 15:50
 * @Project: springboottest
 */
@Component
public class MessageSender {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String msg, int delayTime) {
        System.out.println("内容:" + msg + "  延迟时间: " + delayTime);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.rabbitTemplate.convertAndSend(PluginsDelayConfig.DELAYED_EXCHANGE_XDELAY, PluginsDelayConfig.DELAY_ROUTING_KEY_XDELAY, msg, message -> {
            message.getMessageProperties().setDelay(delayTime);
            System.out.println("发送时间:" + sdf.format(new Date()));
            return message;
        });
    }
}
import com.gyc.springboottest.config.PluginsDelayConfig;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

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

/**
 * @Desc 消费者
 * @User Aiden
 * @DateTime: 2023-11-09 15:50
 * @Project: springboottest
 */
@Component
@EnableRabbit
@Configuration
public class MessageConsumer {
    @RabbitListener(queues = PluginsDelayConfig.IMMEDIATE_QUEUE_XDELAY)
    @RabbitHandler
    public void get(String msg) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("收到延时消息时间:" + sdf.format(new Date()) + " Delay sent.");
        System.out.println("收到延时消息了:" + msg);
    }
}

3:发送消息:

import com.gyc.springboottest.message.MessageSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Desc
 * @User Aiden
 * @DateTime: 2023-11-09 14:43
 * @Project: springboottest
 */
@RestController
public class MessageController {
    @Autowired
    private MessageSender messageSender;

    /**
     * 发送消息
     *
     * @return
     */
    @GetMapping("send/msg")
    public void send() {
        messageSender.send("你好", 3000);// 消息延迟三秒接收
    }
}

4:启动项目:浏览器打开:http://localhost:8083/send/msg

控制台查看输出:

 

  

  

简单案例,欢迎大家的指点!!!!!!  

  

  

posted @ 2023-11-09 16:45  TOPHP  阅读(38)  评论(0编辑  收藏  举报