void ReceiveMessages(CancellationToken outer, IEnumerable <string> queues)
 {
     using (var source = CancellationTokenSource.CreateLinkedTokenSource(_disposal.Token, outer))
     {
         var             token = source.Token;
         MessageEnvelope envelope;
         foreach (var queue in queues)
         {
             while (_queueReader.TakeMessage(queue, token, out envelope))
             {
                 try
                 {
                     var msg       = envelope.MessageReference.Message;
                     var consumers = _consumerFactory.GetMessageConsumers(msg);
                     foreach (var consumer in consumers)
                     {
                         _observer.Notify(new ConsumeBegin(msg, consumer, envelope.QueueName));
                         consumer.Consume(msg);
                         _observer.Notify(new ConsumeEnd(msg, consumer, envelope.QueueName));
                     }
                 }
                 catch (Exception ex)
                 {
                     _observer.Notify(new FailedToConsumeMessage(ex, envelope.EnvelopeId, envelope.QueueName));
                 }
                 try
                 {
                     _queueReader.DeleteMessage(envelope);
                 }
                 catch (Exception ex)
                 {
                     // not a big deal. Message will be processed again.
                     _observer.Notify(new FailedToAckMessage(ex, envelope.EnvelopeId, envelope.QueueName));
                 }
             }
         }
     }
 }
Example #2
0
        void ReceiveMessages(CancellationToken outer)
        {
            using (var source = CancellationTokenSource.CreateLinkedTokenSource(_disposal.Token, outer))
            {
                while (true)
                {
                    MessageTransportContext context;
                    try
                    {
                        if (!_inbox.TakeMessage(source.Token, out context))
                        {
                            // we didn't retrieve queue within the token lifetime.
                            // it's time to shutdown the server
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    try
                    {
                        ProcessMessage(context);
                    }
                    catch (ThreadAbortException)
                    {
                        // Nothing. we are being shutdown
                    }
                    catch (Exception ex)
                    {
                        var e = new DispatchRecoveryFailed(ex, context, context.QueueName);
                        SystemObserver.Notify(e);
                    }
                }
            }
        }