RabbitMQ(高级特性)设置单条消息存活时间

设置单条消息存活时间

java 复制代码
@Test
public void testSendMessage() {
  //设置消息属性
  MessageProperties messageProperties = new MessageProperties();
  //设置存活时间
  messageProperties.setExpiration("10000");
  // 创建消息对象
  Message message = new Message("send message...".getBytes(StandardCharsets.UTF_8), messageProperties);
  // 发送消息
  rabbitTemplate.convertAndSend("my_topic_exchange", "my_routing", message);
}

注意:

1 如果设置了单条消息的存活时间,也设置了队列的存活时间,以时间短的为准。

2 消息过期后,并不会马上移除消息,只有消息消费到队列顶端时,才会移除该消息。

第二条测试:

java 复制代码
@Test
public void testSendMessage2() {
  for (int i = 0; i < 10; i++) {
    if (i == 5) {
      // 1.创建消息属性
      MessageProperties messageProperties = new MessageProperties();
      // 2.设置存活时间
      messageProperties.setExpiration("10000");
      // 3.创建消息对象
      Message message = new Message(("send message..." + i).getBytes(), messageProperties);
      // 4.发送消息
      rabbitTemplate.convertAndSend("my_topic_exchange", "my_routing", message);
     } else {
      rabbitTemplate.convertAndSend("my_topic_exchange", "my_routing", "send message..." + i);
     }
   }
}

在以上案例中,i=5的消息才有过期时间,10s后消息并没有马上被移除,但该消息已经不会被消费了,当它到达队列顶端时会被移除。

相关推荐
weixin_468466858 分钟前
Data-Engineering-Zoomcamp 新手实战指南
python·自动化·pandas·编程·数据处理
sleven fung13 分钟前
Milvus 向量数据库
开发语言·数据库·python·langchain·milvus
aqi0024 分钟前
15天学会AI应用开发(三)把历史对话作为提示词会怎样
人工智能·python·大模型·ai编程·ai应用
大数据魔法师24 分钟前
Streamlit(十八)- API 参考文档(十一)- 页面导航组件
python·web
weixin_4684668530 分钟前
数据高效处理实战:从痛点解决到价值落地
大数据·python·自动化·数据处理
hui函数1 小时前
Python系列Bug修复|如何解决 pip install 报错 ModuleNotFoundError: No module named ‘pygame’ 问题
python·bug·pip
xcLeigh1 小时前
Python入门:Python3 operator模块全面学习教程
开发语言·python·学习·教程·python3·operator
xcLeigh1 小时前
Python小游戏实战:实现2048游戏小游戏附源码
python·游戏·教程·pygame·2048·python3
大叔带刺1 小时前
使用python创建自己的专属星座签名APP:Name2Constell
开发语言·python·pygame