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

RabbitMQ设置消息过期时间

RabbitMQ设置消息过期时间

  • 1、过期消息(死信)
  • 2、设置消息过期的两种方式
    • 2.1、设置单条消息的过期时间
      • 2.1.1、配置文件application.yml
      • 2.1.2、配置类RabbitConfig
      • 2.1.3、发送消息业务类service(核心代码)
      • 2.1.4、启动类
      • 2.1.5、依赖文件pom.xml
      • 2.1.6、测试
    • 2.2、通过队列属性设置消息过期时间
      • 2.1.1、配置文件application.yml
      • 2.1.2、配置类RabbitConfig(核心代码)
      • 2.2.3、发送消息业务类service
      • 2.2.4、启动类
      • 2.2.5、依赖文件pom.xml
      • 2.2.6、测试

1、过期消息(死信)

过期消息也叫TTL消息,TTL:Time To Live
消息的过期时间有两种设置方式:设置单条消息的过期时间通过队列属性设置消息过期时间

2、设置消息过期的两种方式

队列的过期时间决定了在没有任何消费者的情况下,队列中的消息可以存活多久;
注意事项:如果消息和对列都设置过期时间,则消息的TTL以两者之间较小的那个数值为准。

2.1、设置单条消息的过期时间

在这里插入图片描述

2.1.1、配置文件application.yml

server:port: 8080
spring:application:name: ttl-test01rabbitmq:host: 你的服务器IPport: 5672username: 你这账号password: 你的密码virtual-host: powermy:exchangeName: exchange.ttl.aqueueName: queue.ttl.a

2.1.2、配置类RabbitConfig

package com.power.config;import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RabbitConfig {@Value("${my.exchangeName}")private String exchangeName;@Value("${my.queueName}")private String queueName;//创建交换机@Beanpublic DirectExchange directExchange(){return ExchangeBuilder.directExchange(exchangeName).build();}//创建队列@Beanpublic Queue queue(){return QueueBuilder.durable(queueName).build();}@Beanpublic Binding binding(DirectExchange exchangeName,Queue queueName){return BindingBuilder.bind(queueName).to(exchangeName).with("info");}
}

2.1.3、发送消息业务类service(核心代码)

