本文主要講解如何通過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環境
如有哪裡講得不是很明白或是有錯誤,歡迎指正
如您喜歡的話不妨點個贊收藏一下吧?