Scatter Gather:等待所有“Gather-Workers”完成

时间:2016-05-09 13:56:42

标签: rabbitmq message-queue

我刚进入RabbitMQ。

我有分散并收集概念验证工作。

我的最终设置将是:

MyClient(又名,Scatter'er)

MyGatherWorker1

MyGatherWorker2

MyGatherWorker3

MyGatherWorkerN(此时可能达到10)

MyRabbitMQServerHost

MyGatherWorker1-MyGatherWorkerN将连接到MyRabbitMQServerHost上的“Exchange”(我认为这是正确的术语)。

MyClient将向MyRabbitMQServerHost发送分散 - 收集请求。

下面是“MyClient”代码。

我有一个minResponses变量。其中说“一旦得到N个响应,请考虑分散并收集完成”。

但是,确保我配置的所有“MyGatherWorker”都能提供回复的最佳策略是什么?

我希望保证“等待所有MyGatherWorkers”策略。并且希望不是我必须在客户端配置上放置“N”值以匹配(相同)数量的MyGatherWorker。

我知道我最终会在系统中添加更多MyGatherWorker。

private ICollection<CustomerMoveSummary> SendScatterGatherMessageToQueues(List<string> customerIds, IModel model, TimeSpan timeout, string routingKey, int minResponses)
        {
            List<CustomerMoveSummary> returnItems = new List<CustomerMoveSummary>(); 

            if (string.IsNullOrEmpty(_scatterGatherResponseQueue))
            {
                _scatterGatherResponseQueue = model.QueueDeclare().QueueName;
            }

            if (_scatterGatherConsumer == null)
            {
                _scatterGatherConsumer = new QueueingBasicConsumer(model);
                model.BasicConsume(_scatterGatherResponseQueue, true, _scatterGatherConsumer);
            }

            string correlationId = Guid.NewGuid().ToString();
            IBasicProperties basicProperties = model.CreateBasicProperties();
            basicProperties.ReplyTo = _scatterGatherResponseQueue;
            basicProperties.CorrelationId = correlationId;


            String jsonified = JsonConvert.SerializeObject(customerIds);
            byte[] messageBytes = Encoding.UTF8.GetBytes(jsonified);

            model.BasicPublish(_scatterGatherExchange, routingKey, basicProperties, messageBytes);

            DateTime timeoutDate = DateTime.UtcNow + timeout;
            while (DateTime.UtcNow <= timeoutDate)
            {
                BasicDeliverEventArgs deliveryArguments;
                _scatterGatherConsumer.Queue.Dequeue(500, out deliveryArguments);
                if (deliveryArguments != null && deliveryArguments.BasicProperties != null
                    && deliveryArguments.BasicProperties.CorrelationId == correlationId)
                {
                    //string response = Encoding.UTF8.GetString(deliveryArguments.Body);

                    string returnSummaryJsonified = Encoding.UTF8.GetString(deliveryArguments.Body);
                    CustomerMoveSummary summary = JsonConvert.DeserializeObject<CustomerMoveSummary>(returnSummaryJsonified);

                    returnItems.Add(summary);
                    if (returnItems.Count >= minResponses)
                    {
                        Console.WriteLine("returnItems.Count >= minResponses !!");
                        break;
                    }
                }
            }

            return returnItems;
        }

PS

如果有任何“MyGatherWorker发生异常”,那么处理该问题的最佳做法是什么?

0 个答案:

没有答案