Esempio n. 1
0
        private SimpleConsumer Consume(string queue, Connection.IConnection connection)
        {
            R.IModel       channel  = null;
            SimpleConsumer consumer = null;

            try
            {
                channel = connection.CreateChannel(IsChannelTransacted);
                channel.BasicQos(0, (ushort)PrefetchCount, false);  // TODO: Verify this
                consumer = new SimpleConsumer(this, connection, channel, queue);
                channel.QueueDeclarePassive(queue);
                consumer.ConsumerTag = channel.BasicConsume(
                    queue,
                    AcknowledgeMode.IsAutoAck(),
                    ConsumerTagStrategy != null ? ConsumerTagStrategy.CreateConsumerTag(queue) : string.Empty,
                    NoLocal,
                    Exclusive,
                    ConsumerArguments,
                    consumer);
            }

            // catch (AmqpApplicationContextClosedException e)
            // {
            //    throw new AmqpConnectException(e);
            // }
            catch (Exception e)
            {
                RabbitUtils.CloseChannel(channel, _logger);
                RabbitUtils.CloseConnection(connection, _logger);

                consumer = HandleConsumeException(queue, consumer, e);
            }

            return(consumer);
        }
        private void CheckMissingQueues()
        {
            var now = DateTimeOffset.Now.ToUnixTimeMilliseconds();

            if (now - RetryDeclarationInterval > LastRetryDeclaration)
            {
                lock (MissingQueues)
                {
                    List <string> toRemove = new List <string>();
                    Exception     error    = null;
                    foreach (var queueToCheck in MissingQueues)
                    {
                        bool        available       = true;
                        IConnection connection      = null;
                        RC.IModel   channelForCheck = null;
                        try
                        {
                            channelForCheck = ConnectionFactory.CreateConnection().CreateChannel(false);
                            channelForCheck.QueueDeclarePassive(queueToCheck);
                            Logger?.LogInformation("Queue '{queue}' is now available", queueToCheck);
                        }
                        catch (Exception e)
                        {
                            available = false;
                            Logger?.LogWarning(e, "Queue '{queue}' is not available", queueToCheck);
                        }
                        finally
                        {
                            RabbitUtils.CloseChannel(channelForCheck);
                            RabbitUtils.CloseConnection(connection);
                        }

                        if (available)
                        {
                            try
                            {
                                ConsumeFromQueue(queueToCheck);
                                toRemove.Add(queueToCheck);
                            }
                            catch (Exception e)
                            {
                                error = e;
                                break;
                            }
                        }
                    }

                    if (toRemove.Count > 0)
                    {
                        foreach (var remove in toRemove)
                        {
                            MissingQueues.Remove(remove);
                        }
                    }

                    if (error != null)
                    {
                        throw RabbitExceptionTranslator.ConvertRabbitAccessException(error);
                    }
                }

                LastRetryDeclaration = now;
            }
        }