Ejemplo n.º 1
0
        public void Connect()
        {
            if (_consumerClient != null)
            {
                return;
            }

            _connectionLock.Wait();

            try
            {
                if (_consumerClient == null)
                {
                    _kafkaOptions.MainConfig["group.id"]          = _groupId;
                    _kafkaOptions.MainConfig["auto.offset.reset"] = "earliest";
                    var config = _kafkaOptions.AsKafkaConfig();

                    _consumerClient = new ConsumerBuilder <string, byte[]>(config)
                                      .SetErrorHandler(ConsumerClient_OnConsumeError)
                                      .Build();
                }
            }
            finally
            {
                _connectionLock.Release();
            }
        }
Ejemplo n.º 2
0
        public ConnectionPool(ILogger <ConnectionPool> logger, KafkaOptions options)
        {
            _logger        = logger;
            _pool          = new ConcurrentQueue <Producer>();
            _maxSize       = options.ConnectionPoolSize;
            _activator     = CreateActivator(options);
            ServersAddress = options.Servers;

            _logger.LogDebug("Kafka configuration of CAP :\r\n {0}",
                             JsonConvert.SerializeObject(options.AsKafkaConfig(), Formatting.Indented));
        }
Ejemplo n.º 3
0
        private void InitKafkaClient()
        {
            _kafkaOptions.MainConfig["group.id"] = _groupId;

            var config = _kafkaOptions.AsKafkaConfig();

            _consumerClient = new Consumer <Null, string>(config, null, StringDeserializer);
            _consumerClient.OnConsumeError += ConsumerClient_OnConsumeError;
            _consumerClient.OnMessage      += ConsumerClient_OnMessage;
            _consumerClient.OnError        += ConsumerClient_OnError;
        }
Ejemplo n.º 4
0
 private void InitKafkaClient()
 {
     lock (_kafkaOptions)
     {
         _kafkaOptions.MainConfig["group.id"]          = _groupId;
         _kafkaOptions.MainConfig["auto.offset.reset"] = "earliest";
         var config = _kafkaOptions.AsKafkaConfig();
         _consumerClient = new ConsumerBuilder <Null, string>(config)
                           .SetErrorHandler(ConsumerClient_OnConsumeError)
                           .Build();
     }
 }
Ejemplo n.º 5
0
        public IProducer <Null, string> RentProducer()
        {
            if (_producerPool.TryDequeue(out var producer))
            {
                Interlocked.Decrement(ref _pCount);

                return(producer);
            }

            producer = new ProducerBuilder <Null, string>(_options.AsKafkaConfig()).Build();

            return(producer);
        }
Ejemplo n.º 6
0
 private static Func <Producer> CreateActivator(KafkaOptions options)
 {
     return(() => new Producer(options.AsKafkaConfig()));
 }
Ejemplo n.º 7
0
        public ConnectionPool(ILogger <ConnectionPool> logger, IOptions <KafkaOptions> options)
        {
            _options      = options.Value;
            _producerPool = new ConcurrentQueue <IProducer <Null, string> >();
            _maxSize      = _options.ConnectionPoolSize;

            logger.LogDebug("Kafka configuration of CAP :\r\n {0}", JsonConvert.SerializeObject(_options.AsKafkaConfig(), Formatting.Indented));
        }