RabbitMQ的延迟队列实现[死信队列](笔记二)

2024-02-08 07:12

本文主要是介绍RabbitMQ的延迟队列实现[死信队列](笔记二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

上一篇已经讲述了实现死信队列的rabbitMQ服务配置,可以点击: RabbitMQ的延迟队列实现(笔记一)

目录

  • 搭建一个新的springboot项目
  • 模仿订单延迟支付过期操作
  • 启动项目进行测试

搭建一个新的springboot项目

1.相关核心依赖如下

		<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--mq依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><!-- lombok 依赖 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>

2.配置文件如下

server:port: 8080spring:#MQ配置rabbitmq:host: ipport: 5673username: rootpassword: root+12345678

3.目录结构
在这里插入图片描述

模仿订单延迟支付过期操作

1.创建OrderMqConstant.java,设定常量,代码如下

package com.example.aboutrabbit.constant;
/*** @description 订单队列常量* @author lxh* @time 2024/2/7 17:05*/
public interface OrderMqConstant {/***交换机*/String exchange = "order-event-exchange";/*** 队列*/String orderQueue = "order.delay.queue";/*** 路由*/String orderDelayRouting = "order.delay.routing";
}

2.创建OrderDelayConfig.java,配置绑定

package com.example.aboutrabbit.config;import com.example.aboutrabbit.constant.OrderMqConstant;
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;/*** @author lxh* @description 配置绑定* @time 2024/2/7 17:15**/
@Configuration
public class OrderDelayConfig {/*** 延时队列交换机* 注意这里的交换机类型:CustomExchange*/@Beanpublic CustomExchange maliceDelayExchange() {Map<String, Object> args = new HashMap<>();args.put("x-delayed-type", "direct");// 属性参数 交换机名称 交换机类型 是否持久化 是否自动删除 配置参数return new CustomExchange(OrderMqConstant.exchange, "x-delayed-message", true, false, args);}/*** 延时队列*/@Beanpublic Queue maliceDelayQueue() {// 属性参数 队列名称 是否持久化return new Queue(OrderMqConstant.orderQueue, true);}/*** 给延时队列绑定交换机*/@Beanpublic Binding maliceDelayBinding() {return BindingBuilder.bind(maliceDelayQueue()).to(maliceDelayExchange()).with(OrderMqConstant.orderDelayRouting).noargs();}
}

3、创建 OrderMQReceiver.java监听过期的消息

package com.example.aboutrabbit.config;import com.example.aboutrabbit.constant.OrderMqConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;
import java.util.Date;/*** @author lxh* @description 接收过期订单* @time 2024/2/7 17:21**/
@Component
@Slf4j
public class OrderMQReceiver {@RabbitListener(queues = OrderMqConstant.orderQueue)public void onDeadMessage(String infoId) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");log.info("收到头框过期时间:{},消息是:{}", sdf.format(new Date()), infoId);}
}

4.分别创建MQService.java和MQServiceImpl.java,处理消息发送

package com.example.aboutrabbit.service;
/*** @description MQ发消息服务* @author lxh* @time 2024/2/7 17:26*/
public interface MQService {/*** 发送或加队列* @param orderId 订单主键* @param time 毫秒*/void sendOrderAddInfo(Long orderId, Integer time);
}
package com.example.aboutrabbit.service.impl;import com.example.aboutrabbit.constant.OrderMqConstant;
import com.example.aboutrabbit.service.MQService;
import lombok.extern.slf4j.Slf4j;
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 lxh* @description MQ发消息服务实现* @time 2024/2/7 17:26**/
@Slf4j
@Service
public class MQServiceImpl implements MQService {@Autowiredprivate RabbitTemplate rabbitTemplate;/*** 发送或加队列* @param orderId 订单主键* @param time 毫秒*/@Overridepublic void sendOrderAddInfo(Long orderId, Integer time) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");log.info("过期队列添加|添加时间:{},内容是:{},过期毫秒数:{}",sdf.format(new Date()),orderId, time);rabbitTemplate.convertAndSend(OrderMqConstant.exchange, OrderMqConstant.orderDelayRouting,orderId,message -> {message.getMessageProperties().setDelay(time);return message;});}
}

5.创建控制层进行测试TestController.java

package com.example.aboutrabbit.controller;import com.example.aboutrabbit.service.MQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** @author lxh* @description 测试* @time 2024/2/7 17:36**/
@RestController
@RequestMapping("/test")
public class TestController {@Autowiredprivate MQService mqService;@GetMapping("/send")public String list(@RequestParam Long orderId,@RequestParam Integer fenTime) {//默认Integer time = fenTime * 60 * 1000;mqService.sendOrderAddInfo(orderId, time);return "success";}
}

6.全部结构展示
在这里插入图片描述

启动项目进行测试

1.示例:localhost:8080/test/send?orderId=1&fenTime=1
订单id为1的延迟一分钟过期,如下
在这里插入图片描述
2.查看日志
在这里插入图片描述

这篇关于RabbitMQ的延迟队列实现[死信队列](笔记二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/690274

相关文章

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

Nexus安装和启动的实现教程

《Nexus安装和启动的实现教程》:本文主要介绍Nexus安装和启动的实现教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Nexus下载二、Nexus安装和启动三、关闭Nexus总结一、Nexus下载官方下载链接:DownloadWindows系统根

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

MySQL 横向衍生表(Lateral Derived Tables)的实现

《MySQL横向衍生表(LateralDerivedTables)的实现》横向衍生表适用于在需要通过子查询获取中间结果集的场景,相对于普通衍生表,横向衍生表可以引用在其之前出现过的表名,本文就来... 目录一、横向衍生表用法示例1.1 用法示例1.2 使用建议前面我们介绍过mysql中的衍生表(From子句