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);
        
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值