Пример #1
0
        private Task CreateChannels() => Task.Run(() =>
        {
            var options    = this.options;
            var connection = this.connection;
            var controller = this.controller;

            foreach (var channelOptions in options.Channels)
            {
                var channelModel = connection.CreateModel();

                if (channelOptions.ChannelPrefetchCount.HasValue)
                {
                    channelModel.BasicQos(0, channelOptions.ChannelPrefetchCount.Value, true);
                }

                if (channelOptions.ConsumerPrefetchCount.HasValue)
                {
                    channelModel.BasicQos(0, channelOptions.ConsumerPrefetchCount.Value, false);
                }

                var channel = new RabbitMQChannel(channelOptions, channelModel);

                channels.Add(channel.Tag, channel);

                controller.On(LifecycleState.DeclareEntities, async() =>
                              await DeclareEntities(channel)
                              );

                controller.On(LifecycleState.SubscribeQueues, async() =>
                              await SubscribeQueues(channel)
                              );
            }
        });
Пример #2
0
        private async Task SubscribeQueues(RabbitMQChannel channel)
        {
            var services       = options.Services;
            var channelOptions = channel.Options;

            services.GetContextAccessor()
            .Set(channel);

            foreach (var subscriberFactory in channelOptions.SubscribersFactories)
            {
                await channel.Subscribe(subscriberFactory);
            }
        }
        public Task Initialize(RabbitMQChannel channel)
        => Task.Run(() => {
            var model = channel.Model;

            var consumer       = new EventingBasicConsumer(model);
            consumer.Received += (_, ea)
                                 => deliveryHandler.Handle(channel, ea)
                                 .GetAwaiter()
                                 .GetResult();

            model.BasicConsume(
                queue: queue,
                autoAck: acknowledgeMode == AcknowledgeMode.Automatic,
                consumer: consumer
                );
        });
        public async Task Handle(RabbitMQChannel channel, BasicDeliverEventArgs source)
        {
            asyncContextAccessor.Set(channel);

            var delivery = new RabbitMQDelivery(channel.Model, acknowledgeMode, source);

            if (acknowledgeMode == AcknowledgeMode.BeforeHandling)
            {
                await delivery.TryAcknowledge();
            }

            try
            {
                await handler.Handle(delivery);
            }
            catch (Exception ex)
            {
                await delivery.SetException(ex);
            }

            if (delivery.Exceptions.Any())
            {
                switch (exceptionMode)
                {
                case ExceptionMode.Hold:
                    return;

                case ExceptionMode.Reject:
                    await delivery.TryReject();

                    return;

                case ExceptionMode.Unacknowledge:
                    await delivery.TryUnacknowledge();

                    return;

                default:     // Ignore
                    break;
                }
            }

            if (acknowledgeMode == AcknowledgeMode.AfterHandling)
            {
                await delivery.TryAcknowledge();
            }
        }
Пример #5
0
        private async Task DeclareEntities(RabbitMQChannel channel)
        {
            var services       = options.Services;
            var channelOptions = channel.Options;
            var channelModel   = channel.Model;

            foreach (var exchangeOptions in channelOptions.ExchangesOptions)
            {
                channelModel.ExchangeDeclare(
                    exchangeOptions.Exchange,
                    exchangeOptions.Type,
                    exchangeOptions.Durable,
                    exchangeOptions.AutoDelete,
                    exchangeOptions.Arguments
                    );
            }

            foreach (var queueOptions in channelOptions.QueuesOptions)
            {
                var queueDeclaration = channelModel.QueueDeclare(
                    queueOptions.Queue,
                    queueOptions.Durable,
                    queueOptions.Exclusive,
                    queueOptions.AutoDelete,
                    queueOptions.Arguments
                    );

                var queue = new RabbitMQQueue(queueOptions, queueDeclaration.QueueName);

                channel.AddQueue(queue);

                services.GetContextAccessor()
                .Set(channel, queue);

                foreach (var action in queueOptions.OnDeclaredActions)
                {
                    await action(services);
                }
            }
        }
Пример #6
0
        public Task Initialize(RabbitMQChannel channel)
        => Task.Run(() => {
            var model = channel.Model;

            var queueName = model.QueueDeclare().QueueName;

            model.QueueBind(
                queue: queueName,
                exchange: exchange,
                routingKey: routingKey
                );

            var consumer       = new EventingBasicConsumer(model);
            consumer.Received += (_, ea)
                                 => deliveryHandler.Handle(channel, ea)
                                 .GetAwaiter()
                                 .GetResult();

            model.BasicConsume(
                queue: queueName,
                autoAck: acknowledgeMode == AcknowledgeMode.Automatic,
                consumer: consumer
                );
        });