.net core 下使用 RabbitMQ 设置1次消费1条消息 (六)

主要设置

设置prefetchCount=1,来告知RabbitMQ,在未收到消费端的消息确认时,不再分发消息,也就确保了当消费端处于忙碌状态时,不再分配任务。
channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

 

 1  public static void ReceiveMessage()
 2         {
 3             string queueName = "Worker_Queue";
 4             var connection = RabbitMQHelper.GetConnection();
 5             {
 6                 var channel = connection.CreateModel();
 7                 {
 8                     channel.QueueDeclare(queueName, false, false, false, null);
 9                     var consumer = new EventingBasicConsumer(channel);
10                     //设置prefetchCount : 1来告知RabbitMQ,在未收到消费端的消息确认时,不再分发消息,也就确保了当消费端处于忙碌状态时,不再分配任务。
11                     channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); //能者多劳
12                     consumer.Received +=(model, ea) => {
13                         var message = Encoding.UTF8.GetString(ea.Body.ToArray());
14                         Console.WriteLine(" Worker Queue Received => {0}", message);
15                     }; 
16                     channel.BasicConsume(queueName,true, consumer);
17                 }
18                
19             }
20           
21         } 

 

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

导航