.net core 下使用 RabbitMQ 延迟队列 (三)

延迟队列,跟死信队列差不多

队列设置及参数:

参数名 类型 作用
x-message-tti(Time-To-Live) int,毫秒 对消息设置预期的时间,过期将被丢弃
x-max-length int 限制队列最大长度,个数,新增后删除最早的
x-expires int,毫秒 队列没有访问超时时,自动删除时间
x-max-length-bytes int 限制队列最大容量
x-dead-letter-exchange String 指定死信交换机
x-dead-letter-routing-key String 死信路由,指定routingKey
x-max-priority int 队列优先级
x-queue-mode 默认"lazy" 对列模式,默认lazy(将数据放入磁盘,消费时放入内存)
x-queue-master-locator QueueBuilder.MasterLocator 主队列选择策略,min-masters:选择承载最小绑定主机数量的节点,client-local:选择客户机声明队列连接到的节点,min-masters:随机选择一个节点

发送消息

 1  /// <summary>
 2         /// 建立连接实现了多线程建立
 3         /// </summary>
 4         public void Connection()
 5         {
 6             if (_connection == null)
 7             {
 8                 lock (_lock)
 9                 {
10                     var connectionFactory = new ConnectionFactory()
11                     {
12                         HostName = _options.HostName,
13                         VirtualHost = _options.VirtualHost,
14                         Password = _options.Password,
15                         UserName = _options.UserName,
16                         Port = _options.Port
17                     };
18                     this._connection = connectionFactory.CreateConnection();
19                 }
20             }
21         }

 

 1  /// <summary>
 2         /// 发送延迟消息
 3         /// </summary>
 4         /// <param name="msg"></param>
 5         public void SendDelayMsg(string msg)
 6         {
 7             //建立连接
 8             Connection();
 9             //建立信道
10             var channel = _connection.CreateModel();
11             Dictionary<string, object> dic = new Dictionary<string, object>();
12             //  dic.Add("x-expires", 30000); // 30秒后队列自动干掉
13             dic.Add("x-message-ttl", 12000);//队列上消息过期时间,应小于队列过期时间  
14             dic.Add("x-dead-letter-exchange", "exchange-direct");//过期消息转向路由  
15             dic.Add("x-dead-letter-routing-key", "routing-delay");//过期消息转向路由相匹配routingkey  
16             channel.ExchangeDeclare("exchange_delay", ExchangeType.Direct,true,false,null);
17             channel.QueueDeclare(queue: "delay_queue",
18                 durable: true,
19                 exclusive: false,
20                 autoDelete: false,
21                 arguments: dic);
22             channel.QueueBind(queue: "delay_queue", "exchange_delay", "delay_route");
23             var message = "Hello World!";
24             var body = Encoding.UTF8.GetBytes(message);
25             //向该消息队列发送消息message
26             channel.BasicPublish(exchange: "",routingKey: "delay_queue",basicProperties: null, body: body);
27             Console.WriteLine(" [x] Sent {0}", message);
28 
29         }

 

接收消息

 1   /// <summary>
 2         /// 接收延迟队列
 3         /// </summary>
 4         public void ReceiveDelay(Func<string, bool> func)
 5         {
 6             //建立连接
 7             Connection();
 8             //建立信道
 9             IModel channel = this._connection.CreateModel();
10             channel.ExchangeDeclare(exchange: "exchange-direct", type: "direct",true,false,null);
11             channel.QueueDeclare("exchange-direct-queue",true,false,false,null);
12             channel.QueueBind(queue: "exchange-direct-queue", exchange: "exchange-direct", routingKey: "routing-delay");
13 
14             //回调,当consumer收到消息后会执行该函数
15             var consumer = new EventingBasicConsumer(channel);
16             consumer.Received += (model, ea) =>
17             {
18                 var body = ea.Body.ToArray();
19                 var message = Encoding.UTF8.GetString(body);
20                 Console.WriteLine(ea.RoutingKey);
21                 Console.WriteLine(" [x] Received {0}", message);
22             };
23 
24             //Console.WriteLine("name:" + name);
25             //消费队列"hello"中的消息
26             channel.BasicConsume(queue: "exchange-direct-queue",
27                                  autoAck: true,
28                                  consumer: consumer);
29 
30             Console.WriteLine(" Press [enter] to exit.");
31         }

 

posted on 2023-02-01 00:01  是水饺不是水饺  阅读(125)  评论(0编辑  收藏  举报

导航