public async Task DeclareAsync()
        {
            bool exists = await _bunny.ExchangeExistsAsync(Name);

            if (exists)
            {
                return;
            }
            IModel channel = null;

            try
            {
                channel = _bunny.Channel(newOne: true);

                await Task.Run(() =>
                {
                    channel.ExchangeDeclare(Name, ExchangeType, Durable, AutoDelete, _args);
                });
            }
            catch (System.Exception exc)
            {
                throw DeclarationException.DeclareFailed(exc, "exchange-declare failed!");
            }
            finally
            {
                channel.Close();
            }
        }
        ///<summary>
        /// Enter Queue DeclarationMode
        ///</summary>
        public static IQueue Queue(this IDeclare declare, string name)
        {
            if ((declare is DeclareBase) == false)
            {
                throw DeclarationException.WrongType(typeof(DeclareBase), declare);
            }

            IBunny bunny = CheckGetBunny(declare, name, "queue");

            return(new DeclareQueue(bunny, name));
        }
 public IPublish <T> WithConfirm(Func <BasicAckEventArgs, Task> onAck, Func <BasicNackEventArgs, Task> onNack)
 {
     if (onAck == null ||  onNack == null)
     {
         throw DeclarationException.Argument(new ArgumentException("handlers for ack and nack must not be null"));
     }
     _useConfirm   = true;
     _ackCallback  = onAck;
     _nackCallback = onNack;
     return(this);
 }
Beispiel #4
0
 public DeclareResponder(IBunny bunny, string rpcExchange, string fromQueue, Func <TRequest, Task <TResponse> > respond)
 {
     if (respond == null)
     {
         throw DeclarationException.Argument(new ArgumentException("respond delegate must not be null"));
     }
     _bunny            = bunny;
     _respond          = respond;
     _rpcExchange      = rpcExchange;
     _serialize        = Config.Serialize;
     _consumeFromQueue = fromQueue;
     _thisChannel      = new PermanentChannel(bunny);
     _deserialize      = Config.Deserialize <TRequest>;
 }
 private static IBunny CheckGetBunny(IDeclare declare, string toCheck, string errorPrefix)
 {
     if (string.IsNullOrWhiteSpace(toCheck))
     {
         var arg = new ArgumentException($"{errorPrefix}-name must not be null-or-whitespace");
         throw DeclarationException.Argument(arg);
     }
     if (toCheck.Length > 255)
     {
         var arg = new ArgumentException($"{errorPrefix}-length must be less than or equal to 255 character");
         throw DeclarationException.Argument(arg);
     }
     return(declare.Bunny);
 }
        private void AttemptPassiveDeclarations()
        {
            DeclarationException failures = null;

            foreach (var queueName in Queues)
            {
                try
                {
                    try
                    {
                        Channel.QueueDeclarePassive(queueName);
                    }
                    catch (RC.Exceptions.WireFormattingException e)
                    {
                        try
                        {
                            if (Channel is IChannelProxy)
                            {
                                ((IChannelProxy)Channel).TargetChannel.Close();
                            }
                        }
                        catch (TimeoutException)
                        {
                        }

                        throw new FatalListenerStartupException("Illegal Argument on Queue Declaration", e);
                    }
                }
                catch (RC.Exceptions.RabbitMQClientException e)
                {
                    Logger?.LogWarning("Failed to declare queue: {name} ", queueName);
                    if (!Channel.IsOpen)
                    {
                        throw new RabbitIOException(e);
                    }

                    if (failures == null)
                    {
                        failures = new DeclarationException(e);
                    }

                    failures.AddFailedQueue(queueName);
                }
            }

            if (failures != null)
            {
                throw failures;
            }
        }
        internal static async Task <bool> ExchangeExistsAsync(this IBunny bunny, string name)
        {
            try
            {
                var channel = bunny.Channel(newOne: true);
                await Task.Run(() => channel.ExchangeDeclarePassive(name));

                return(true);
            }
            catch (RabbitMQ.Client.Exceptions.OperationInterruptedException)
            {
                return(false);
            }
            catch (Exception ex)
            {
                throw DeclarationException.DeclareFailed(ex);
            }
        }
        internal static async Task <bool> QueueExistsAsync(this IBunny bunny, string name)
        {
            try
            {
                var channel = bunny.Channel(newOne: true);
                var result  = await new TaskFactory().StartNew <QueueDeclareOk>(() => channel.QueueDeclarePassive(name));

                return(true);
            }
            catch (RabbitMQ.Client.Exceptions.OperationInterruptedException)
            {
                return(false);
            }
            catch (Exception ex)
            {
                throw DeclarationException.DeclareFailed(ex);
            }
        }
        private void HandleDeclarationException(int passiveDeclareRetries, DeclarationException e)
        {
            if (passiveDeclareRetries > 0 && Channel.IsOpen)
            {
                Logger?.LogWarning(e, "Queue declaration failed; retries left={retries}", passiveDeclareRetries);
                try
                {
                    Thread.Sleep(FailedDeclarationRetryInterval);
                }
                catch (Exception e1)
                {
                    Declaring = false;
                    ActiveObjectCounter.Release(this);
                    throw RabbitExceptionTranslator.ConvertRabbitAccessException(e1);
                }
            }
            else if (e.FailedQueues.Count < Queues.Count)
            {
                Logger?.LogWarning("Not all queues are available; only listening on those that are - configured: {queues}; not available: {notavail}", string.Join(',', Queues), string.Join(',', e.FailedQueues));
                lock (MissingQueues)
                {
                    foreach (var q in e.FailedQueues)
                    {
                        MissingQueues.Add(q);
                    }
                }

                LastRetryDeclaration = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            }
            else
            {
                Declaring = false;
                ActiveObjectCounter.Release(this);
                throw new QueuesNotAvailableException("Cannot prepare queue for listener. Either the queue doesn't exist or the broker will not allow us to use it.", e);
            }
        }
 public Task DeclareAsync()
 {
     throw DeclarationException.BaseNotValid();
 }