当前位置:网站首页>延时队列优化 (2)
延时队列优化 (2)
2022-07-30 18:28:00 【一个风轻云淡】
在这里新增了一个队列QC,绑定关系如下,该队列不设置TTL时间

配置类文件:
@Bean("queueC")
public Queue queueC()
{
HashMap<String, Object> args = new HashMap<String, Object>();
args.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
args.put("x-dead-letter-routing-key","YD");
//在这里没有声明TTL属性
return QueueBuilder.durable(QUEUE_C).withArguments(args).build();
}
@Bean
public Binding queueBindingX(@Qualifier("queueC") Queue queueC,@Qualifier("xExchange") DirectExchange exchange)
{
return BindingBuilder.bind(queueC).to(exchange).with("XC");
}生产者:
@Slf4j
@RequestMapping("ttl")
@RestController
public class Produce01 {
@Autowired
private RabbitTemplate rabbitTemplate;
// @GetMapping("sendMsg/{message}")
// public void sendMsg(@PathVariable String message)
// {
//
// log.info("当前时间:{},发送一条消息给俩个TTL队列:{}", new Date(),message);
// rabbitTemplate.convertAndSend("X","XA","消息来于10s"+message);
// rabbitTemplate.convertAndSend("X","XB","消息来于40s"+message);
// }
@GetMapping("sendExpirationMsg/{message}/{ttlTime}")
public void senMsg(@PathVariable String message,@PathVariable String ttlTime)
{
log.info("当前时间:{},发送一条时长{}毫秒TTL信息给队列C:{}", new Date(),ttlTime, message);
rabbitTemplate.convertAndSend("X","XC",message, correlationData ->{
correlationData.getMessageProperties().setExpiration(ttlTime);
return correlationData;
});
}
}
消费者:
@Slf4j
@Component
public class Consumer {
@RabbitListener(queues = "QD")
public void receiveD(Message message, Channel channel)
{
String s = new String(message.getBody());
log.info("当前时间{},死信队列D接收到消息---->{}",new Date(),s);
}
}
![]()
看起来似乎没什么问题,但是在最开始的时候,就介绍过如果使用在消息属性上设置TTL的方式,消息可能并不会按时“死亡“,因为RabbitMQ只会检查第一个消息是否过期,如果过期则丢到死信队列,
如果第一个消息的延时时长很长,而第二个消息的延时时长很短,第二个消息并不会优先得到执行。
边栏推荐
猜你喜欢

Graphic LeetCode -- 11. Containers of most water (difficulty: medium)

Redis for infrastructure

EMC VPLEX VS2 SPS电池更换详细探讨

while,do while,for循环语句

CCNA-网络汇总 超网(CIDR) 路由最长掩码匹配

AI基础:图解Transformer

OSPF详解(3)

博纳影通过IPO注册:阿里腾讯是股东 受疫情冲击明显

natural language processing nltk

The large-scale application of artificial intelligence AI products in industrial-grade mature shipping ports of CIMC World Lianda will create a new generation of high-efficiency smart ports and innova
随机推荐
6块钱1斤,日本公司为何来中国收烟头?
ByteArrayInputStream 类源码分析
二分答案裸题(加一点鸽巢原理)
5分钟搞懂MySQL - 行转列
Codeblocks + Widgets create window code analysis
「Redis应用与深度实践笔记」,深得行业人的心,这还不来看看?
OneFlow源码解析:Op、Kernel与解释器
原生js系列
千亿级、大规模:腾讯超大 Apache Pulsar 集群性能调优实践
微博广告分布式配置中心的构建与实践(有彩蛋)
core sound driver详解
ESP8266-Arduino programming example-HC-SR04 ultrasonic sensor driver
Scrapy framework is introduced
Recommended Books | Recommend 3 database books with rave reviews
CCNA-子网划分(VLSM)
【HarmonyOS】【FAQ】鸿蒙问题合集4
A senior with 13 years of experience in software testing, summed up 5 test employment suggestions....
The use of terminal split screen tool Terminalx
【HarmonyOS】【FAQ】鸿蒙问题合集3
Fixed asset visualization intelligent management system