private void AckReleased(IMessageDelivery delivery)
 {
     Message.Message nmsMessage = delivery.Message;
     Tracer.DebugFormat("Consumer {0} Acking Released for Message {1} ", ConsumerId, nmsMessage.NMSMessageId);
     Amqp.Message amqpMessage = (nmsMessage.GetMessageCloak() as Message.AMQP.AMQPMessageCloak).AMQPMessage;
     this.Link.Release(amqpMessage);
 }
Exemplo n.º 2
0
        protected override IMessageDelivery RemoveFirst()
        {
            IMessageDelivery first = list.First.Value;

            list.RemoveFirst();
            return(first);
        }
        private void AckRejected(IMessageDelivery delivery, NMSException ex)
        {
            Error err = new Error(NMSErrorCode.INTERNAL_ERROR);

            err.Description = ex.Message;
            AckRejected(delivery, err);
        }
 protected bool TryDequeue(out IMessageDelivery value, int timeout = -1)
 {
     value = null;
     lock (SyncRoot)
     {
         bool signaled = true;
         while (IsEmpty)
         {
             if (IsClosed || mode.Value.Equals(Resource.Mode.Stopping))
             {
                 return(false);
             }
             signaled = (timeout > -1) ? Monitor.Wait(SyncRoot, timeout) : Monitor.Wait(SyncRoot);
             if (!signaled && timeout > -1)
             {
                 return(false);
             }
         }
         if (!signaled)
         {
             return(false);
         }
         value = RemoveFirst();
     }
     return(value != null);
 }
