示例#1
0
        /// <summary>
        /// Create a queue asynchronously.
        /// <para>Returns success or failure.</para>
        /// </summary>
        /// <param name="queueName"></param>
        /// <param name="durable"></param>
        /// <param name="exclusive"></param>
        /// <param name="autoDelete"></param>
        /// <param name="args"></param>
        /// <returns>A bool indicating failure.</returns>
        public async Task <bool> CreateQueueAsync(
            string queueName,
            bool durable    = true,
            bool exclusive  = false,
            bool autoDelete = false,
            IDictionary <string, object> args = null)
        {
            Guard.AgainstNullOrEmpty(queueName, nameof(queueName));

            var error    = false;
            var chanHost = await _channelPool.GetChannelAsync().ConfigureAwait(false);

            try
            {
                chanHost.GetChannel().QueueDeclare(
                    queue: queueName,
                    durable: durable,
                    exclusive: exclusive,
                    autoDelete: autoDelete,
                    arguments: args);
            }
            catch { error = true; }
            finally { await _channelPool.ReturnChannelAsync(chanHost, error).ConfigureAwait(false); }

            return(error);
        }
示例#2
0
        public async Task <bool> TransferMessageAsync(
            IChannelPool channelPool,
            string originQueueName,
            string targetQueueName)
        {
            Guard.AgainstNull(channelPool, nameof(channelPool));
            Guard.AgainstNullOrEmpty(originQueueName, nameof(originQueueName));
            Guard.AgainstNullOrEmpty(targetQueueName, nameof(targetQueueName));

            var error       = false;
            var channelHost = await channelPool.GetChannelAsync().ConfigureAwait(false);

            var properties = channelHost.GetChannel().CreateBasicProperties();

            properties.DeliveryMode = 2;

            try
            {
                var result = channelHost.GetChannel().BasicGet(originQueueName, true);

                if (result?.Body != null)
                {
                    channelHost.GetChannel().BasicPublish(string.Empty, targetQueueName, false, properties, result.Body);
                }
            }
            catch { error = true; }
            finally
            {
                await channelPool
                .ReturnChannelAsync(channelHost, error);
            }

            return(error);
        }
示例#3
0
        public async Task <bool> PurgeQueueAsync(
            IChannelPool channelPool,
            string queueName,
            bool deleteQueueAfter = false)
        {
            Guard.AgainstNull(channelPool, nameof(channelPool));
            Guard.AgainstNullOrEmpty(queueName, nameof(queueName));

            var error       = false;
            var channelHost = await channelPool.GetChannelAsync().ConfigureAwait(false);

            try
            {
                channelHost.GetChannel().QueuePurge(queueName);

                if (deleteQueueAfter)
                {
                    channelHost.GetChannel().QueueDelete(queueName, false, false);
                }
            }
            catch { error = true; }
            finally
            {
                await channelPool
                .ReturnChannelAsync(channelHost, error);
            }

            return(error);
        }
        public async Task <bool> TransferAllMessagesAsync(
            IChannelPool originChannelPool,
            IChannelPool targetChannelPool,
            string originQueueName,
            string targetQueueName)
        {
            Guard.AgainstNull(originChannelPool, nameof(originChannelPool));
            Guard.AgainstNull(targetChannelPool, nameof(targetChannelPool));
            Guard.AgainstNullOrEmpty(originQueueName, nameof(originQueueName));
            Guard.AgainstNullOrEmpty(targetQueueName, nameof(targetQueueName));

            var error       = false;
            var channelHost = await originChannelPool.GetChannelAsync().ConfigureAwait(false);

            var properties = channelHost.Channel.CreateBasicProperties();

            properties.DeliveryMode = 2;

            BasicGetResult result = null;

            while (true)
            {
                try
                {
                    result = channelHost.Channel.BasicGet(originQueueName, true);
                    if (result == null)
                    {
                        break;
                    }
                }
                catch { error = true; }
                finally
                {
                    await originChannelPool
                    .ReturnChannelAsync(channelHost, error);
                }

                if (!error && result?.Body != null)
                {
                    try
                    {
                        var targetChannelHost = await targetChannelPool.GetChannelAsync().ConfigureAwait(false);

                        targetChannelHost.Channel.BasicPublish(string.Empty, targetQueueName, false, properties, result.Body);
                    }
                    catch { error = true; }
                    finally
                    {
                        await targetChannelPool
                        .ReturnChannelAsync(channelHost, error);
                    }
                }
            }

            return(error);
        }
示例#5
0
        // A basic implementation of publish but using the ChannelPool. If message properties is null, one is created and all messages are set to persistent.
        public async Task <bool> PublishAsync(
            string exchangeName,
            string routingKey,
            ReadOnlyMemory <byte> payload,
            bool mandatory = false,
            IBasicProperties messageProperties = null,
            string messageId = null)
        {
            Guard.AgainstBothNullOrEmpty(exchangeName, nameof(exchangeName), routingKey, nameof(routingKey));

            var error       = false;
            var channelHost = await _channelPool.GetChannelAsync().ConfigureAwait(false);

            if (messageProperties == null)
            {
                messageProperties = channelHost.GetChannel().CreateBasicProperties();
                messageProperties.DeliveryMode = 2;
                messageProperties.MessageId    = messageId ?? Guid.NewGuid().ToString();

                if (!messageProperties.IsHeadersPresent())
                {
                    messageProperties.Headers = new Dictionary <string, object>();
                }
            }

            // Non-optional Header.
            messageProperties.Headers[Constants.HeaderForObjectType] = Constants.HeaderValueForMessage;

            try
            {
                channelHost
                .GetChannel()
                .BasicPublish(
                    exchange: exchangeName ?? string.Empty,
                    routingKey: routingKey,
                    mandatory: mandatory,
                    basicProperties: messageProperties,
                    body: payload);
            }
            catch (Exception ex)
            {
                _logger.LogDebug(LogMessages.Publishers.PublishFailed, $"{exchangeName}->{routingKey}", ex.Message);
                error = true;
            }
            finally
            {
                await _channelPool
                .ReturnChannelAsync(channelHost, error);
            }

            return(error);
        }