Example #1
0
        private void ReceiveTopicMessages(CancellationTokenSource cancellationSource,
                                          OnMessagesReceived onMessagesReceived)
        {
            var  needPeek       = true;
            long sequenceNumber = 0;
            IEnumerable <BrokeredMessage> brokeredMessages = null;

            #region peek messages that not been consumed since last time

            while (!cancellationSource.IsCancellationRequested && needPeek)
            {
                try
                {
                    brokeredMessages = _subscriptionClient.PeekBatch(sequenceNumber, 50);
                    if (brokeredMessages == null || brokeredMessages.Count() == 0)
                    {
                        break;
                    }
                    var messageContexts = new List <IMessageContext>();
                    foreach (var message in brokeredMessages)
                    {
                        if (message.State != MessageState.Deferred)
                        {
                            needPeek = false;
                            break;
                        }
                        messageContexts.Add(new MessageContext(message));
                        sequenceNumber = message.SequenceNumber + 1;
                    }
                    onMessagesReceived(messageContexts.ToArray());
                }
                catch (OperationCanceledException)
                {
                    return;
                }
                catch (ThreadAbortException)
                {
                    return;
                }
                catch (Exception ex)
                {
                    Thread.Sleep(1000);
                    _logger.Error($"subscriptionClient.ReceiveBatch {_subscriptionClient.Name} failed", ex);
                }
            }

            #endregion

            #region receive messages to enqueue consuming queue

            while (!cancellationSource.IsCancellationRequested)
            {
                try
                {
                    brokeredMessages =
                        _subscriptionClient.ReceiveBatch(50,
                                                         Configuration.Instance.GetMessageQueueReceiveMessageTimeout());
                    foreach (var message in brokeredMessages)
                    {
                        message.Defer();
                        onMessagesReceived(new MessageContext(message));
                    }
                }
                catch (OperationCanceledException)
                {
                    return;
                }
                catch (ThreadAbortException)
                {
                    return;
                }
                catch (Exception ex)
                {
                    Thread.Sleep(1000);
                    _logger.Error($"subscriptionClient.ReceiveBatch {_subscriptionClient.Name} failed", ex);
                }
            }

            #endregion
        }
Example #2
0
        static void Test(NamespaceManager namespaceManager, MessagingFactory messageFactory)
        {
            var q = namespaceManager.GetQueue(QueueName);
            var t = namespaceManager.GetTopic(TopicName);

            var gf = namespaceManager.GetTopics();

            if (!namespaceManager.QueueExists(QueueName))
            {
                namespaceManager.CreateQueue(QueueName);
            }


            QueueClient myQueueClient = messageFactory.CreateQueueClient(QueueName);

            try
            {
                BrokeredMessage sendMessage = new BrokeredMessage("Hello World !");
                myQueueClient.Send(sendMessage);

                //// Receive the message from the queue
                //BrokeredMessage receivedMessage = myQueueClient.Receive(TimeSpan.FromSeconds(5));

                //if (receivedMessage != null)
                //{
                //    Console.WriteLine(string.Format("Message received: {0}", receivedMessage.GetBody<string>()));
                //    receivedMessage.Complete();
                //}

                //Check for messages that are older than they should be
                int      minutesOld = 1;
                DateTime oldest     = DateTime.UtcNow.AddMinutes(-minutesOld);

                int oldMessageCount             = 0;
                List <BrokeredMessage> messages = new List <BrokeredMessage>(myQueueClient.PeekBatch(1000));

                foreach (BrokeredMessage b in messages)
                {
                    if (b.EnqueuedTimeUtc < oldest)
                    {
                        oldMessageCount++;
                    }
                }

                BrokeredMessage bd = myQueueClient.Receive();
                bd.DeadLetter();

                //check for dead letter messages
                QueueClient dlClient = messageFactory.CreateQueueClient(QueueClient.FormatDeadLetterPath(QueueName));

                List <BrokeredMessage> dlMessages = new List <BrokeredMessage>(dlClient.PeekBatch(1000));
                int i = dlMessages.Count;

                //b.DeadLetter();


                if (!namespaceManager.TopicExists(TopicName))
                {
                    namespaceManager.CreateTopic(TopicName);
                }


                if (!namespaceManager.SubscriptionExists(TopicName, SubName))
                {
                    namespaceManager.CreateSubscription(TopicName, SubName);
                }



                TopicClient topicClient = messageFactory.CreateTopicClient(TopicName);

                topicClient.Send(new BrokeredMessage("Message"));

                SubscriptionDescription s = namespaceManager.GetSubscription(TopicName, SubName);

                SubscriptionClient subClient = messageFactory.CreateSubscriptionClient(TopicName, SubName);

                List <BrokeredMessage> subMessages = new List <BrokeredMessage>(subClient.PeekBatch(1000));
                BrokeredMessage        bms         = subClient.Receive();
                bms.DeadLetter();

                foreach (BrokeredMessage b in messages)
                {
                    if (b.EnqueuedTimeUtc < oldest)
                    {
                        oldMessageCount++;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception {0}", e.ToString());
                throw;
            }
            finally
            {
                if (messageFactory != null)
                {
                    messageFactory.Close();
                }
            }
            //Send Message
        }
Example #3
0
 public override IEnumerable <BrokeredMessage> PeekNextBatch(long peekStartingPoint, int number)
 {
     return(Client.PeekBatch(peekStartingPoint, number));
 }