Пример #1
0
        private void PublishMessages(QueueName requestQueueName, QueueName replyQueueName, ref int processedMessages, CancellationToken token)
        {
            IQueueingService queueingService = CreateProvider();

            processedMessages = 0;
            Random random = new Random();

            while (true)
            {
                long x = random.Next();
                long y = random.Next();

                Message <CalculatorOperation> message = new Message <CalculatorOperation>(TimeSpan.FromMinutes(5), new CalculatorOperation(replyQueueName, "+", x, y));
                queueingService.PostMessages(requestQueueName, message);

                bool handled = false;
                while (true)
                {
                    // process reply messages
                    using (Claim claim = queueingService.ClaimMessage(replyQueueName, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1)))
                    {
                        foreach (QueuedMessage queuedMessage in claim.Messages)
                        {
                            CalculatorResult result = queuedMessage.Body.ToObject <CalculatorResult>();
                            if (result._id == message.Body._id)
                            {
                                // this is the reply to this thread's operation
                                Assert.AreEqual(message.Body._operand1 + message.Body._operand2, result._result);
                                Assert.AreEqual(x + y, result._result);
                                queueingService.DeleteMessage(replyQueueName, queuedMessage.Id, claim);
                                processedMessages++;
                                handled = true;
                            }
                            else if (token.IsCancellationRequested)
                            {
                                // shutdown trigger
                                return;
                            }
                        }
                    }

                    if (handled)
                    {
                        break;
                    }

                    if (token.IsCancellationRequested)
                    {
                        // shutdown trigger
                        return;
                    }
                }

                if (token.IsCancellationRequested)
                {
                    // shutdown trigger
                    return;
                }
            }
        }
Пример #2
0
        public void SynchronousTestQueueClaims()
        {
            IQueueingService provider  = CreateProvider();
            QueueName        queueName = CreateRandomQueueName();

            provider.CreateQueue(queueName);

            provider.PostMessages(queueName, new Message <SampleMetadata>(TimeSpan.FromSeconds(120), new SampleMetadata(3, "yes")));

            QueueStatistics statistics;

            using (Claim claim = provider.ClaimMessage(queueName, null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(1)))
            {
                Assert.AreEqual(TimeSpan.FromMinutes(5), claim.TimeToLive);

                Assert.IsNotNull(claim.Messages);
                Assert.AreEqual(1, claim.Messages.Count);

                statistics = provider.GetQueueStatistics(queueName);
                Assert.AreEqual(1, statistics.MessageStatistics.Claimed);

                QueuedMessage message = provider.GetMessage(queueName, claim.Messages[0].Id);
                Assert.IsNotNull(message);

                TimeSpan age = claim.Age;
                Thread.Sleep(TimeSpan.FromSeconds(2));
                claim.Refresh();
                Assert.IsTrue(claim.Age >= age + TimeSpan.FromSeconds(2));

                claim.Renew(TimeSpan.FromMinutes(10));
                Assert.AreEqual(TimeSpan.FromMinutes(10), claim.TimeToLive);
            }

            statistics = provider.GetQueueStatistics(queueName);
            Assert.AreEqual(0, statistics.MessageStatistics.Claimed);

            provider.DeleteQueue(queueName);
        }
Пример #3
0
        private void SubscribeMessages(QueueName requestQueueName, ref int processedMessages, CancellationToken token)
        {
            IQueueingService queueingService = CreateProvider();

            processedMessages = 0;

            while (true)
            {
                // process request messages
                using (Claim claim = queueingService.ClaimMessage(requestQueueName, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1)))
                {
                    List <QueuedMessage> messagesToDelete = new List <QueuedMessage>();

                    foreach (QueuedMessage queuedMessage in claim.Messages)
                    {
                        if (token.IsCancellationRequested)
                        {
                            return;
                        }

                        CalculatorOperation operation = queuedMessage.Body.ToObject <CalculatorOperation>();
                        CalculatorResult    result;
                        switch (operation._command)
                        {
                        case "+":
                            result = new CalculatorResult(operation, operation._operand1 + operation._operand2);
                            break;

                        case "-":
                            result = new CalculatorResult(operation, operation._operand1 - operation._operand2);
                            break;

                        case "*":
                            result = new CalculatorResult(operation, operation._operand1 * operation._operand2);
                            break;

                        case "/":
                            result = new CalculatorResult(operation, operation._operand1 / operation._operand2);
                            break;

                        default:
                            throw new InvalidOperationException();
                        }

                        messagesToDelete.Add(queuedMessage);

                        queueingService.PostMessages(operation._replyQueueName, new Message <CalculatorResult>(TimeSpan.FromMinutes(5), result));
                        processedMessages++;
                    }

                    if (messagesToDelete.Count > 0)
                    {
                        queueingService.DeleteMessages(requestQueueName, messagesToDelete.Select(i => i.Id));
                    }
                }

                if (token.IsCancellationRequested)
                {
                    return;
                }
            }
        }