/// <inheritdoc cref="IStartable.Start"/>
        public void Start()
        {
            var rabbitMqSubscriptionSettings = RabbitMqSubscriptionSettings.ForSubscriber(
                _connectionString,
                _exchangeName,
                _queueName);

            if (_isDurable)
            {
                rabbitMqSubscriptionSettings = rabbitMqSubscriptionSettings.MakeDurable();
            }

            _subscriber = new RabbitMqSubscriber <TMessage>(
                _logFactory,
                rabbitMqSubscriptionSettings,
                new ResilientErrorHandlingStrategy(
                    _logFactory,
                    rabbitMqSubscriptionSettings,
                    TimeSpan.FromSeconds(10),
                    next: new DeadQueueErrorHandlingStrategy(_logFactory, rabbitMqSubscriptionSettings)))
                          .SetMessageDeserializer(new JsonMessageDeserializer <TMessage>())
                          .SetPrefetchCount(_prefetchCount)
                          .Subscribe(ProcessMessageAsync)
                          .CreateDefaultBinding()
                          .Start();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Registers type inherited from <see cref="JsonRabbitSubscriber{TMessage}"/> in Autofac container.
        /// </summary>
        /// <typeparam name="TSubscriber">Class type.</typeparam>
        /// <typeparam name="TMessage">Message type.</typeparam>
        /// <param name="builder">Autofac container builder.</param>
        /// <param name="rabbitMqConnString">Connection string to RabbitMq.</param>
        /// <param name="exchangeName">Exchange name.</param>
        /// <param name="queueName">Queu name.</param>
        public static IRegistrationBuilder <TSubscriber, ConcreteReflectionActivatorData, SingleRegistrationStyle> RegisterJsonRabbitSubscriber <TSubscriber, TMessage>(
            [NotNull] this ContainerBuilder builder,
            [NotNull] string rabbitMqConnString,
            [NotNull] string exchangeName,
            [NotNull] string queueName
            ) where TSubscriber : JsonRabbitSubscriber <TMessage>
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (string.IsNullOrWhiteSpace(rabbitMqConnString))
            {
                throw new ArgumentNullException(nameof(rabbitMqConnString));
            }

            if (string.IsNullOrWhiteSpace(exchangeName))
            {
                throw new ArgumentNullException(nameof(exchangeName));
            }

            if (string.IsNullOrWhiteSpace(queueName))
            {
                throw new ArgumentNullException(nameof(queueName));
            }

            var settings = RabbitMqSubscriptionSettings.ForSubscriber(
                rabbitMqConnString,
                exchangeName,
                queueName);

            return(builder.RegisterType <TSubscriber>()
                   .As <IStartStop>()
                   .WithParameter(TypedParameter.From(settings))
                   .SingleInstance());
        }