Exemplo n.º 1
0
        private bool TryGetRequest(ConcurrentQueueingConsumer consumer, out HttpRequestPacket request, out MessageDispatch dispatch)
        {
            request  = null;
            dispatch = null;

            BasicDeliverEventArgs item;

            if (!consumer.TryInstantDequeue(out item, throwIfClosed: false))
            {
                return(false);
            }

            //TODO: Pool MessageDispatch
            //Get message
            dispatch = new MessageDispatch {
                Consumer = consumer, Delivery = item
            };

            //Deserialize message
            bool wasDeserialized = true;

            try
            {
                request = HttpRequestPacket.Deserialize(item.Body);
            }
            catch
            {
                wasDeserialized = false;
            }

            if (wasDeserialized)
            {
                //Add/Update Subscriber-Id header
                request.Headers[Common.Shared.SUBSCRIBER_ID_HEADER] = this.subscriberIdHeader;

                //Add redelivered header if item was redelivered.
                if (item.Redelivered)
                {
                    request.Headers[Common.Shared.REDELIVERED_HEADER] = TRUE_STRING_ARRAY;
                }
            }
            //Reject message if deserialization failed.
            else if (!wasDeserialized && Settings.AckBehavior != SubscriberAckBehavior.Automatic)
            {
                consumer.Model.BasicReject(item.DeliveryTag, false);
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        private bool TryGetRequest(SharedQueue <BasicDeliverEventArgs> queue, out HttpRequestPacket request, out IBasicProperties properties)
        {
            object obj;
            BasicDeliverEventArgs evt;

            request    = null;
            properties = null;

            obj = queue.DequeueNoWait(null);
            if (obj != null)
            {
                evt = (BasicDeliverEventArgs)obj;
            }
            else
            {
                return(false);
            }


            //Get message properties
            properties = evt.BasicProperties;

            //Deserialize message
            bool wasDeserialized = true;

            try
            {
                request = HttpRequestPacket.Deserialize(evt.Body);

                //Add/Update Content-Length Header
                request.Headers["Content-Length"] = new string[] { (request.Content == null ? 0 : request.Content.Length).ToString() };

                //Add/Update Subscriber-Id header
                request.Headers[Common.Shared.SUBSCRIBER_ID_HEADER] = new string[] { this.subscriberId };
            }
            catch
            {
                wasDeserialized = false;
            }


            //Ack or reject message
            if (evt.ConsumerTag == workConsumer.ConsumerTag)
            {
                if (wasDeserialized)
                {
                    workConsumer.Model.BasicAck(evt.DeliveryTag, false);
                    return(true);
                }
                else
                {
                    workConsumer.Model.BasicReject(evt.DeliveryTag, false);
                    return(false);
                }
            }
            else if (evt.ConsumerTag == subscriberConsumer.ConsumerTag)
            {
                if (wasDeserialized)
                {
                    subscriberConsumer.Model.BasicAck(evt.DeliveryTag, false);
                    return(true);
                }
                else
                {
                    subscriberConsumer.Model.BasicReject(evt.DeliveryTag, false);
                    return(false);
                }
            }
            else
            {
                throw new InvalidOperationException("Message was dequeued by an unexpected/unknown consumer");
            }
        }