/// <summary>
        /// Unbind an Exchange from another Exchange asynchronously.
        /// <para>Returns success or failure.</para>
        /// </summary>
        /// <param name="childExchangeName"></param>
        /// <param name="parentExchangeName"></param>
        /// <param name="routingKey"></param>
        /// <param name="args"></param>
        /// <returns>A bool indicating success or failure.</returns>
        public async Task <bool> ExchangeUnbindFromExchangeAsync(string childExchangeName, string parentExchangeName, string routingKey = "",
                                                                 IDictionary <string, object> args = null)
        {
            Guard.AgainstNullOrEmpty(parentExchangeName, nameof(parentExchangeName));
            Guard.AgainstNullOrEmpty(childExchangeName, nameof(childExchangeName));

            var success     = false;
            var channelPair = await RabbitChannelPool.GetPooledChannelPairAsync().ConfigureAwait(false);

            try
            {
                channelPair.Channel.ExchangeUnbind(destination: childExchangeName,
                                                   source: parentExchangeName,
                                                   routingKey: routingKey,
                                                   arguments: args);

                success = true;
            }
            catch (Exception e)
            {
                await HandleErrorAsync(e, channelPair.ChannelId, new { channelPair.ChannelId }).ConfigureAwait(false);

                if (_seasoning.ThrowExceptions)
                {
                    throw;
                }
            }
            finally { RabbitChannelPool.ReturnChannelToPool(channelPair); }

            return(success);
        }
        /// <summary>
        /// Create an Exchange asynchronously.
        /// <para>Returns success or failure.</para>
        /// </summary>
        /// <param name="exchangeName"></param>
        /// <param name="exchangeType"></param>
        /// <param name="durable"></param>
        /// <param name="autoDelete"></param>
        /// <param name="args"></param>
        /// <returns>A bool indicating success or failure.</returns>
        public async Task <bool> ExchangeDeclareAsync(string exchangeName, string exchangeType, bool durable = true,
                                                      bool autoDelete = false, IDictionary <string, object> args = null)
        {
            Guard.AgainstNullOrEmpty(exchangeName, nameof(exchangeName));

            var success     = false;
            var channelPair = await RabbitChannelPool.GetPooledChannelPairAsync().ConfigureAwait(false);

            try
            {
                channelPair.Channel.ExchangeDeclare(exchange: exchangeName,
                                                    type: exchangeType,
                                                    durable: durable,
                                                    autoDelete: autoDelete,
                                                    arguments: args);

                success = true;
            }
            catch (Exception e)
            {
                await HandleErrorAsync(e, channelPair.ChannelId, new { channelPair.ChannelId }).ConfigureAwait(false);

                if (_seasoning.ThrowExceptions)
                {
                    throw;
                }
            }
            finally { RabbitChannelPool.ReturnChannelToPool(channelPair); }

            return(success);
        }
        /// <summary>
        /// Delete an Exchange asynchronously.
        /// <para>Returns success or failure.</para>
        /// </summary>
        /// <param name="exchangeName"></param>
        /// <param name="onlyIfUnused"></param>
        /// <returns>A bool indicating success or failure.</returns>
        public async Task <bool> ExchangeDeleteAsync(string exchangeName, bool onlyIfUnused = false)
        {
            Guard.AgainstNullOrEmpty(exchangeName, nameof(exchangeName));

            var success     = false;
            var channelPair = await RabbitChannelPool.GetPooledChannelPairAsync().ConfigureAwait(false);

            try
            {
                channelPair.Channel.ExchangeDelete(exchange: exchangeName,
                                                   ifUnused: onlyIfUnused);

                success = true;
            }
            catch (Exception e)
            {
                await HandleErrorAsync(e, channelPair.ChannelId, new { channelPair.ChannelId }).ConfigureAwait(false);

                if (_seasoning.ThrowExceptions)
                {
                    throw;
                }
            }
            finally { RabbitChannelPool.ReturnChannelToPool(channelPair); }

            return(success);
        }
        /// <summary>
        /// Bind a queue to exchange asynchronously.
        /// <para>Returns success or failure.</para>
        /// </summary>
        /// <param name="queueName"></param>
        /// <param name="exchangeName"></param>
        /// <param name="routingKey"></param>
        /// <param name="args"></param>
        /// <returns>A bool indicating success or failure.</returns>
        public async Task <bool> QueueBindToExchangeAsync(string queueName, string exchangeName, string routingKey = "",
                                                          IDictionary <string, object> args = null)
        {
            Guard.AgainstNullOrEmpty(exchangeName, nameof(exchangeName));
            Guard.AgainstNullOrEmpty(queueName, nameof(queueName));

            var success     = false;
            var channelPair = await RabbitChannelPool.GetPooledChannelPairAsync().ConfigureAwait(false);

            try
            {
                channelPair.Channel.QueueBind(queue: queueName,
                                              exchange: exchangeName,
                                              routingKey: routingKey,
                                              arguments: args);

                success = true;
            }
            catch (Exception e)
            {
                await HandleErrorAsync(e, channelPair.ChannelId, new { channelPair.ChannelId }).ConfigureAwait(false);

                if (_seasoning.ThrowExceptions)
                {
                    throw;
                }
            }
            finally { RabbitChannelPool.ReturnChannelToPool(channelPair); }

            return(success);
        }
