/// <summary>
        /// Request an individual message from the queue. Returns null if queue is empty.
        /// </summary>
        /// <param name="noAck">If the acknowledgement will be manual (noAck == false) or automatic (true).</param>
        public RabbitWorkMessage GetOne(bool noAck = false)
        {
            var item = ConsumerChannel.BasicGet(QueueName, noAck);

            return(item != null ? new RabbitWorkMessage(this, item) : null);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public QMessage Dequeue <T>(bool ack = false)
        {
            QMessage       msg = null;
            BasicGetResult result;

            try
            {
                //IBasicProperties basicProperties = ConsumerChannel.CreateBasicProperties();
                //basicProperties.Persistent = true;
                lock (_lockSubscriber)
                {
                    bool noAck = false;
                    // get message from queue
                    result = ConsumerChannel.BasicGet(_consumerSettings.Queue, noAck);
                    if (result == null)
                    {
                        // No message available at this time.
                    }
                    else
                    {
                        IBasicProperties props = result.BasicProperties;

                        // get body
                        byte[] body = result.Body;

                        var json = Encoding.UTF8.GetString(body);

                        json = json.Replace("\"$type\":\"Infrastructure.Queueing.Model.QMessage, Infrastructure.Queueing\",", "");
                        json = json.Replace("\"$type\":\"Infra.Quotes.Brige.Models.BridgeQuote, Infra.Quotes.Bridge\",", "");

                        msg             = JsonConvert.DeserializeObject <QMessage>(json, _consumerSerializationSettings);
                        msg.DeliveryTag = result.DeliveryTag;
                        msg.Properties  = new QProperties()
                        {
                            AppId           = props.AppId,
                            ClusterId       = props.ClusterId,
                            ContentEncoding = props.ContentEncoding,
                            ContentType     = props.ContentType,
                            CorrelationId   = props.CorrelationId,
                            DeliveryMode    = props.DeliveryMode,
                            Expiration      = props.Expiration,
                            MessageId       = props.MessageId,
                            Priority        = props.Priority,
                            ReplyTo         = props.ReplyTo,
                            Type            = props.Type,
                            UserId          = props.UserId
                        };

                        if (ack)
                        {
                            ConsumerChannel.BasicAck(result.DeliveryTag, false);
                        }
                    }
                }
            }
            catch (OperationInterruptedException ex)
            {
                _logger.LogCritical($"Dequeue Error {ex.Message},Inner Exception:{ex.InnerException}, Stack: {ex.StackTrace}");
                throw;
            }

            return(msg);
        }