Exemplo n.º 1
0
        /// <summary>
        /// Publishes the message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="headers">User specified message headers.</param>
        /// <param name="regenerate">Generate new unique message identifier.</param>
        public void PublishMessage(Message message, int delayMilliseconds)
        {
            var messageId = message.Id;
            var deliveryTag = message.Header.Bag.ContainsKey(HeaderNames.DELIVERY_TAG) ? message.GetDeliveryTag().ToString() : null;

            var headers = new Dictionary<string, object>
                                      {
                                          {HeaderNames.MESSAGE_TYPE, message.Header.MessageType.ToString()},
                                          {HeaderNames.TOPIC, message.Header.Topic},
                                          {HeaderNames.HANDLED_COUNT, message.Header.HandledCount.ToString(CultureInfo.InvariantCulture)},
                                      };

            message.Header.Bag.Each((header) =>
            {
                if (!HeadersToReset.Any(htr => htr.Equals(header.Key))) headers.Add(header.Key, header.Value);
            });

            if (!String.IsNullOrEmpty(deliveryTag))
                headers.Add(HeaderNames.DELIVERY_TAG, deliveryTag);

            if (delayMilliseconds > 0)
                headers.Add(HeaderNames.DELAY_MILLISECONDS, delayMilliseconds);

            _channel.BasicPublish(
                _exchangeName,
                message.Header.Topic,
                false,
                false,
                CreateBasicProperties(messageId, message.Header.TimeStamp, headers),
                Encoding.UTF8.GetBytes(message.Body.Value));
        }