Example #5
0
        /// <summary>
        /// Transfers a number of messages from one queue to another queue.
        /// </summary>
        /// <param name="originQueueName">The queue to remove from.</param>
        /// <param name="targetQueueName">The destination queue.</param>
        /// <param name="count">Number of messages to transfer</param>
        /// <returns>A bool indicating success or failure.</returns>
        public async Task <bool> TransferMessagesAsync(string originQueueName, string targetQueueName, ushort count)
        {
            Guard.AgainstNullOrEmpty(originQueueName, nameof(originQueueName));
            Guard.AgainstNullOrEmpty(targetQueueName, nameof(targetQueueName));

            var success     = false;
            var channelPair = await RabbitChannelPool.GetPooledChannelPairAsync().ConfigureAwait(false);

            try
            {
                for (ushort i = 0; i < count; i++)
                {
                    var result = channelPair.Channel.BasicGet(originQueueName, true);

                    if (result?.Body != null)
                    {
                        channelPair.Channel.BasicPublish(string.Empty, targetQueueName, false, null, result.Body);
                    }
                }

                success = true;
            }
            catch (Exception e)
            {
                await HandleErrorAsync(e, channelPair.ChannelId, new { channelPair.ChannelId }).ConfigureAwait(false);

                if (_seasoning.ThrowExceptions)
                {
                    throw;
                }
            }
            finally { RabbitChannelPool.ReturnChannelToPool(channelPair); }

            return(success);
        }
Example #6
0
        /// <summary>
        /// Empty/purge the queue.
        /// </summary>
        /// <param name="queueName">The queue to remove from.</param>
        /// <param name="deleteQueueAfter">Indicate if you want to delete the queue after purge.</param>
        /// <returns>A bool indicating success or failure.</returns>
        public async Task <bool> PurgeQueueAsync(string queueName, bool deleteQueueAfter = false)
        {
            Guard.AgainstNullOrEmpty(queueName, nameof(queueName));

            var success     = false;
            var channelPair = await RabbitChannelPool.GetPooledChannelPairAsync().ConfigureAwait(false);

            try
            {
                channelPair.Channel.QueuePurge(queueName);

                if (deleteQueueAfter)
                {
                    channelPair.Channel.QueueDelete(queueName, false, false);
                }

                success = true;
            }
            catch (Exception e)
            {
                await HandleErrorAsync(e, channelPair.ChannelId, new { channelPair.ChannelId }).ConfigureAwait(false);

                if (_seasoning.ThrowExceptions)
                {
                    throw;
                }
            }
            finally { RabbitChannelPool.ReturnChannelToPool(channelPair); }

            return(success);
        }
        private async Task <bool> PublishTestMessageAsync(string queueName)
        {
            var success     = false;
            var channelPair = await RabbitChannelPool.GetPooledChannelPairAsync().ConfigureAwait(false);

            try
            {
                channelPair.Channel.BasicPublish(exchange: string.Empty,
                                                 routingKey: queueName,
                                                 body: _testPayload);

                success = true;
            }
            catch (Exception e)
            {
                await HandleErrorAsync(e, channelPair.ChannelId, new { channelPair.ChannelId }).ConfigureAwait(false);
            }
            finally { RabbitChannelPool.ReturnChannelToPool(channelPair); }

            return(success);
        }
Example #8
0
        private async Task <byte[]> GetTestMessageAsync(string queueName)
        {
            byte[] body        = null;
            var    channelPair = await RabbitChannelPool.GetPooledChannelPairAsync().ConfigureAwait(false);

            try
            {
                var result = channelPair.Channel.BasicGet(queueName, true);

                if (result?.Body != null)
                {
                    body = result.Body;
                }
            }
            catch (Exception e)
            {
                await HandleErrorAsync(e, channelPair.ChannelId, new { channelPair.ChannelId }).ConfigureAwait(false);
            }
            finally { RabbitChannelPool.ReturnChannelToPool(channelPair); }

            return(body);
        }