Exemple #1
0
        private void CreateConsumer()
        {
            _model = _connection.CreateModel();

            if (!_disablePrefetch)
            {
                _model.BasicQos(0, _prefetchCount, false);
            }

            var consumer = new AsyncEventingBasicConsumer(_model);

            consumer.Received += Event;

            _model.BasicConsume(_queueName, false, consumer);

            _logger.Debug("Started consuming");
        }
Exemple #2
0
        public void StartConsuming(string queueName, IList <string> messageTypes, ConsumerEventHandler eventHandler, IConfiguration config)
        {
            _transportSettings = config.TransportSettings;
            _durable           = !_transportSettings.ClientSettings.ContainsKey("Durable") || (bool)_transportSettings.ClientSettings["Durable"];
            _exclusive         = _transportSettings.ClientSettings.ContainsKey("Exclusive") && (bool)_transportSettings.ClientSettings["Exclusive"];
            _autoDelete        = _transportSettings.ClientSettings.ContainsKey("AutoDelete") && (bool)_transportSettings.ClientSettings["AutoDelete"];
            _queueArguments    = _transportSettings.ClientSettings.ContainsKey("Arguments") ? (IDictionary <string, object>)_transportSettings.ClientSettings["Arguments"] : new Dictionary <string, object>();
            _retryDelay        = _transportSettings.RetryDelay;

            if (_connection == null)
            {
                _connection = new Connection(config.TransportSettings, queueName, _logger);
            }

            if (_model == null)
            {
                _model = _connection.CreateModel();
            }

            // Configure exchanges
            foreach (string messageType in messageTypes)
            {
                ConfigureExchange(messageType, "fanout");
            }

            // Configure queue
            ConfigureQueue(queueName);

            // Purge all messages on queue
            if (_transportSettings.PurgeQueueOnStartup)
            {
                _logger.Debug("Purging queue");
                _model.QueuePurge(queueName);
            }

            // Configure retry queue
            ConfigureRetryQueue(queueName);

            // Configure Error Queue/Exchange
            var errorExchange = ConfigureErrorExchange();
            var errorQueue    = ConfigureErrorQueue();

            if (!string.IsNullOrEmpty(errorExchange))
            {
                _model.QueueBind(errorQueue, errorExchange, string.Empty, null);
            }

            // Configure Audit Queue/Exchange
            if (_transportSettings.AuditingEnabled)
            {
                var auditExchange = ConfigureAuditExchange();
                var auditQueue    = ConfigureAuditQueue();

                if (!string.IsNullOrEmpty(auditExchange))
                {
                    _model.QueueBind(auditQueue, auditExchange, string.Empty, null);
                }
            }

            var clientCount = config.Clients;

            for (int i = 0; i < clientCount; i++)
            {
                var client = new Client(_connection, config.TransportSettings, _logger);
                client.StartConsuming(eventHandler, queueName);
                foreach (string messageType in messageTypes)
                {
                    client.ConsumeMessageType(messageType);
                }
                _clients.Add(client);
            }
        }