Exemplo n.º 5
0
 public override void EnqueueFirst(IMessageDelivery message)
 {
     lock (SyncRoot)
     {
         list.AddFirst(message);
         Monitor.PulseAll(SyncRoot);
     }
 }
        public EmailEventHandler(IMessageFormatter messageFormatter, IMessageDelivery messageDelivery)
        {
            if (messageFormatter == null) throw new ArgumentNullException("messageFormatter");
            if (messageDelivery == null) throw new ArgumentNullException("messageDelivery");

            this.messageFormatter = messageFormatter;
            this.messageDelivery = messageDelivery;
        }
 private void AckConsumed(IMessageDelivery delivery)
 {
     Message.Message nmsMessage = delivery.Message;
     Tracer.DebugFormat("Consumer {0} Acking Accepted for Message {1} ", ConsumerId, nmsMessage.NMSMessageId);
     delivered.Remove(delivery);
     Amqp.Message amqpMessage = (nmsMessage.GetMessageCloak() as Message.AMQP.AMQPMessageCloak).AMQPMessage;
     this.Link.Accept(amqpMessage);
 }
 public override void EnqueueFirst(IMessageDelivery message)
 {
     lock (SyncRoot)
     {
         priorityList[(int)MsgPriority.Highest].AddFirst(message);
         count++;
         Monitor.PulseAll(SyncRoot);
     }
 }
 private void AckModified(IMessageDelivery delivery, bool deliveryFailed, bool undeliverableHere = false)
 {
     Message.Message nmsMessage = delivery.Message;
     Tracer.DebugFormat("Consumer {0} Acking Modified for Message {1}{2}{3}.", ConsumerId, nmsMessage.NMSMessageId,
                        deliveryFailed ? " Delivery Failed" : "",
                        undeliverableHere ? " Undeliveryable Here" : "");
     Amqp.Message amqpMessage = (nmsMessage.GetMessageCloak() as Message.AMQP.AMQPMessageCloak).AMQPMessage;
     //TODO use Link.Modified from amqpNetLite 2.0.0
     this.Link.Modify(amqpMessage, deliveryFailed, undeliverableHere, null);
 }
        private bool IsMessageRedeliveryExceeded(IMessageDelivery delivery)
        {
            Message.Message   message = delivery.Message;
            IRedeliveryPolicy policy  = this.Session.Connection.RedeliveryPolicy;

            if (policy != null && policy.MaximumRedeliveries >= 0)
            {
                IMessageCloak msgCloak = message.GetMessageCloak();
                return(msgCloak.RedeliveryCount > policy.MaximumRedeliveries);
            }
            return(false);
        }
        private void AckReceived(IMessageDelivery delivery)
        {
            IMessageCloak cloak = delivery.Message.GetMessageCloak();

            if (cloak.AckHandler != null)
            {
                delivered.AddLast(delivery);
            }
            else
            {
                AckConsumed(delivery);
            }
        }
 private bool IsMessageExpired(IMessageDelivery delivery)
 {
     Message.Message message = delivery.Message;
     if (message.NMSTimeToLive != TimeSpan.Zero)
     {
         DateTime now = DateTime.UtcNow;
         if (!IsBrowser && (message.NMSTimestamp + message.NMSTimeToLive) < now)
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 13
0
        public EmailEventHandler(IMessageFormatter <TAccount> messageFormatter, IMessageDelivery messageDelivery)
        {
            if (messageFormatter == null)
            {
                throw new ArgumentNullException("messageFormatter");
            }
            if (messageDelivery == null)
            {
                throw new ArgumentNullException("messageDelivery");
            }

            this.messageFormatter = messageFormatter;
            this.messageDelivery  = messageDelivery;
        }
        protected void SendForDelivery(IMessageDelivery nmsDelivery)
        {
            this.messageQueue.Enqueue(nmsDelivery);

            if (this.OnMessage != null && pendingMessageDeliveryTasks < ExecutorThreshold)
            {
                AddTaskRef();
                DispatchEvent dispatchEvent = new MessageListenerDispatchEvent(this);
                Session.Dispatcher.Enqueue(dispatchEvent);
            }
            else if (pendingMessageDeliveryTasks < 0)
            {
                Tracer.ErrorFormat("Consumer {0} has invalid pending tasks count on executor {1}.", Id, pendingMessageDeliveryTasks);
            }
        }
 public override void Enqueue(IMessageDelivery message)
 {
     if (message.EnqueueFirst)
     {
         EnqueueFirst(message);
     }
     else
     {
         lock (SyncRoot)
         {
             LinkedList <IMessageDelivery> list = priorityList[GetPriorityIndex(message)];
             list.AddLast(message);
             count++;
             Monitor.PulseAll(SyncRoot);
         }
     }
 }
 protected override IMessageDelivery RemoveFirst()
 {
     if (!IsClosed && count > 0)
     {
         for (int i = (int)MsgPriority.Highest; i >= 0; i--)
         {
             LinkedList <IMessageDelivery> list = priorityList[i];
             if (list.Count != 0)
             {
                 IMessageDelivery first = list.First.Value;
                 list.RemoveFirst();
                 count--;
                 return(first);
             }
         }
     }
     return(null);
 }
        internal void Recover()
        {
            Tracer.DebugFormat("Session recover for consumer: {0}", Id);
            IMessageCloak    cloak    = null;
            IMessageDelivery delivery = null;

            lock (messageQueue.SyncRoot)
            {
                while ((delivery = delivered.Last?.Value) != null)
                {
                    cloak = delivery.Message.GetMessageCloak();
                    cloak.DeliveryCount = cloak.DeliveryCount + 1;
                    (delivery as MessageDelivery).EnqueueFirst = true;
                    delivered.RemoveLast();
                    SendForDelivery(delivery);
                }
                delivered.Clear();
            }
        }
        internal void AcknowledgeMessage(Message.Message message, Message.AckType ackType)
        {
            ThrowIfClosed();
            IMessageDelivery nmsDelivery = null;

            foreach (IMessageDelivery delivery in delivered)
            {
                if (delivery.Message.Equals(message))
                {
                    nmsDelivery = delivery;
                }
            }
            if (nmsDelivery == null)
            {
                nmsDelivery = new MessageDelivery(message);
            }
            switch (ackType)
            {
            case Message.AckType.ACCEPTED:
                AckConsumed(nmsDelivery);
                break;

            case Message.AckType.MODIFIED_FAILED:
                AckModified(nmsDelivery, true);
                break;

            case Message.AckType.MODIFIED_FAILED_UNDELIVERABLE:
                AckModified(nmsDelivery, true, true);
                break;

            case Message.AckType.REJECTED:
                AckRejected(nmsDelivery);
                break;

            case Message.AckType.RELEASED:
                AckReleased(nmsDelivery);
                break;

            default:
                throw new NMSException("Unkown message acknowledgement type " + ackType);
            }
        }
            public void DispatchMessageListeners()
            {
                IMessageDelivery delivery = null;

                Message.Message nmsProviderMessage = null;
                if (consumer.IsClosed)
                {
                    return;
                }
                consumer.EnterMessageListenerEvent();
                // the consumer pending Message delivery task

                while ((delivery = consumer.messageQueue.DequeueNoWait()) != null)
                {
                    nmsProviderMessage = delivery.Message;
                    consumer.AddTaskRef();
                    consumer.messageDispatchCount++;
                    try
                    {
                        if (consumer.IsMessageExpired(delivery))
                        {
                            consumer.AckModified(delivery, true);
                        }
                        else if (consumer.IsMessageRedeliveryExceeded(delivery))
                        {
                            consumer.AckModified(delivery, true, true);
                        }
                        else
                        {
                            bool deliveryFailed = false;
                            bool isAutoOrDupsOk = consumer.Session.AcknowledgementMode.Equals(AcknowledgementMode.AutoAcknowledge) ||
                                                  consumer.Session.AcknowledgementMode.Equals(AcknowledgementMode.DupsOkAcknowledge);
                            if (isAutoOrDupsOk)
                            {
                                consumer.delivered.AddLast(delivery);
                            }
                            else
                            {
                                consumer.AckReceived(delivery);
                            }

                            Message.Message copy = nmsProviderMessage.Copy();
                            try
                            {
                                consumer.Session.ClearRecovered();
                                consumer.PrepareMessageForDelivery(copy);
                                if (Tracer.IsDebugEnabled)
                                {
                                    Tracer.DebugFormat("Invoking Client Message Listener Callback for message {0}.", copy.NMSMessageId);
                                }
                                consumer.OnMessage(copy);
                            }
                            catch (SystemException se)
                            {
                                Tracer.WarnFormat("Caught Exception on MessageListener for Consumer {0}. Message {1}.", consumer.Id, se.Message);
                                deliveryFailed = true;
                            }

                            if (isAutoOrDupsOk && !consumer.Session.IsRecovered)
                            {
                                if (!deliveryFailed)
                                {
                                    consumer.AckConsumed(delivery);
                                }
                                else
                                {
                                    consumer.AckReleased(delivery);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        // unhandled failure
                        consumer.Session.OnException(e);
                    }
                    consumer.RemoveTaskRef();
                }
                consumer.LeaveMessageListenerEvent();
            }
 public NotificationService(IMessageDelivery messageDelivery, ApplicationInformation appInfo)
 {
     this.messageDelivery = messageDelivery;
     this.appInfo         = appInfo;
 }
 public NotificationService(IMessageDelivery messageDelivery, ApplicationInformation appInfo)
 {
     this.messageDelivery = messageDelivery;
     this.appInfo = appInfo;
 }
Exemplo n.º 22
0
 public UserEmailEventHandler(ILoggerFactory loggerFactory, IMessageFormatter <HierarchicalUserAccount> messageFormatter, IMessageDelivery messageDelivery)
     : base(messageFormatter, messageDelivery)
 {
     _logger = loggerFactory.CreateLogger(this.GetType().ToString());
 }
 public EmailAccountEventsHandler(IMessageFormatter messageFormatter, IMessageDelivery messageDelivery)
     : base(messageFormatter, messageDelivery)
 {
 }
 public NotificationService(IMessageDelivery messageDelivery, ApplicationInformation appInfo)
 {
     this.smtpEventHandler = new EmailEventHandler(new EmailMessageFormatter(appInfo), messageDelivery);
     this.appInfo = appInfo;
 }
 private void AckRejected(IMessageDelivery delivery, Error err = null)
 {
     Tracer.DebugFormat("Consumer {0} Acking Rejected for Message {1} with error {2} ", ConsumerId, delivery.Message.NMSMessageId, err?.ToString());
     Amqp.Message amqpMessage = (delivery.Message.GetMessageCloak() as Message.AMQP.AMQPMessageCloak).AMQPMessage;
     this.Link.Reject(amqpMessage, err);
 }
 public abstract void EnqueueFirst(IMessageDelivery message);
 private MsgPriority GetPriority(IMessageDelivery message)
 {
     return(message.Priority);
 }
 public virtual IEnumerator GetEnumerator()
 {
     IMessageDelivery[] messages = new IMessageDelivery[Count];
     this.CopyTo(messages, 0);
     return(new MessageQueueEnumerator(messages));
 }
        protected virtual bool TryDequeue(out IMessageDelivery delivery, int timeout)
        {
            delivery = null;
            DateTime deadline = DateTime.UtcNow + TimeSpan.FromMilliseconds(timeout);

            Tracer.DebugFormat("Waiting for msg availability Deadline {0}", deadline);
            try
            {
                while (true)
                {
                    if (timeout == 0)
                    {
                        delivery = this.messageQueue.DequeueNoWait();
                    }
                    else
                    {
                        delivery = this.messageQueue.Dequeue(timeout);
                    }

                    if (delivery == null)
                    {
                        if (timeout == 0 || this.Link.IsClosed || this.messageQueue.IsClosed)
                        {
                            return(false);
                        }
                        else if (timeout > 0)
                        {
                            timeout = Math.Max((deadline - DateTime.UtcNow).Milliseconds, 0);
                        }
                    }
                    else if (IsMessageExpired(delivery))
                    {
                        DateTime now = DateTime.UtcNow;
                        Error    err = new Error(NMSErrorCode.PROPERTY_ERROR);
                        err.Description = "Message Expired";
                        AckRejected(delivery, err);
                        if (timeout > 0)
                        {
                            timeout = Math.Max((deadline - now).Milliseconds, 0);
                        }
                        if (Tracer.IsDebugEnabled)
                        {
                            Tracer.DebugFormat("{0} Filtered expired (deadline {1} Now {2}) message: {3}", ConsumerId, deadline, now, delivery);
                        }
                    }
                    else if (IsMessageRedeliveryExceeded(delivery))
                    {
                        if (Tracer.IsDebugEnabled)
                        {
                            Tracer.DebugFormat("{0} Filtered Message with excessive Redelivery Count: {1}", ConsumerId, delivery);
                        }
                        AckModified(delivery, true, true);
                        if (timeout > 0)
                        {
                            timeout = Math.Max((deadline - DateTime.UtcNow).Milliseconds, 0);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                throw ExceptionSupport.Wrap(e, "Failed to Received message on consumer {0}.", ConsumerId);
            }

            return(true);
        }
 public NotificationService(IMessageDelivery messageDelivery, ApplicationInformation appInfo)
 {
     this.smtpEventHandler = new EmailEventHandler(new EmailMessageFormatter(appInfo), messageDelivery);
     this.appInfo          = appInfo;
 }
 private int GetPriorityIndex(IMessageDelivery message)
 {
     return((int)GetPriority(message));
 }
Exemplo n.º 32
0
 public EmailAccountEventsHandler(IMessageFormatter <T> messageFormatter, IMessageDelivery messageDelivery)
     : base(messageFormatter, messageDelivery)
 {
 }
Exemplo n.º 33
0
 public DefaultEmailEventHandler(ILoggerFactory loggerFactory, IMessageFormatter <HierarchicalUserAccount> messageFormatter, IMessageDelivery messageDelivery, MembershipRebootConfiguration <HierarchicalUserAccount> config)
     : base(messageFormatter, messageDelivery)
 {
     _logger = loggerFactory.CreateLogger(this.GetType().ToString());
     _config = config;
 }
 public EmailAccountCreatedEventHandler(IMessageFormatter messageFormatter, IMessageDelivery messageDelivery)
     : base(messageFormatter, messageDelivery)
 {
 }