Beispiel #1
0
        public RabbitMQHost(RabbitMQHostOptions options)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));

            var factory = new ConnectionFactory()
            {
                HostName = _options.HostName,
                UserName = _options.UserName,
                Password = _options.Password
            };

            factory.AutomaticRecoveryEnabled = true;
            factory.TopologyRecoveryEnabled  = true;

            _connection = factory.CreateConnection();
            _channel    = _connection.CreateModel();

            var consumer = new EventingBasicConsumer(_channel);

            consumer.Received += (model, ea) =>
            {
                Task.Run(async() => await ProcessMessage(ea));
            };

            _consumerTag = _channel.BasicConsume(queue: _options.MessageQueue,
                                                 autoAck: false, consumer: consumer);
        }
        public static IGlobalConfiguration <RabbitMQHost> UseRabbitMQHost(
            [NotNull] this IGlobalConfiguration configuration,
            [NotNull] RabbitMQHostOptions options)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var queueHost = new RabbitMQHost(options);

            return(configuration.UseQueueHost(queueHost));
        }
Beispiel #3
0
        public static IGlobalConfiguration <RabbitMQHost> UseRabbitMQHost(
            [NotNull] this IGlobalConfiguration configuration,
            IConfiguration config)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var options = new RabbitMQHostOptions()
            {
                AmqpUri = config["AccessData:RabbitMQ:AmqpUri"]
            };

            var sections = config.GetSection("AccessData:RabbitMQ:Queues");

            foreach (var section in sections.GetChildren())
            {
                if (!Int32.TryParse(config[$"{section.Path}:NodesCount"], out var nodes))
                {
                    nodes = 1;
                }

                if (!UInt16.TryParse(config[$"{section.Path}:PrefetchCount"], out var prefetch))
                {
                    prefetch = 1;
                }

                options.Queues.Add(new RabbitMQHostOptions.Queue()
                {
                    Name          = config[$"{section.Path}:Name"],
                    NodesCount    = nodes,
                    PrefetchCount = prefetch
                });
            }

            sections = config.GetSection("AccessData:RabbitMQ:RetryInMilliseconds");
            foreach (var section in sections.GetChildren())
            {
                options.RetryInMilliseconds.Add(section.Value);
            }

            return(configuration.UseRabbitMQHost(options));
        }
Beispiel #4
0
        public RabbitMQHost(RabbitMQHostOptions options)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));

            SetUpQueues();

            var factory = ConnectionFactory();

            foreach (var queue in _options.Queues)
            {
                for (var i = 0; i < queue.NodesCount; i++)
                {
                    var connection = factory.CreateConnection(@"FoxyLink client");
                    _connections.Add(connection);

                    var channel = connection.CreateModel();
                    _channels.Add(channel);

                    channel.BasicQos(0, queue.PrefetchCount, false);
                    var consumer = new AsyncEventingBasicConsumer(channel);
                    consumer.Received += async(o, ea) =>
                    {
                        try
                        {
                            await SendMessageToEndpoint(ea, queue.Name);
                        }
                        catch (Exception ex)
                        {
                            MoveToInvalidQueue(ea, ex.ToString());
                        }
                        finally
                        {
                            channel.BasicAck(deliveryTag: ea.DeliveryTag,
                                             multiple: false);
                        }
                    };
                    channel.BasicConsume(consumer, queue.Name, autoAck: false);
                }
            }
        }