当前位置: 首页 > news >正文

rabbit MQ的延迟队列处理模型示例(基于SpringBoot延时插件实现)

rabbitMQ安装插件rabbitmq-delayed-message-exchange

交换机由此type 表示组件安装成功
在这里插入图片描述

在这里插入图片描述

生产者发送消息时设置延迟值 消息在交换机滞纳至指定延迟后,进入队列,被消费者消费。

组件注解类:

package com.esint.configs;import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.HashMap;
import java.util.Map;@Configuration
public class DelayedQueueConfig {//交换机public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";//队列public static final String DELAYED_QUEUE_NAME = "delayed.queue";//routingKeypublic static final String DELAYED_ROUTING_KEY = "delayed.routingkey";/*** 基于插件声明一个自定义交换机* @return*/@Beanpublic  CustomExchange delayedExchange(){//String name, String type, boolean durable, boolean autoDelete, Map<String, Object> arguments) {Map<String, Object> arguments = new HashMap<>();arguments.put("x-delayed-type","direct");return new CustomExchange(DELAYED_EXCHANGE_NAME,"x-delayed-message",true, false,arguments);}@Beanpublic Queue delayedQueue(){return QueueBuilder.durable(DELAYED_QUEUE_NAME).build();}@Beanpublic Binding delayedQueueBindingDelayedExchange(@Qualifier("delayedQueue") Queue delayedQueue,@Qualifier("delayedExchange") CustomExchange delayedExchange){return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();}
}

生产者代码实现:

package com.esint.controller;//发送延迟消息import com.esint.configs.DelayedQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Date;@Slf4j
@RestController
@RequestMapping("/ttl")
public class SendMesController {@Autowiredprivate RabbitTemplate rabbitTemplate;@GetMapping("/sendDelayMsg/{message}/{delayTime}")public void sendMsg(@PathVariable String message,@PathVariable Integer delayTime){log.info("当前时间:{},发送一条ttl为{}ms的消息给延迟交换机转队列:{}",new Date().toString(),delayTime,message);rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE_NAME,DelayedQueueConfig.DELAYED_ROUTING_KEY,message, mes->{mes.getMessageProperties().setDelay(delayTime);return mes;});}}

消费者实现:

package com.esint.consumer;import com.esint.configs.DelayedQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.util.Date;/*** 基于插件的延时消息*/
@Slf4j
@Component
public class DelayQueueConsumer {//监听消息队列@RabbitListener(queues = DelayedQueueConfig.DELAYED_QUEUE_NAME)public void receiveDelayQueue(Message message){String msg = new String(message.getBody());log.info("当前时间{} 收到延迟消息:{}",new Date().toString(),msg);}
}

测试:

http://127.0.0.1:19092/ttl/sendDelayMsg/helloDelay1/30000
http://127.0.0.1:19092/ttl/sendDelayMsg/helloDelay2/3000

发送第一条消息:helloDelay1 延迟30s
发送第二条消息:helloDelay2 延迟3s

在这里插入图片描述

满足条件。

总结:
阻塞层在交换机。
发送消息灵活设置时间,现达到时间先被消费。
需要安装延时插件。

http://www.lryc.cn/news/241598.html

相关文章:

  • 虽不想承认,但这就是CSGO游戏搬砖行业的现状
  • 想问问各位大佬,网络安全这个专业普通人学习会有前景吗?
  • uniapp IOS从打包到上架流程(详细简单) 原创
  • React Native项目接入Sentry指南
  • 首批!创邻科技入选《图数据库金融应用场景优秀案例》
  • WPF树形控件TreeView使用介绍
  • Django 模型和Admin站点管理(三)
  • JVMj之console Java监视与管理控制台
  • Android进阶知识:ANR的定位与解决
  • 基于SSM的老年公寓信息管理(有报告)。Javaee项目
  • 阿里云 ACK 新升级,打造智算时代的现代化应用平台
  • CodeWhisperer 一款好玩的 AI 插件
  • 大模型生态新篇章:以AI Agent为引,助企业创新应用落地
  • 什么是网络安全工程师,你想知道的都在这里!
  • uniapp开发的微信小程序进行代码质量控制,分包+压缩js+组件按需注入等
  • GD32替换STM32使用HAL库开发问题
  • PE文件分析
  • MySQL 中文转拼音函数
  • HTML5+CSS3+JS小实例:蜂巢里的小蜜蜂光标特效
  • leetcode做题笔记1410. HTML 实体解析器
  • sql18(Leetcode1633各赛事的用户注册率)
  • 开发旅游APP的意义
  • docker安装xxl-job
  • Django QuerySet.order_by SQL注入漏洞(CVE-2021-35042)
  • 鼠标拖拽问题,不选中文本不触发单击事件
  • Java 之 final 详解
  • 数据分析策略
  • 子虔科技亮相2023工业软件生态大会 以先进理念赋能工业软件发展
  • 《C++PrimePlus》第9章 内存模型和名称空间
  • FFmpeg常用命令讲解及实战二