Example #1
0
        public override void PreStart()
        {
            base.PreStart();

            _consumer = _settings.CreateKafkaConsumer();
            Log.Debug($"Consumer started: {_consumer.Name}");

            _consumer.OnRecord             += HandleOnMessage;
            _consumer.OnConsumeError       += HandleConsumeError;
            _consumer.OnError              += HandleOnError;
            _consumer.OnPartitionsAssigned += HandleOnPartitionsAssigned;
            _consumer.OnPartitionsRevoked  += HandleOnPartitionsRevoked;

            switch (_subscription)
            {
            case TopicSubscription ts:
                _consumer.Subscribe(ts.Topics);
                break;

            case Assignment a:
                _consumer.Assign(a.TopicPartitions);
                break;

            case AssignmentWithOffset awo:
                _consumer.Assign(awo.TopicPartitions);
                break;
            }

            _messagesReceived   = GetAsyncCallback <ConsumerRecord <K, V> >(MessagesReceived);
            _partitionsAssigned = GetAsyncCallback <IEnumerable <TopicPartition> >(PartitionsAssigned);
            _partitionsRevoked  = GetAsyncCallback <IEnumerable <TopicPartition> >(PartitionsRevoked);
            ScheduleRepeatedly(TimerKey, _settings.PollInterval);
        }
Example #2
0
        private void ApplySettings(ConsumerSettings <K, V> updatedSettings)
        {
            _settings         = updatedSettings;
            _pollTimeout      = _settings.PollTimeout;
            _positionTimeout  = _settings.PositionTimeout;
            _commitRefreshing = CommitRefreshing.Create <K, V>(_settings.CommitRefreshInterval);
            try
            {
                if (_log.IsDebugEnabled)
                {
                    _log.Debug($"Creating Kafka consumer with settings: {JsonConvert.SerializeObject(_settings)}");
                }

                var localSelf = Self;
                _consumer = _settings.CreateKafkaConsumer(
                    consumeErrorHandler: (c, e) => localSelf.Tell(new Status.Failure(new KafkaException(e))),
                    partitionAssignedHandler: (c, tp) => localSelf.Tell(new PartitionAssigned(tp.ToImmutableHashSet())),
                    partitionRevokedHandler: (c, tp) => localSelf.Tell(new PartitionRevoked(tp.ToImmutableHashSet())),
                    statisticHandler: (c, json) => _statisticsHandler.OnStatistics(c, json));

                if (_settings.ConnectionCheckerSettings.Enabled)
                {
                    _connectionCheckerActor = Context.ActorOf(ConnectionChecker.Props(_settings.ConnectionCheckerSettings));
                }
            }
            catch (Exception e)
            {
                ProcessError(e);
                throw;
            }
        }