/// <summary>
        /// Publishes a message to the queue using the QueueName as the routingKey.
        /// </summary>
        /// <typeparam name="T">The type of the message object.</typeparam>
        /// <param name="msg">The message.</param>
        /// <param name="exchange">[Optional] The exchange name.</param>
        /// <param name="delayMilliseconds">[Optional] The delay milliseconds before the message is available in the queue.</param>
        private void PublishMessage <T> (T msg, string exchange, string queueName, int delayMilliseconds)
        {
            // set persistent property
            IBasicProperties basicProperties = null;

            if (_publishPersistent)
            {
                if (basicProperties == null)
                {
                    basicProperties = ConsumerChannel.CreateBasicProperties();
                }
                basicProperties.Persistent = _publishPersistent;
            }

            if (_retryDelayMilliseconds > 0)
            {
                if (basicProperties == null)
                {
                    basicProperties = ConsumerChannel.CreateBasicProperties();
                }
                basicProperties.AddHeader("x-delay", _retryDelayMilliseconds);
            }

            // publish message
            PublishChannel.BasicPublish(exchange: exchange ?? "",
                                        routingKey: queueName,
                                        basicProperties: basicProperties,
                                        body: GetMessageContent(msg));
        }
        private bool CheckRetryLimit(long retryLimit, RabbitWorkMessage msg)
        {
            IBasicProperties properties = msg.BasicProperties;

            if (retryLimit <= 0)
            {
                return(true);
            }

            // check dead-letter counter (number of times the message was dlx)
            long count = msg.GetRetryCount();

            // check dlx count against our threshold
            if (count >= retryLimit)
            {
                // move message to dead-letter queue
                if (String.IsNullOrEmpty(deadLetterQueue))
                {
                    // create dead letter queue
                    lock (QueueName)
                    {
                        deadLetterQueue = QueueName + ".dead-letter";
                        deadLetterQueue = EnsureQueueExists(deadLetterQueue, Mode);
                    }
                }
                // publish message to the deadletter queue
                PublishChannel.BasicPublish("", deadLetterQueue, (IBasicProperties)properties.Clone(), msg.Body);
                // delete message
                Ack(msg);
                return(false);
            }
            return(true);
        }
예제 #3
0
        /// <summary>
        ///     PublishAsync Publishes a message
        /// </summary>
        /// <param name="event">The name of the event to publish</param>
        /// <param name="data">The data of the event to publish</param>
        /// <param name="options">Optional options for this Publish</param>
        /// <returns>Task</returns>
        public Task PublishAsync(string @event, byte[] data, IBasicProperties options = null)
        {
            lock (PublishChannel)
            {
                PublishChannel.BasicPublish(Group, @event, false, options ?? new BasicProperties(),
                                            data);
            }

            return(Task.CompletedTask);
        }