c# Dead letter queue demo

Write a custom catalog title here

c# dead letter queue demo

What is a dead letter queue? For example, set the expiration time of the task and put it in a queue, wait until the expiration time is up to pass the switch, and put it in another consumption queue. This chestnut is just an application of the dead letter queue. The principle is this: there are two queues, one is used to set the expiration condition (it can be a single message setting, or the entire queue setting), and the other is used Accept messages that have expired and configure the switch to accept them.

What can the dead letter queue do? First of all, we understand the principle of the dead letter queue, then we can use its characteristics. Such as the delayed cancellation of the order business, the automatic cancellation of the timeout without delivery, the notification reminder half an hour before the order cancellation, etc., there are places with delayed tasks.

Let's look at the demo producer

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mqquartz
{
    class Program
    {
        private static string USER_NAME = "guest";//用户名
        private static string PASSWORD = "guest";//密码

        //队列名称
        //****==================订单延时队列=======================*****//
        //订单延时队列
        private static string DELAY_QUEUE_NAME = "delay-queue-orderOverTime";
        //订单延时消费队列
        private static string CONSUME_QUEUE_NAME = "consume-queue-orderOverTime";
        //订单延时队列死信交换的交换器名称
        private static string EXCHANGENAME = "exchange-orderOverTime";
        //订单延时队列死信的交换器路由key
        private static string ROUTINGKEY = "routingKey-orderOverTime";

        static void Main(string[] args)
        {

            var RAB_FACTORY = new ConnectionFactory();
            RAB_FACTORY.HostName = "127.0.0.1";
            RAB_FACTORY.Port = 5672;
            RAB_FACTORY.UserName = USER_NAME;
            RAB_FACTORY.Password = PASSWORD;

            using (var connection = RAB_FACTORY.CreateConnection())
            {
                var delayChannel = connection.CreateModel();//延时队列连接通道
                var consumerChannel = connection.CreateModel();//消费队列连接通道
                consumerChannel.ExchangeDeclare(EXCHANGENAME, "direct");//创建交换器
                Dictionary<string, object> arg = new Dictionary<string, object>();
                //配置死信交换器
                arg.Add("x-dead-letter-exchange", EXCHANGENAME); //交换器名称
                //死信交换路由key (交换器可以将死信交换到很多个其他的消费队列,可以用不同的路由key 来将死信路由到不同的消费队列去)
                arg.Add("x-dead-letter-routing-key", ROUTINGKEY);
                delayChannel.QueueDeclare(DELAY_QUEUE_NAME, true, false, false, arg);

                /**创建消费队列*/
                consumerChannel.QueueDeclare(CONSUME_QUEUE_NAME, true, false, false, null);
                //参数1:绑定的队列名  参数2:绑定至哪个交换器  参数3:绑定路由key
                consumerChannel.QueueBind(CONSUME_QUEUE_NAME, EXCHANGENAME, ROUTINGKEY);
                //最多接受条数 0为无限制,每次消费消息数(根据实际场景设置),true=作用于整channel,false=作用于具体的消费者
                consumerChannel.BasicQos(0, 10, false);
                int i = 20;
                while (i>0)
                {
                    var body = Encoding.UTF8.GetBytes("你牛逼奥!"+i);
                    var properties = delayChannel.CreateBasicProperties();
                    properties.DeliveryMode = 2;
                    properties.Expiration = "15000";
                    delayChannel.BasicPublish("",DELAY_QUEUE_NAME,properties,body);
                    i--;
                }
            }
        }
    }
}

consumer

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace consumer
{
    class Program
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory();
            factory.HostName = "localhost";
            factory.UserName = "guest";
            factory.Password = "guest";

            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare("consume-queue-orderOverTime", true, false, false, null);

                    var consumer = new EventingBasicConsumer(channel);
                    channel.BasicConsume("consume-queue-orderOverTime", false, consumer);
                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine("已接收: {0}", message);
                        channel.BasicAck(ea.DeliveryTag, false);
                    };
                    consumer.HandleBasicConsumeOk("ok");
                    Console.ReadLine();
                }
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_29798755/article/details/96474507