C#之RabbitMQ系列(三)--Work Queues

7 篇文章 0 订阅
2 篇文章 0 订阅

工作队列

使用场景:Work Queue被用以处理大量耗时任务,与其等待任务处理完毕耗费大量cpu资源,还不如立即返回并交由代理worker随后处理message。

消息持久化

生产者和消费者的代码和上一节Publish-Consumer基本相同,唯一不同的是配置项的参数调整。代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using RabbitMQ.Client;

namespace NewTask
{
    class NewTask
    {
        public static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: "task_queue",
                                     durable: true,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: null);
                //At this point we're sure that the task_queue queue won't be lost even if RabbitMQ restarts. Now we need to mark our messages as persistent - by setting IBasicProperties.SetPersistent to true.
                var properties = channel.CreateBasicProperties();
                //properties.SetPersistent(true);//此方法已经过时:“这种设置方法已经被摒弃,现在使用持久化属性取代它”
                properties.Persistent = true;//取代上面的写法

                ...
        }
    }
}

队列持久化通过durable: true声明,服务重启后队列依然存在,但如果声明为排他队列exclusive: true,则不受持久化影响,连接断开即移除。
消息持久化通过properties.Persistent = true来设置,前提是队列持久化,否则服务宕掉后消息肯定丢失,因为消息的载体队列都没了。

消息回执

在一些对准确度要求比较高的场景下时,我们可能需要收到从消费者传回的ack后才从队列删除。

  • 收不到ack,消息会一直驻留在队列中直到连接断开,此时会发送到集群中下一个消费者去处理;
  • 队列永远不断开呢?那么消息当然永驻队列中直到内存吃尽,这可不是好事情,所以消费端切记做好异常处理并且finally发送回执;
  • 没有集群则连接重新打开就会再次发送给原来的消费者;

谈回worker

在我看来,worker只是消费者的另外一种叫法,只是它的功能更加特殊,本文开头也说了,消费端基本上声明队列,注册接受事件这些步骤都一样,只是配置项的不同罢了,看下worker的配置:

using System;
using System.Text;
using System.Threading;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace Worker
{
    class Worker
    {
        public static void Main()
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: "task_queue",
                                     durable: true,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: null);

                channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);//prefetchCount:This tells RabbitMQ not to give more than one message to a worker at a time. Or, in other words, don't dispatch a new message to a worker until it has processed and acknowledged the previous one. Instead, it will dispatch it to the next worker that is not still busy(Fair dispatch(公平分发):它会选择空闲的worker去处理消息,适用于对性能敏感的场景;Round-robin dispatching(轮循分发):不解释)

                Console.WriteLine(" [*] Waiting for messages.");

                var consumer = new EventingBasicConsumer(channel);
                channel.BasicConsume(queue: "task_queue",
                                     noAck: false,
                                     consumer: consumer);
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    Console.WriteLine(" [x] Received {0}", message);

                    int dots = message.Split('.').Length - 1;
                    Thread.Sleep(dots * 1000);

                    Console.WriteLine(" [x] Done");

                    channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);//用以处理完毕后发送ack通知服务端
                };

                Console.WriteLine(" Press [enter] to exit.");
                Console.ReadLine();
            }
        }
    }
}

注意这句代码以及注释:channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false) 下面是BasicQops方法的说明:

BasicQops用于设置任务分发,只有收到前一条消息的回执才会发给当前worker,否则轮循下一个worker。如果所有worker都是忙碌,那么建议添加worker设备。

说明worker的特点就是单线程处理消息,如果处于忙碌状态(未发送回执),则不会收到队列推送的消息。

小结:通过队列声明durable: true设置队列持久化,并无多大意义,只是为消息持久化做铺垫;消息持久化通过properties.Persistent = true或者properties.DeliveryMode = 2进行设置;消息回执需要注意处理消费端的代码保证ack总是返回给队列;worker实际上就是一种单线程机制的消费者。

就到这供自己学习,欢迎指正批评!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

by_ron

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值