C#通過rabbitmq實現定時任務(延時隊列)
本文主要講解如何通過RabbitMQ實現定時任務(延時隊列)
環境準備
需要在MQ中進行安裝插件 地址鏈接
插件介紹地址:https://www.rabbitmq.com/blog/2015/04/16/scheduling-messages-with-rabbitmq/
使用場景
作為一個新的預支付訂單被初始化放置,如果該訂單在指定時間內未進行支付,則將被認為超時訂單進行關閉處理;電商系統中應用較多,用戶購買商品產生訂單,但未進行支付,訂單產生30分鐘內未支付將關閉訂單(且滿足該場景數量龐大),不可能采用人工幹預。
代碼介紹
生產者
var factory = new ConnectionFactory() { Uri = new Uri("MQ地址") }; using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); var exchangeName = "delay-exchange"; var routingkey = "delay.delay"; var queueName = "delay_queueName"; //設置Exchange隊列類型 var argMaps = new Dictionary<string, object>() { {"x-delayed-type", "topic"} }; //設置當前消息為延時隊列 channel.ExchangeDeclare(exchange: exchangeName, type: "x-delayed-message", true, false, argMaps); channel.QueueDeclare(queueName, true, false, false, argMaps); channel.QueueBind(queueName, exchangeName, routingkey); for (int i = 0; i < 3; i++) { var time = 1000 * 5; var message = $@"發送時間為 {DateTime.Now:yyyy-MM-dd HH:mm:ss} 延時時間為:{time}"; var body = Encoding.UTF8.GetBytes(message); var props = channel.CreateBasicProperties(); //設置消息的過期時間 props.Headers = new Dictionary<string, object>() { { "x-delay", 5000 } }; channel.BasicPublish(exchange: exchangeName, routingKey: routingkey, basicProperties: props, body: body); Console.WriteLine(message); } Console.ReadLine();
消費者(自動綁定隊列寫法)
var factory = new ConnectionFactory() { Uri = new Uri(MQ地址) }; using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); var queueName = "delay_queueName"; channel.QueueDeclare(queueName, true, false, false, null); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; var message = Encoding.UTF8.GetString(body); var routingKey = ea.RoutingKey; Console.WriteLine($@"接受到消息的時間為 {DateTime.Now:yyyy-MM-dd HH:mm:ss},routingKey:{routingKey} message:{message} "); }; channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer); Console.ReadLine();
消費者(手動綁定隊列寫法)
var factory = new ConnectionFactory() { Uri = new Uri(MQ地址) }; using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); var exchangeName = "delay-exchange"; var routingkey = "delay.delay"; var queueName = "delay_queueName"; var autoDelete = true; var argMaps = new Dictionary<string, object>() { {"x-delayed-type", "topic"} }; channel.ExchangeDeclare(exchange: exchangeName, type: "x-delayed-message", true, false, argMaps); channel.QueueDeclare(queueName, true, false, false, argMaps); channel.QueueBind(queue: queueName, exchange: exchangeName, routingKey: routingkey); //channel.QueueDeclare(queueName, true, false, false, null); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; var message = Encoding.UTF8.GetString(body); var routingKey = ea.RoutingKey; Console.WriteLine($@"接受到消息的時間為 {DateTime.Now:yyyy-MM-dd HH:mm:ss},routingKey:{routingKey} message:{message} "); }; channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer); Console.ReadLine();
最終實現效果(兩個消費者)
在上述實現中,其實主要靠以下參數來幫我們實現當前功能
聲明Exchange中的 type: “x-delayed-message” 這個表明當前隊列為延時消息隊列
聲明Exchange中arguments中的 {“x-delayed-type”, “topic”} 當前表明當前隊列為Topic模式
最後 我們在CreateBasicProperties的Header中設置 { “x-delay”, 5000 }來達到消息延時的功能(單位為ms)
建議
如果使用當前模式來做定時任務,在要求消息不丟失的前提下,需要運維同學提供穩定的MQ環境
到此這篇關於C#通過rabbitmq實現定時任務(延時隊列)的文章就介紹到這瞭,更多相關C# rabbitmq定時任務內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- .Net Core和RabbitMQ限制循環消費的方法
- 關於golang監聽rabbitmq消息隊列任務斷線自動重連接的問題
- rabbitmq中routingkey的作用說明
- C#利用RabbitMQ實現點對點消息傳輸
- RabbitMQ 延遲隊列實現訂單支付結果異步階梯性通知(實例代碼)