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

RabbitMq应用延时消息

一.建立绑定关系

package com.lx.mq.bind;import com.lx.constant.MonitorEventConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.HashMap;
import java.util.Map;/*** @author liuweiping.com* @version 1.0* @date 2023-06-26 10:04:03*/
@Slf4j
@Configuration
public class MonitorRabbitMqBinding {@Value(value = "-${spring.profiles.active}")private String profile;/*** Description: 延迟消息 <br/>* Created By: liu wei ping <br/>* Creation Time: 2023年6月26日 下午6:59:43 <br/>* <br/>* @return <br/>*/@Bean("delayExchange")public CustomExchange buildDelayedMessageNoticeExchange(){Map<String, Object> args = new HashMap<>();args.put("x-delayed-type", "direct");return new CustomExchange(MonitorEventConst.MONITOR_DELAYED_MESSAGE_EXCHANGE + profile, "x-delayed-message", Boolean.FALSE, Boolean.FALSE, args);}@Beanpublic Queue buildDelayedMessageNoticeQueue(){return QueueBuilder.durable(MonitorEventConst.MONITOR_DELAYED_MESSAGE_QUEUE + profile).build();}@Beanpublic Binding buildDelayedMessageNoticeBinding(){return BindingBuilder.bind(buildDelayedMessageNoticeQueue()).to(buildDelayedMessageNoticeExchange()).with(MonitorEventConst.MONITOR_DELAYED_MESSAGE_ROUTING_KEY).noargs();}/*** 交车完成事件消息定时处理队列*/@Beanpublic Queue deliveryCompleteEventHandQueue() {return QueueBuilder.durable(MonitorEventConst.DELIVERY_COMPLETE_DELAYED_QUEUE + profile).build();}/*** 交车完成事件消息定时处理队列绑定*/@Beanpublic Binding deliveryCompleteBinding() {return BindingBuilder.bind(deliveryCompleteEventHandQueue()).to(buildDelayedMessageNoticeExchange()).with(MonitorEventConst.DELIVERY_COMPLETE_DELAYED_ROUTING_KEY).noargs();}}

二.建立生产者

1.消息实体

