public QueueConsumer(QueueActivityConsumer consumer, IRegisteredConsumer registeredConsumer, ConsumeToken consumeToken)
 {
     Consumer = consumer;
     ConsumeToken = consumeToken;
     RegisteredConsumer = registeredConsumer;
 }
        private QueueActivityConsumer CreateAndSetupQueueConsumer(IModel model, string queue)
        {
            var queueConsumer = new QueueActivityConsumer(model, queue);
            queueConsumer.Deliver += OnQueueDeliver;

            return queueConsumer;
        }
        private void OnQueueDeliver(object sender, QueueActivityConsumer.DeliverEventArgs e)
        {
            QueueActivityConsumer consumer = (QueueActivityConsumer) sender;

            this._deliveryQueue.Enqueue(new KeyValuePair<QueueActivityConsumer, QueueActivityConsumer.DeliverEventArgs>(consumer, e));
            lock (this._consumerLockObject)
            {
                Monitor.Pulse(this._consumerLockObject);
            }
        }
        private void CallbackWithMessage(QueueActivityConsumer sender, QueueActivityConsumer.DeliverEventArgs args)
        {
            if (this.QueueConsumers.ContainsKey(args.Queue) &&
                !this.QueueConsumers[args.Queue].ConsumeToken.IsClosed)
            {
                var registeredConsumer = this.QueueConsumers[args.Queue].RegisteredConsumer;
                var envelope = Serializer.DeserializeMessage(args);

                // Fire MessageReceived event
                Config.PipelineEvents.OnMessageReceived(new PipelineEventArgs(envelope));

                try
                {
                    if (!string.IsNullOrEmpty(envelope.UserName))
                    {
                        var resolvePrincipalArgs = new ResolvePrincipalEventArgs(envelope);

                        // Fire ResolvePrincipal event
                        Config.PipelineEvents.OnResolvePrincipal(resolvePrincipalArgs);

                        if (resolvePrincipalArgs.Principal != null)
                        {
                            Thread.CurrentPrincipal = resolvePrincipalArgs.Principal;
                        }
                    }

                    var acceptance = registeredConsumer.Accept(envelope.Message);

                    if (acceptance == Acceptance.Accept)
                    {
                        Log.DebugFormat("{0} accepted message {1}.",
                                       registeredConsumer.ConsumerType.Name, registeredConsumer.MessageType.Name);

                        registeredConsumer.Invoke(envelope.Message);

                        sender.Model.BasicAck(args.DeliveryTag, false);

                        // Fire MessageConsumed event
                        Config.PipelineEvents.OnMessageConsumed(new PipelineEventArgs(envelope));
                    }
                    else if (acceptance == Acceptance.Requeue)
                    {
                        Log.DebugFormat("{0} is re-queueing message {1}.",
                                       registeredConsumer.ConsumerType.Name, registeredConsumer.MessageType.Name);

                        sender.Model.BasicNack(args.DeliveryTag, false, true);
                    }
                    else if (acceptance == Acceptance.DelayedRequeue)
                    {
                        Log.DebugFormat("{0} is re-queueing message {1} with delay.",
                                       registeredConsumer.ConsumerType.Name, registeredConsumer.MessageType.Name);

                        this._requeueQueue.Enqueue(
                            new KeyValuePair<QueueActivityConsumer, QueueActivityConsumer.DeliverEventArgs>(sender, args));

                        lock (this._requeueLockObject)
                        {
                            Monitor.Pulse(this._requeueLockObject);
                        }
                    }
                    else
                    {
                        Log.WarnFormat("{0} rejected message {1}.",
                                       registeredConsumer.ConsumerType.Name, registeredConsumer.MessageType.Name);

                        sender.Model.BasicNack(args.DeliveryTag, false, false);
                    }
                }
                catch (Exception ex)
                {
                    this._requeueQueue.Enqueue(
                        new KeyValuePair<QueueActivityConsumer, QueueActivityConsumer.DeliverEventArgs>(sender, args));

                    lock (this._requeueLockObject)
                    {
                        Monitor.Pulse(this._requeueLockObject);
                    }

                    Log.Error(
                        string.Format("An exception was thrown while invoking the consumer."), ex);
                }
            }
        }