Example #1
0
 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);
 }
Example #2
0
        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);
        }
Example #3
0
 public IMessage ReceiveNoWait()
 {
     ThrowIfClosed();
     ThrowIfAsync();
     if (TryDequeue(out IMessageDelivery delivery, 0))
     {
         Message.Message copy = delivery.Message.Copy();
         PrepareMessageForDelivery(copy);
         AckReceived(delivery);
         return(copy);
     }
     return(null);
 }
Example #4
0
 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);
 }
Example #5
0
        public IMessage Receive(TimeSpan timeout)
        {
            ThrowIfClosed();
            ThrowIfAsync();
            int timeoutMilis = Convert.ToInt32(timeout.TotalMilliseconds);

            if (timeoutMilis == 0)
            {
                timeoutMilis = -1;
            }
            if (TryDequeue(out IMessageDelivery delivery, timeoutMilis))
            {
                Message.Message copy = delivery.Message.Copy();
                PrepareMessageForDelivery(copy);
                AckReceived(delivery);
                return(copy);
            }
            return(null);
        }
Example #6
0
        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);
            }
        }
Example #7
0
 protected void PrepareMessageForDelivery(Message.Message message)
 {
     if (message == null)
     {
         return;
     }
     if (message is Message.BytesMessage)
     {
         (message as Message.BytesMessage).Reset();
     }
     else if (message is Message.StreamMessage)
     {
         (message as Message.StreamMessage).Reset();
     }
     else
     {
         message.IsReadOnly = true;
     }
     message.IsReadOnlyProperties = true;
 }
Example #8
0
            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();
            }
Example #9
0
        protected void DoSend(IDestination destination, IMessage message, MsgDeliveryMode deliveryMode, MsgPriority priority, TimeSpan timeToLive)
        {
            this.Attach();
            bool sendSync = deliveryMode.Equals(MsgDeliveryMode.Persistent);

            if (destination.IsTemporary && (destination as TemporaryDestination).IsDeleted)
            {
                throw new InvalidDestinationException("Can not send message on deleted temporary topic.");
            }
            message.NMSDestination  = destination;
            message.NMSDeliveryMode = deliveryMode;
            message.NMSPriority     = priority;
            // If there is  timeToLive, set it before setting NMSTimestamp as timeToLive
            // is required to calculate absolute expiry time.
            // TBD: If the messageProducer has a non-default timeToLive and the message
            // already has a timeToLive set by application, which should take precedence, this
            // code overwrites the message TimeToLive in this case but not if the producer TimeToLive
            // is the default ...
            if (timeToLive != NMSConstants.defaultTimeToLive)
            {
                message.NMSTimeToLive = timeToLive;
            }
            if (!DisableMessageTimestamp)
            {
                message.NMSTimestamp = DateTime.UtcNow;
            }


            if (!DisableMessageID)
            {
                message.NMSMessageId = MessageIdGenerator.GenerateId().ToString();
            }

            Amqp.Message amqpmsg = null;
            if (message is Message.Message)
            {
                Message.Message copy = (message as Message.Message).Copy();
                copy.NMSDestination = DestinationTransformation.Transform(Session.Connection, destination);
                PrepareMessageForSend(copy);
                IMessageCloak cloak = copy.GetMessageCloak();
                if (cloak is AMQPMessageCloak)
                {
                    amqpmsg = (cloak as AMQPMessageCloak).AMQPMessage;
                }
            }
            else
            {
                Message.Message nmsmsg = this.Session.Connection.TransformFactory.TransformMessage <Message.Message>(message);
                PrepareMessageForSend(nmsmsg);
                IMessageCloak cloak = nmsmsg.GetMessageCloak().Copy();
                if (cloak is AMQPMessageCloak)
                {
                    amqpmsg = (cloak as AMQPMessageCloak).AMQPMessage;
                }
            }



            if (amqpmsg != null)
            {
                if (Tracer.IsDebugEnabled)
                {
                    Tracer.DebugFormat("Sending message : {0}", message.ToString());
                }

                if (sendSync)
                {
                    DoAMQPSendSync(amqpmsg, this.RequestTimeout);
                }
                else
                {
                    DoAMQPSendAsync(amqpmsg, HandleAsyncAMQPMessageOutcome);
                }
            }
        }