Example #1
0
        public static IApplicationBuilder SilverbackConfigure(this IApplicationBuilder app, BusConfigurator busConfigurator, IConfiguration configuration)
        {
            var uri = configuration.GetSection("KafkaConfiguration:Uri").Value;
            var maxFailedAttempts     = 3;
            var ConfigurationConsumer = new KafkaConsumerConfig
            {
                BootstrapServers = uri,
                GroupId          = "Geo"
            };
            var ConfigurationProducer = new KafkaProducerConfig
            {
                BootstrapServers = uri
            };

            busConfigurator.Connect(endpoints => endpoints
                                    .AddInbound(
                                        new KafkaConsumerEndpoint("create-geolocalization")
            {
                Configuration = ConfigurationConsumer
            }, policy => policy.Chain(
                                            policy.Move(new KafkaProducerEndpoint("retry_5M_topic")
            {
                Configuration = new KafkaProducerConfig {
                    BootstrapServers = configuration["Kafka:Uri"]
                }
            })
                                            .MaxFailedAttempts(maxFailedAttempts),
                                            policy.Move(new KafkaProducerEndpoint("retry_30M_topic")
            {
                Configuration = new KafkaProducerConfig {
                    BootstrapServers = configuration["Kafka:Uri"]
                }
            })
                                            .MaxFailedAttempts(maxFailedAttempts),
                                            policy.Move(new KafkaProducerEndpoint("retry_60M_topic")
            {
                Configuration = new KafkaProducerConfig {
                    BootstrapServers = configuration["Kafka:Uri"]
                }
            })
                                            .MaxFailedAttempts(maxFailedAttempts),
                                            policy.Move(new KafkaProducerEndpoint("failed_topic")
            {
                Configuration = new KafkaProducerConfig {
                    BootstrapServers = configuration["Kafka:Uri"]
                }
            })
                                            .MaxFailedAttempts(maxFailedAttempts)))
                                    .AddOutbound <GeocodificadoEvent>(
                                        new KafkaProducerEndpoint("geocodification")
            {
                Configuration = ConfigurationProducer
            }));


            return(app);
        }
Example #2
0
        public void IsAutoCommitEnabled_CorrectlySet(bool?enableAutoCommit, bool expected)
        {
            var config = new KafkaConsumerConfig
            {
                EnableAutoCommit = enableAutoCommit
            };

            config.IsAutoCommitEnabled.Should().Be(expected);
        }
Example #3
0
        public KafkaConsumer(
            IServiceProvider serviceProvider,
            ILogger <KafkaConsumer> logger,
            IConfiguration configuration
            )
        {
            this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
            this.logger          = logger ?? throw new ArgumentNullException(nameof(logger));

            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            // get configuration from appsettings.json
            config = configuration.GetKafkaConsumerConfig();
        }
        public KafkaConsumer(IServiceScopeFactory serviceScopeFactory, IHostApplicationLifetime application,
                             ILogger <KafkaConsumer <TMessage, THandler> > logger, IOptions <ConsumerConfig> config,
                             IOptions <KafkaConsumerConfig <TMessage> > consumerConfig)
        {
            _logger              = logger;
            _application         = application;
            _serviceScopeFactory = serviceScopeFactory;
            _config              = consumerConfig.Value;
            _builder             = new ConsumerBuilder <Null, TMessage>(config.Value)
                                   .SetValueDeserializer(KafkaDataConverter <TMessage> .Instance);

            _policy = Policy
                      .Handle <Exception>()
                      .WaitAndRetryAsync(_config.RetryCount,
                                         i => _config.RetryInterval * i,
                                         (ex, _, retry, _) =>
            {
                var level = retry > _config.RetryCount * 0.7
                            ? LogLevel.Critical
                            : LogLevel.Warning;

                _logger.Log(level, ex, "Error occured in kafka handler for '{N}'", _config.MessageType.Name);
            });
        }