Exemplo n.º 1
0
        public static void AddKafkaConsumer <T>(this IServiceCollection services,
                                                Action <KafkaConfiguration> configurationBuilder,
                                                ServiceLifetime lifetime = ServiceLifetime.Transient) where T : KafkaConsumer
        {
            if (configurationBuilder == null)
            {
                throw new InvalidOperationException(ConfigurationBuilderCannotBeNullMessage);
            }

            var config = new KafkaConfiguration();

            configurationBuilder(config);
            var ctor = typeof(T)
                       .GetConstructors()
                       .First();
            var parameterTypes = ctor
                                 .GetParameters()
                                 .Select(p => p.ParameterType)
                                 .ToArray();

            Func <IServiceProvider, T> implementationFactory = provider =>
            {
                var parameters = parameterTypes
                                 .Select(provider.GetRequiredService)
                                 .ToArray();
                var instance       = ctor.Invoke(parameters);
                var castedInstance = instance as T;
                castedInstance !.Initialize(config);
                return(castedInstance);
            };

            var descriptor = new ServiceDescriptor(typeof(T), implementationFactory, lifetime);

            services.Add(descriptor);
        }
Exemplo n.º 2
0
        public void Initialize(KafkaConfiguration configuration)
        {
            if (_initialized)
            {
                throw new InvalidOperationException(KafkaConsumerIsAlreadyInitializedMessage);
            }

            _configuration = configuration;

            ValidateConfiguration(_configuration);

            Consumer = BuildConsumer(configuration);

            _initialized = true;
        }
Exemplo n.º 3
0
        private IConsumer <string, string> BuildConsumer(KafkaConfiguration configuration)
        {
            var consumerConfig = configuration as ConsumerConfig;

            return(new ConsumerBuilder <string, string>(consumerConfig)
                   .SetErrorHandler(configuration.ErrorHandler)
                   .SetKeyDeserializer(configuration.KeyDeserializer)
                   .SetValueDeserializer(configuration.ValueDeserializer)
                   .SetLogHandler(configuration.LogHandler)
                   .SetStatisticsHandler(configuration.StatisticsHandler)
                   .SetOffsetsCommittedHandler(configuration.OffsetsCommittedHandler)
                   .SetPartitionsAssignedHandler(configuration.PartitionsAssignedHandler)
                   .SetPartitionsRevokedHandler(configuration.PartitionsRevokedHandler)
                   .Build());
        }
Exemplo n.º 4
0
        private void ValidateConfiguration(KafkaConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(KafkaConfiguration));
            }

            var topics = configuration.Topics;

            if (topics == null)
            {
                throw new ArgumentNullException(nameof(KafkaConfiguration.Topics));
            }

            foreach (var topic in topics)
            {
                if (topic == null)
                {
                    throw new ArgumentNullException(nameof(topic));
                }
            }
        }
Exemplo n.º 5
0
        private async Task StartConsumeLoop(KafkaConfiguration kafkaConfiguration, CancellationToken token)
        {
            // KafkaConsumer is initialized and Consumer cannot be null
            // Topics is validated, and cannot be null, or cannot contain null topic
            Consumer.Subscribe(kafkaConfiguration.Topics);

            try
            {
                while (!token.IsCancellationRequested)
                {
                    try
                    {
                        var result = null as ConsumeResult <string, string>;

                        try
                        {
                            result = Consumer.Consume(token);
                        }
                        catch (ConsumeException e)
                        {
                            await OnError(e, result);

                            continue;
                        }

                        if (result.Message == null)
                        {
                            continue;
                        }

                        try
                        {
                            await OnConsume(result);
                        }
                        catch (Exception e)
                        {
                            await OnError(e, result);
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        break;
                    }
                    catch (ObjectDisposedException)
                    {
                        break;
                    }
                    catch (AccessViolationException)
                    {
                        break;
                    }
                    catch (BadImageFormatException)
                    {
                        break;
                    }
                    catch (Exception e)
                    {
                        await LogError(e);
                    }
                }
            }
            finally
            {
                Consumer.Close();
                _disposed = true;
            }
        }
Exemplo n.º 6
0
 public KafkaConsumer(KafkaConfiguration configuration)
 {
     Initialize(configuration);
 }