RabbitMQ 设置消息的TTL(过期时间)

我们在RabbitMQ中发布消息时,在代码中有两种方法设置某个队列的消息过期时间:

1、针对队列来说,可以使用x-message-ttl参数设置当前队列中所有消息的过期时间,即当前队列中所有的消息过期时间都一样;

2、针对单个消息来说,在发布消息时,可以使用Expiration参数来设置单个消息的过期时间。

以上两个参数的单位都是毫秒,即1000毫秒为1秒。如果以上两个都设置,则以当前消息最短的那个过期时间为准。

接下来让我们在在代码中相见!

针对队列来说:

//首先创建一个连接工厂对象
var factory = new ConnectionFactory() { HostName = "localhost", UserName = "yyt", Password = "yyt888888",VirtualHost="log" };
//然后使用工厂对象创建一个TCP连接
using (var connection = factory.CreateConnection()){
    //在当前连接上创建一根通信的虚拟管道
    using (var channel = connection.CreateModel()) {      
            //声明一个交换机
            channel.ExchangeDeclare("e.log", "direct");
            //声明一个队列,设置arguments的参数x-message-ttl为10000毫秒
            channel.QueueDeclare(queue: "q.log.error",
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: new Dictionary<string, object> {                                       
                                         { "x-message-ttl",10000} //x-message-ttl即设置当前队列消息的过期时间。ttl即为time to live
                                 }); 
              
            channel.QueueBind("q.log.error", //队列名称
                              "e.log",      //交换机名称
                              "log.error");  //自定义的RoutingKey
                
            var body = Encoding.UTF8.GetBytes("测试消息");
            var properties = channel.CreateBasicProperties();
            //设置消息持久化
            properties.SetPersistent(true);
            
            //发布消息
            channel.BasicPublish(exchange: "e.log",
                                 routingKey: "log.error",
                                 basicProperties: properties,
                                 body: body);
        
    }
}

针对的单个消息来说:

//首先创建一个连接工厂对象
var factory = new ConnectionFactory() { HostName = "localhost", UserName = "yyt", Password = "yyt888888",VirtualHost="log" };
//然后使用工厂对象创建一个TCP连接
using (var connection = factory.CreateConnection()){
    //在当前连接上创建一根通信的虚拟管道
    using (var channel = connection.CreateModel()) {      
            //声明一个交换机
            channel.ExchangeDeclare("e.log", "direct");
            //声明一个队列,设置arguments的参数x-message-ttl为10000毫秒
                channel.QueueDeclare(queue: "q.log.error",
                                     durable: false,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: new Dictionary<string, object> {                                       
                                         //{ "x-message-ttl",10000} //x-message-ttl即设置当前队列消息的过期时间。ttl即为time to live
                                     }); 
              
                channel.QueueBind("q.log.error", //队列名称
                                  "e.log",      //交换机名称
                                  "log.error");  //自定义的RoutingKey
                                  
                var body = Encoding.UTF8.GetBytes("测试消息");
                var properties = channel.CreateBasicProperties();
                //设置消息持久化
                properties.SetPersistent(true);
               
                //设置当个消息的过期时间为5000毫秒
                properties.Expiration = "5000";
                
                channel.BasicPublish(exchange: "e.log",
                                     routingKey: "log.error",
                                     basicProperties: properties,
                                     body: body);
        
    }
}

参考信息:https://www.rabbitmq.com/ttl.html

原文地址:https://www.cnblogs.com/williamwsj/p/8108637.html