package com.lx.dto.monitor;import lombok.Data;import java.util.Date;/*** @author liuweiping.com* @version 1.0* @date 2023-06-26 10:11:06*/
@Data
public class MonitorEventMessage {/*** 事件id*/private String eventId;/*** 事件编码*/private String eventCode;/*** 业务数据*/private String businessUniqueKey;/*** 业务类型*/private String businessType;/*** 到期时间*/private Long expireMillis;/***  时间处理唯一版本号*/private Integer eventHandVersion;/*** 定时处理时间*/private Date timedOperationTime;public void setTimedOperationTime(Date timedOperationTime) {this.timedOperationTime = timedOperationTime;expireMillis = timedOperationTime.getTime() - new Date().getTime();if (expireMillis < 0) {expireMillis = 0L;}}
}
package com.lx.mq.producer;import com.lx.constant.MonitorEventConst;
import com.lx.designPattern.strategypattern.workorderbase.service.sysField.JsonUtil;
import com.lx.dto.monitor.MonitorEventMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;/*** 监控事件消息发送类*/
@Slf4j
@Component
public class MonitorEventMessageProducer {@Value(value = "-${spring.profiles.active}")private String profile;@Autowiredprivate RabbitTemplate rabbitTemplate;/***  交车完成监控事件定时发送*/public void sendDeliveryCompleteEventHandMessage(MonitorEventMessage monitorEventMessage) {String message = JsonUtil.toJson(monitorEventMessage);;rabbitTemplate.convertAndSend(MonitorEventConst.MONITOR_DELAYED_MESSAGE_EXCHANGE + profile,MonitorEventConst.DELIVERY_COMPLETE_DELAYED_ROUTING_KEY,message,msg -> {msg.getMessageProperties().setDelay(monitorEventMessage.getExpireMillis().intValue());return msg;});log.info("sending event processing messages: {}", message);//发送事件处理消息}}

三.建立消费者

package com.lx.mq.consumer;import com.lx.constant.MonitorEventConst;
import com.lx.designPattern.strategypattern.workorderbase.service.sysField.JsonUtil;
import com.lx.dto.monitor.MonitorEventMessage;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;import java.nio.charset.StandardCharsets;/*** 监控事件消息发送类*/
@Slf4j
@Component
public class MonitorEventMessageConsumer {@Value(value = "-${spring.profiles.active}")private String profile;/***  交车完成事件处理mq监听*/@RabbitListener(queues = MonitorEventConst.DELIVERY_COMPLETE_DELAYED_QUEUE + "-${spring.profiles.active}")public void dealWithDeliveryCompleteEventHandMessage(String eventMessage, Channel channel, Message message) {log.info("dealWithDeliveryCompleteEventHandMessage:【{}】", JsonUtil.toJson(eventMessage));String str = new String(message.getBody(), StandardCharsets.UTF_8);log.info("Received the message of regular loading and unloading of goods: {}", str); //收到商品定时上下架消息MonitorEventMessage monitorEventMessage = JsonUtil.toBean(eventMessage, MonitorEventMessage.class);try {analyzeHand(monitorEventMessage);}catch (Exception e){log.error("交车完成事件分析失败,参数:{},e:{}",JsonUtil.toJson(monitorEventMessage),JsonUtil.toJson(e));}}/***  事件分析* @param monitorEventMessage*/private void  analyzeHand(MonitorEventMessage monitorEventMessage) throws Exception {}
}

四.测试类测试

package com.lx.controller;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;import com.lx.conf.MQConfig;
import com.lx.conmon.ResultData;
import com.lx.dto.monitor.MonitorEventMessage;
import com.lx.mq.producer.MonitorEventMessageProducer;
import com.lx.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RestController;import com.lx.constant.RedisMange;
import com.lx.utils.RedisUtil;
import org.thymeleaf.util.DateUtils;/*** @Author : liu wei ping* @CreateTime : 2019/9/3* @Description :**/@RestController
public class SendMessageController {@Autowiredprivate MonitorEventMessageProducer messageProducer;@GetMapping("/sendTopicMessage3")public ResultData<String> sendTopicMessage3() {MonitorEventMessage monitorEventMessage = new MonitorEventMessage();monitorEventMessage.setEventCode("delivery");//设置定时处理时间= 当前时间+ 定时处理时长monitorEventMessage.setTimedOperationTime(DateUtil.date(DateUtil.getCurrentMillis() + 30 * 1000));monitorEventMessage.setBusinessType("deliveryType");messageProducer.sendDeliveryCompleteEventHandMessage(monitorEventMessage);return new ResultData<>("ok");}
}

五.效果如图所示

相关文章:

RabbitMq应用延时消息

一.建立绑定关系 package com.lx.mq.bind;import com.lx.constant.MonitorEventConst; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annota…...

【WEB自动化测试】- 浏览器操作方法

一、常用方法 1. maximize_window() 最大化窗口 (重点) 说明&#xff1a;如果能够在打开页面时&#xff0c;全屏显示页面&#xff0c;就能尽最大可能加载更多的页面&#xff0c;提高可定位性 2. set_window_size(width, height) 设置浏览器窗口的大小 (了解) 场景&#xff1…...

VSCode设置鼠标滚轮滑动设置字体大小

1&#xff1a;打开"文件->首选项->设置 2 :打开settings.json文件 英文版这里有个坑 一般点击我下图右上角那个{ } 就可以打开了 在 设置的json 文件中加入如下 “editor.mouseWheelZoom”: true { “editor.mouseWheelZoom”: true, “json.schemas”: [ ]}...

Spring MVC是什么?详解它的组件、请求流程及注解

作者&#xff1a;Insist-- 个人主页&#xff1a;insist--个人主页 作者会持续更新网络知识和python基础知识&#xff0c;期待你的关注 前言 本文将讲解Spring MVC是什么&#xff0c;它的优缺点与九大组件&#xff0c;以及它的请求流程与常用的注解。 目录 一、Spring MVC是什…...

基于Spring Boot的广告公司业务管理平台设计与实现(Java+spring boot+MySQL)

获取源码或者论文请私信博主 演示视频&#xff1a; 基于Spring Boot的广告公司业务管理平台设计与实现&#xff08;Javaspring bootMySQL&#xff09; 使用技术&#xff1a; 前端&#xff1a;html css javascript jQuery ajax thymeleaf 后端&#xff1a;Java springboot框架 …...

docker 基本命令安装流程

docker 基本命令安装流程 1.更新Ubuntu的apt源索引 $ sudo apt-get update2.安装包允许apt通过HTTPS使用仓库 $ sudo dpkg --configure -a $ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common3.添加Docker官方GPG key $ curl -f…...

尚硅谷大数据Flink1.17实战教程-笔记02【Flink部署】

尚硅谷大数据技术-教程-学习路线-笔记汇总表【课程资料下载】视频地址&#xff1a;尚硅谷大数据Flink1.17实战教程从入门到精通_哔哩哔哩_bilibili 尚硅谷大数据Flink1.17实战教程-笔记01【Flink概述、Flink快速上手】尚硅谷大数据Flink1.17实战教程-笔记02【Flink部署】尚硅谷…...

【LeetCode每日一题合集】2023.7.3-2023.7.9

文章目录 2023.7.3——445. 两数相加 II&#xff08;大数相加/高精度加法&#xff09;2023.7.4——2679. 矩阵中的和2023.7.5——2600. K 件物品的最大和&#xff08;贪心&#xff09;代码1——贪心模拟代码2——Java一行 2023.7.6——2178. 拆分成最多数目的正偶数之和&#x…...

java企业工程项目管理系统平台源码

工程项目管理软件&#xff08;工程项目管理系统&#xff09;对建设工程项目管理组织建设、项目策划决策、规划设计、施工建设到竣工交付、总结评估、运维运营&#xff0c;全过程、全方位的对项目进行综合管理 工程项目各模块及其功能点清单 一、系统管理 1、数据字典&#…...

软件设计模式与体系结构-设计模式-行为型软件设计模式-访问者模式

目录 二、访问者模式概念代码类图实例一&#xff1a;名牌运动鞋专卖店销售软件实例二&#xff1a;计算机部件销售软优缺点适用场合课程作业 二、访问者模式 概念 对于系统中的某些对象&#xff0c;它们存储在同一个集合中&#xff0c;具有不同的类型对于该集合中的对象&#…...

【LeetCode】503. 下一个更大元素 II

503. 下一个更大元素 II&#xff08;中等&#xff09; 方法&#xff1a;单调栈 「 对于找最近一个比当前值大/小」的问题&#xff0c;都可以使用单调栈来解决。栈可以很好的保存原始位置&#xff0c;最近影射栈顶。题目要求更大&#xff0c;因此更大即解–出栈&#xff0c;更小…...

使用infura创建以太坊网络

创建账号 https://www.infura.io/zh 进入控制台Dashboard&#xff0c;选择CREATE API KEY 创建成功后&#xff0c;进入API KEY查看&#xff0c;使用PostMan测试 返回result即为当前区块。...

TCP/IP协议是什么?

78. TCP/IP协议是什么&#xff1f; TCP/IP协议是一组用于互联网通信的网络协议&#xff0c;它定义了数据在网络中的传输方式和规则。作为前端工程师&#xff0c;了解TCP/IP协议对于理解网络通信原理和调试网络问题非常重要。本篇文章将介绍TCP/IP协议的概念、主要组成部分和工…...

python图像处理实战(三)—图像几何变换

&#x1f680;写在前面&#x1f680; &#x1f58a;个人主页&#xff1a;https://blog.csdn.net/m0_52051577?typeblog &#x1f381;欢迎各位大佬支持点赞收藏&#xff0c;三连必回&#xff01;&#xff01; &#x1f508;本人新开系列专栏—python图像处理 ❀愿每一个骤雨初…...

学习vue2笔记

学习vue2笔记 文章目录 学习vue2笔记脚手架文件结构关于不同版本的Vuevue.config.js配置文件ref属性props配置项mixin(混入)插件scoped样式总结TodoList案例webStorage组件的自定义事件全局事件总线&#xff08;GlobalEventBus&#xff09;消息订阅与发布&#xff08;pubsub&am…...

【SQL】查找多个表中相同的字段

--查找字段所在 SELECTbb.TABLE_NAME,bb.COLUMN_NAME ,aa.COLUMN_ID,aa.DATA_TYPE,aa.DATA_LENGTH ,bb.COMMENTS FROMuser_tab_cols aa JOIN user_col_comments bb ONaa.TABLE_NAME bb.TABLE_NAMEAND aa.COLUMN_NAME bb.COLUMN_NAME JOIN dba_objects cc ONbb.TABLE_NAME cc…...

“未来之光:揭秘创新科技下的挂灯魅力“

写在前面&#xff1a; 高度信息化当下时代&#xff0c;对电脑及数字设备的需求与日俱增无处不在&#xff0c;随之而来的视觉疲劳和眼睛问题也攀升到了前所未有的高度。传统台灯对于长时间使用电脑的人群来说是完全无法解决这些问题的。一款ScreenBar Halo 屏幕挂灯&#xff0c;…...

Spring boot MongoDB实现自增序列

在某些特定的业务场景下&#xff0c;会需要使用自增的序列来维护数据&#xff0c;目前项目中因为使用MongoDB&#xff0c;顾记录一下如何使用MongoDB实现自增序列。 MongoDB自增序列原理 MongoDB本身不具有自增序列的功能&#xff0c;但是MongoDB的$inc操作是具有原子性的&…...

MyBatis查询数据库【秘籍宝典】

0.MyBatis执行流程1.第一个MyBatis查询1.创建数据库和表1.2.添加MyBatis框架依赖【新项目】1.3.添加MyBatis框架依赖【旧项目】1.4.配置连接数据库1.5.配置MyBatis的XML路径2.MyBatis模式开发2.1 添加MyBatis的xml配置 3.增查改删&#xff08;CRUD&#xff09;5.1.增加操作5.2.…...

目标检测舰船数据集整合

PS&#xff1a;大家如果有想要的数据集可以私信我&#xff0c;如果我下载了的话&#xff0c;可以发给你们~ 一、光学数据集 1、 DIOR 数据集(已下载yolo版本)&#xff08;论文中提到过&#xff09; DIOR由23463张最优遥感图像和190288个目标实例组成&#xff0c;这些目标实例用…...

7.4.分块查找

一.分块查找的算法思想&#xff1a; 1.实例&#xff1a; 以上述图片的顺序表为例&#xff0c; 该顺序表的数据元素从整体来看是乱序的&#xff0c;但如果把这些数据元素分成一块一块的小区间&#xff0c; 第一个区间[0,1]索引上的数据元素都是小于等于10的&#xff0c; 第二…...

大话软工笔记—需求分析概述

需求分析&#xff0c;就是要对需求调研收集到的资料信息逐个地进行拆分、研究&#xff0c;从大量的不确定“需求”中确定出哪些需求最终要转换为确定的“功能需求”。 需求分析的作用非常重要&#xff0c;后续设计的依据主要来自于需求分析的成果&#xff0c;包括: 项目的目的…...

蓝牙 BLE 扫描面试题大全(2):进阶面试题与实战演练

前文覆盖了 BLE 扫描的基础概念与经典问题蓝牙 BLE 扫描面试题大全(1)&#xff1a;从基础到实战的深度解析-CSDN博客&#xff0c;但实际面试中&#xff0c;企业更关注候选人对复杂场景的应对能力&#xff08;如多设备并发扫描、低功耗与高发现率的平衡&#xff09;和前沿技术的…...

【磁盘】每天掌握一个Linux命令 - iostat

目录 【磁盘】每天掌握一个Linux命令 - iostat工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景 注意事项 【磁盘】每天掌握一个Linux命令 - iostat 工具概述 iostat&#xff08;I/O Statistics&#xff09;是Linux系统下用于监视系统输入输出设备和CPU使…...

MODBUS TCP转CANopen 技术赋能高效协同作业

在现代工业自动化领域&#xff0c;MODBUS TCP和CANopen两种通讯协议因其稳定性和高效性被广泛应用于各种设备和系统中。而随着科技的不断进步&#xff0c;这两种通讯协议也正在被逐步融合&#xff0c;形成了一种新型的通讯方式——开疆智能MODBUS TCP转CANopen网关KJ-TCPC-CANP…...

工业自动化时代的精准装配革新:迁移科技3D视觉系统如何重塑机器人定位装配

AI3D视觉的工业赋能者 迁移科技成立于2017年&#xff0c;作为行业领先的3D工业相机及视觉系统供应商&#xff0c;累计完成数亿元融资。其核心技术覆盖硬件设计、算法优化及软件集成&#xff0c;通过稳定、易用、高回报的AI3D视觉系统&#xff0c;为汽车、新能源、金属制造等行…...

微信小程序云开发平台MySQL的连接方式

注&#xff1a;微信小程序云开发平台指的是腾讯云开发 先给结论&#xff1a;微信小程序云开发平台的MySQL&#xff0c;无法通过获取数据库连接信息的方式进行连接&#xff0c;连接只能通过云开发的SDK连接&#xff0c;具体要参考官方文档&#xff1a; 为什么&#xff1f; 因为…...

佰力博科技与您探讨热释电测量的几种方法

热释电的测量主要涉及热释电系数的测定&#xff0c;这是表征热释电材料性能的重要参数。热释电系数的测量方法主要包括静态法、动态法和积分电荷法。其中&#xff0c;积分电荷法最为常用&#xff0c;其原理是通过测量在电容器上积累的热释电电荷&#xff0c;从而确定热释电系数…...

七、数据库的完整性

七、数据库的完整性 主要内容 7.1 数据库的完整性概述 7.2 实体完整性 7.3 参照完整性 7.4 用户定义的完整性 7.5 触发器 7.6 SQL Server中数据库完整性的实现 7.7 小结 7.1 数据库的完整性概述 数据库完整性的含义 正确性 指数据的合法性 有效性 指数据是否属于所定…...

【笔记】WSL 中 Rust 安装与测试完整记录

#工作记录 WSL 中 Rust 安装与测试完整记录 1. 运行环境 系统&#xff1a;Ubuntu 24.04 LTS (WSL2)架构&#xff1a;x86_64 (GNU/Linux)Rust 版本&#xff1a;rustc 1.87.0 (2025-05-09)Cargo 版本&#xff1a;cargo 1.87.0 (2025-05-06) 2. 安装 Rust 2.1 使用 Rust 官方安…...