文章插图
设定消费者端第五十条消息会被拒收,且队列的TTL设置为5秒 。
//死信交换机和死信队列var dlxExchangeName = "dlx_exchange";channel.ExchangeDeclare(exchange: dlxExchangeName, type: "fanout", durable: false, autoDelete: false, arguments: null);var dlxQueueName = "dlx_queue";channel.QueueDeclare(queue: dlxQueueName, durable: false, exclusive: false, autoDelete: false, arguments: null);channel.QueueBind(queue: dlxQueueName, exchange: dlxExchangeName, routingKey: "");//常规队列var queueName = "normalmessage_queue";var arguments = new Dictionary<string, object>{ { "x-message-ttl", 5000}, { "x-dead-letter-exchange", dlxExchangeName }};channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: arguments);channel.BasicQos(0, 5, false);var consumer = new EventingBasicConsumer(channel);consumer.Received += (model, ea) =>{ var message = ea.Body; Console.WriteLine("接收到信息为:" + Encoding.UTF8.GetString(message.ToArray())); if (Encoding.UTF8.GetString(message.ToArray()).Contains("50")) { Console.WriteLine($"拒收 {DateTime.Now}"); ((EventingBasicConsumer)model).Model.BasicReject(ea.DeliveryTag, requeue: true); return; } ((EventingBasicConsumer)model).Model.BasicAck(ea.DeliveryTag, multiple: false);};channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer);
当消费者端拒收消息后消息重入队列,再次消费,反复进行超过5秒后 , 消息在队列中达到了过期时间,则被挪入到死信队列中 。文章插图
从Web管理中死信队列中可查看该条过期的消息 。
文章插图
参考资料
- https://www.jianshu.com/p/f77a0b10c140
- https://www.jianshu.com/p/4904c609632f
- https://stackoverflow.com/questions/23158310/how-do-i-set-a-number-of-retry-attempts-in-rabbitmq
2022-10-29,望技术有成后能回来看见自己的脚步
推荐阅读
- IQueryable和IEnumerable 快读《ASP.NET Core技术内幕与项目实战》EFCore2.5:集合查询原理揭秘
- 「MySQL高级篇」MySQL锁机制 && 事务
- <五>掌握左值引用和初识右值引用
- .NET6打包部署到Windows Service
- Linux软件安装方式 - Tarball&RPM&YUM
- <四>1:全面掌握Const的用法
- Dapr实现.Net Grpc服务之间的发布和订阅,并采用WebApi类似的事件订阅方式
- 是什么让.NET7的Min和Max方法性能暴增了45倍?
- 前端性能优化——首屏时间&&白屏时间
- 19 基于.NetCore开发博客项目 StarBlog - Markdown渲染方案探索