示例#1
0
        public void OnCreate(Connection.IConnection connection)
        {
            _logger?.LogDebug("OnCreate for connection: {connection}", connection?.ToString());

            if (Interlocked.CompareExchange(ref _initializing, 1, 0) != 0)
            {
                return;
            }

            try
            {
                /*
                 * ...but it is possible for this to happen twice in the same ConnectionFactory (if more than
                 * one concurrent Connection is allowed). It's idempotent, so no big deal (a bit of network
                 * chatter). In fact it might even be a good thing: exclusive queues only make sense if they are
                 * declared for every connection. If anyone has a problem with it: use auto-startup="false".
                 */
                if (RetryTemplate != null && !RetryDisabled)
                {
                    RetryTemplate.Execute(
                        c =>
                    {
                        Initialize();
                    });
                }
                else
                {
                    Initialize();
                }
            }
            finally
            {
                Interlocked.CompareExchange(ref _initializing, 0, 1);
            }
        }
示例#2
0
            public SimpleConsumer(DirectMessageListenerContainer container, Connection.IConnection connection, R.IModel channel, string queue, ILogger logger = null)
                : base(channel)
            {
                _container  = container;
                _connection = connection;
                Queue       = queue;
                AckRequired = !_container.AcknowledgeMode.IsAutoAck() && !_container.AcknowledgeMode.IsManual();
                if (channel is IChannelProxy)
                {
                    _targetChannel = ((IChannelProxy)channel).TargetChannel;
                }
                else
                {
                    _targetChannel = null;
                }

                _logger              = logger;
                TransactionManager   = _container.TransactionManager;
                TransactionAttribute = _container.TransactionAttribute;
                IsRabbitTxManager    = TransactionManager is RabbitTransactionManager;
                ConnectionFactory    = _container.ConnectionFactory;
                MessagesPerAck       = _container.MessagesPerAck;
                LastAck              = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                AckTimeout           = _container.AckTimeout;
            }
示例#3
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);
        }
示例#4
0
 public void OnClose(Connection.IConnection connection)
 {
     _logger?.LogDebug("OnClose for connection: {connection}", connection?.ToString());
 }
 public MockConnection(Connection.IConnection deleg)
 {
     Delegate = deleg;
 }
示例#6
0
        private void DoConsumeFromQueue(string queue)
        {
            if (!IsActive)
            {
                _logger?.LogDebug("Consume from queue " + queue + " ignore, container stopping");
                return;
            }

            var routingLookupKey = GetRoutingLookupKey();

            if (routingLookupKey != null)
            {
                SimpleResourceHolder.Push(GetRoutingConnectionFactory(), routingLookupKey);
            }

            Connection.IConnection connection = null;
            try
            {
                connection = ConnectionFactory.CreateConnection();
            }
            catch (Exception e)
            {
                // publishConsumerFailedEvent(e.getMessage(), false, e);
                _logger?.LogError("Exception while CreateConnection", e);
                AddConsumerToRestart(new SimpleConsumer(this, null, null, queue));
                throw;

                // throw e instanceof AmqpConnectException
                // ? (AmqpConnectException)e
                // : new AmqpConnectException(e);
            }
            finally
            {
                if (routingLookupKey != null)
                {
                    SimpleResourceHolder.Pop(GetRoutingConnectionFactory());
                }
            }

            var consumer = Consume(queue, connection);

            lock (_consumersMonitor)
            {
                if (consumer != null)
                {
                    _cancellationLock.Add(consumer);
                    _consumers.Add(consumer);
                    _consumersByQueue.TryGetValue(queue, out var list);
                    if (list == null)
                    {
                        list = new List <SimpleConsumer>();
                        _consumersByQueue.Add(queue, list);
                    }

                    list.Add(consumer);
                    _logger?.LogInformation(consumer + " started");

                    // if (getApplicationEventPublisher() != null)
                    // {
                    //    getApplicationEventPublisher().publishEvent(new AsyncConsumerStartedEvent(this, consumer));
                    // }
                }
            }
        }