package com.power.service;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.Date;@Service
@Slf4j
public class MessageService {@Resourceprivate RabbitTemplate rabbitTemplate;@Beanpublic void sendMsg(){//设置消息过期的核心代码MessageProperties messageProperties = new MessageProperties();messageProperties.setExpiration("30000");//设置消息过期时间Message message = MessageBuilder.withBody("hello world".getBytes()).andProperties(messageProperties).build();rabbitTemplate.convertAndSend("exchange.ttl.a","info",message);log.info("消息发送完毕,发送时间是:"+new Date());}
}

2.1.4、启动类

package com.power;import com.power.service.MessageService;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;import javax.annotation.Resource;@SpringBootApplication
public class Application implements ApplicationRunner {@Resourceprivate MessageService messageService;public static void main(String[] args) {SpringApplication.run(Application.class);}@Overridepublic void run(ApplicationArguments args) throws Exception {messageService.sendMsg();}
}

2.1.5、依赖文件pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.power</groupId><artifactId>rabbit_05_ttl01</artifactId><version>1.0-SNAPSHOT</version><name>rabbit_05_ttl01</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.13</version><relativePath/></parent><dependencies><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><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2.1.6、测试

启动项目:
在这里插入图片描述
登录rabbitmq后台:
在消息有效期内,可以获取到消息。

在这里插入图片描述
超过消息有效期,获取不到消息。
在这里插入图片描述

2.2、通过队列属性设置消息过期时间

在这里插入图片描述

2.1.1、配置文件application.yml

server:port: 8080
spring:application:name: ttl-test02rabbitmq:host: 你的服务器IPport: 5672username: 你这账号password: 你的密码virtual-host: powermy:exchangeName: exchange.ttl.bqueueName: queue.ttl.b

2.1.2、配置类RabbitConfig(核心代码)

package com.power.config;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;@Configuration
public class RabbitConfig {@Value("${my.exchangeName}")private String exchangeName;@Value("${my.queueName}")private String queueName;//创建交换机@Beanpublic DirectExchange directExchange(){return ExchangeBuilder.directExchange(exchangeName).build();}//创建队列@Beanpublic Queue queue(){//设置队列消息的过期时间,超过这个有效期,队列内的所有消息都会过期//方式1:new Queue的方式Map<String, Object> arguments = new HashMap<>();arguments.put("x-message-ttl",30000);return new Queue(queueName,true,false,false,arguments);//        方式2:建造者方式
//        return QueueBuilder.durable(queueName).withArguments(arguments).build();}@Beanpublic Binding binding(DirectExchange exchangeName,Queue queueName){return BindingBuilder.bind(queueName).to(exchangeName).with("info");}
}

2.2.3、发送消息业务类service

package com.power.service;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.Date;@Service
@Slf4j
public class MessageService {@Resourceprivate RabbitTemplate rabbitTemplate;@Beanpublic void sendMsg(){Message message = MessageBuilder.withBody("hello world".getBytes()).build();rabbitTemplate.convertAndSend("exchange.ttl.b","info",message);log.info("消息发送完毕,发送时间是:"+new Date());}
}

2.2.4、启动类

package com.power;import com.power.service.MessageService;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;import javax.annotation.Resource;@SpringBootApplication
public class Application implements ApplicationRunner {@Resourceprivate MessageService messageService;public static void main(String[] args) {SpringApplication.run(Application.class);}@Overridepublic void run(ApplicationArguments args) throws Exception {messageService.sendMsg();}
}

2.2.5、依赖文件pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.power</groupId><artifactId>rabbit_05_ttl02</artifactId><version>1.0-SNAPSHOT</version><name>rabbit_05_ttl02</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.13</version><relativePath/></parent><dependencies><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><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2.2.6、测试

启动项目:
在这里插入图片描述登录后台查看

在这里插入图片描述

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

相关文章:

  • 大数据-209 数据挖掘 机器学习理论 - 梯度下降 梯度下降算法调优
  • 粒子群优化双向深度学习!PSO-BiTCN-BiGRU-Attention多输入单输出回归预测
  • 排序算法简介
  • (没有跳过联网激活)导致使用微软账号激活电脑---修改为本地账户和英文名字
  • [论文粗读][REALM: Retrieval-Augmented Language Model Pre-Training
  • flink 内存配置(五):网络缓存调优
  • set和map的使用
  • LCL三相并网逆变器simulink仿真+说明文档
  • 从0开始深度学习(24)——填充和步幅
  • CPU Study - Instructions Fetch
  • GJ Round (2024.9) Round 1~7
  • 【CMCL】多模态情感识别的跨模态对比学习
  • 输入/输出系统
  • asp.net+uniapp养老助餐管理系统 微信小程序
  • 部署istio应用未能产生Envoy sidecar代理
  • 使用YOLO 模型进行线程安全推理
  • ABAP 增强
  • vue使用方法创建组件
  • HTML 基础标签——链接标签 <a> 和 <iframe>
  • @SpringBootApplication源码解析
  • 【实战篇】requests库 - 有道云翻译爬虫 【附:代理IP的使用】
  • 法语动词变位
  • Excel:vba实现批量插入图片
  • Vue3的router和Vuex的学习笔记整理
  • 设置JAVA以适配华为2288HV2服务器的KVM控制台
  • 掌握Qt调试技术
  • 使用NVM自由切换nodejs版本
  • 同三维T610UHK USB单路4K60采集卡
  • Git超详细笔记包含IDEA整合操作
  • 摩尔线程嵌入式面试题及参考答案(2万字长文)