CreateMessage() public method

public CreateMessage ( RabbitMQ.Client.Events.BasicDeliverEventArgs fromQueue ) : Message
fromQueue RabbitMQ.Client.Events.BasicDeliverEventArgs
return Message
コード例 #1
0
        /// <summary>
        /// Receives the specified queue name.
        /// </summary>
        /// <param name="queueName">Name of the queue.</param>
        /// <param name="timeoutInMilliseconds">The timeout in milliseconds.</param>
        /// <returns>Message.</returns>
        public Message Receive(string queueName, string routingKey, int timeoutInMilliseconds)
        {
            Logger.Debug(m => m("RmqMessageConsumer: Preparing  to retrieve next message via exchange {0}", Configuration.Exchange.Name));

            if (!Connect(queueName, routingKey, true))
            {
                Logger.Debug(m => m("RmqMessageConsumer: Unable to connect to the exchange {0}", Configuration.Exchange.Name));
                throw ConnectionFailure;
            }

            var message = new Message();

            try
            {
                BasicDeliverEventArgs fromQueue;
                if (consumer.Queue.Dequeue(timeoutInMilliseconds, out fromQueue))
                {
                    message = messageCreator.CreateMessage(fromQueue);
                    var deliveryTag = (ulong)message.Header.Bag["DeliveryTag"];
                    Logger.Debug(m => m("RmqMessageConsumer: Recieved message with delivery tag {3} from exchange {0} on connection {1} with topic {2} message {4}",
                                        Configuration.Exchange.Name, Configuration.AMPQUri.Uri.ToString(), message.Header.Topic, deliveryTag, JsonConvert.SerializeObject(message)));
                }
                else
                {
                    Logger.Debug(m => m("RmqMessageConsumer: Time out without recieving message from exchange {0} on connection {1} with topic {2}", Configuration.Exchange.Name, Configuration.AMPQUri.Uri.ToString(), queueName));
                }
            }
            catch (Exception e)
            {
                Logger.Error(m => m("RmqMessageConsumer: There was an error listening to channel {0} of {1}", queueName, e.ToString()));
                throw;
            }

            return(message);
        }
コード例 #2
0
        /// <summary>
        /// Receives the specified queue name.
        /// </summary>
        /// <param name="timeoutInMilliseconds">The timeout in milliseconds.</param>
        /// <returns>Message.</returns>
        public Message Receive(int timeoutInMilliseconds)
        {
            _logger.Value.DebugFormat("RmqMessageConsumer: Preparing to retrieve next message from queue {0} with routing key {1} via exchange {2} on connection {3}", _queueName, _routingKey, Connection.Exchange.Name, Connection.AmpqUri.GetSanitizedUri());

            var message = new Message();

            try
            {
                EnsureConsumer();
                BasicDeliverEventArgs fromQueue;
                if (_consumer.Queue.Dequeue(timeoutInMilliseconds, out fromQueue))
                {
                    message = _messageCreator.CreateMessage(fromQueue);
                    _logger.Value.InfoFormat(
                        "RmqMessageConsumer: Received message from queue {0} with routing key {1} via exchange {2} on connection {3}, message: {5}{4}",
                        _queueName, _routingKey, Connection.Exchange.Name, Connection.AmpqUri.GetSanitizedUri(), JsonConvert.SerializeObject(message),
                        Environment.NewLine);
                }
                else
                {
                    _logger.Value.DebugFormat(
                        "RmqMessageConsumer: Time out without receiving message from queue {0} with routing key {1} via exchange {2} on connection {3}",
                        _queueName, _routingKey, Connection.Exchange.Name, Connection.AmpqUri.GetSanitizedUri());
                }
            }
            catch (EndOfStreamException endOfStreamException)
            {
                _logger.Value.DebugException(
                    "RmqMessageConsumer: The consumer {4} was canceled, the model closed, or the connection went away. Listening to queue {0} via exchange {1} via exchange {2} on connection {3}",
                    endOfStreamException,
                    _queueName,
                    _routingKey,
                    Connection.Exchange.Name,
                    Connection.AmpqUri.GetSanitizedUri(),
                    _consumer.ConsumerTag);
            }
            catch (BrokerUnreachableException bue)
            {
                _logger.Value.ErrorException("RmqMessageConsumer: There was an error listening to queue {0} via exchange {1} via exchange {2} on connection {3}",
                                             bue,
                                             _queueName,
                                             _routingKey,
                                             Connection.Exchange.Name,
                                             Connection.AmpqUri.GetSanitizedUri());
                throw new ChannelFailureException("Error connecting to RabbitMQ, see inner exception for details", bue);
            }
            catch (AlreadyClosedException ace)
            {
                _logger.Value.ErrorException("RmqMessageConsumer: There was an error listening to queue {0} via exchange {1} via exchange {2} on connection {3}",
                                             ace,
                                             _queueName,
                                             _routingKey,
                                             Connection.Exchange.Name,
                                             Connection.AmpqUri.GetSanitizedUri());
                throw new ChannelFailureException("Error connecting to RabbitMQ, see inner exception for details", ace);
            }
            catch (OperationInterruptedException oie)
            {
                _logger.Value.ErrorException("RmqMessageConsumer: There was an error listening to queue {0} via exchange {1} via exchange {2} on connection {3}",
                                             oie,
                                             _queueName,
                                             _routingKey,
                                             Connection.Exchange.Name,
                                             Connection.AmpqUri.GetSanitizedUri());
                throw new ChannelFailureException("Error connecting to RabbitMQ, see inner exception for details", oie);
            }
            catch (NotSupportedException nse)
            {
                _logger.Value.ErrorException("RmqMessageConsumer: There was an error listening to queue {0} via exchange {1} via exchange {2} on connection {3}",
                                             nse,
                                             _queueName,
                                             _routingKey,
                                             Connection.Exchange.Name,
                                             Connection.AmpqUri.GetSanitizedUri());
                throw new ChannelFailureException("Error connecting to RabbitMQ, see inner exception for details", nse);
            }
            catch (BrokenCircuitException bce)
            {
                _logger.Value.WarnFormat("CIRCUIT BROKEN: RmqMessageConsumer: There was an error listening to queue {0} via exchange {1} via exchange {2} on connection {3}",
                                         _queueName,
                                         _routingKey,
                                         Connection.Exchange.Name,
                                         Connection.AmpqUri.GetSanitizedUri());
                throw new ChannelFailureException("Error connecting to RabbitMQ, see inner exception for details", bce);
            }
            catch (Exception exception)
            {
                _logger.Value.ErrorException("RmqMessageConsumer: There was an error listening to queue {0} via exchange {1} via exchange {2} on connection {3}", exception, _queueName, _routingKey, Connection.Exchange.Name, Connection.AmpqUri.GetSanitizedUri());
                throw;
            }

            return(message);
        }