您好,登錄后才能下訂單哦!
小編給大家分享一下C#通過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)
以上是“C#通過rabbitmq如何實現定時任務”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。