Exemple #1
0
        public Task StartAsync(CancellationToken token)
        {
            _log.Information($"Starting stream for topic {_topic}");

            Status = NetStreamStatus.Running;

            if (Configuration.TopicCreationEnabled)
            {
                _topicCreator.CreateAll(Configuration.TopicConfigurations).Wait(token);
            }

            _consumer.Subscribe(_topic);

            return(Task.Factory.StartNew(async() =>
            {
                while (!token.IsCancellationRequested && Status != NetStreamStatus.Stopped)
                {
                    ConsumeResult <TKey, TMessage> consumeResult = null;
                    try
                    {
                        consumeResult = _consumer.Consume(100);
                        await ProcessMessageAsync(consumeResult, token);
                    }
                    catch (ConsumeException ce) when(ce.InnerException is MalformedMessageException && _configuration.ShouldSkipMalformedMessages)
                    {
                        _log.Error(ce, $"A malformed message was encountered on topic { _topic}. Skipping message. Skipping offset {ce.ConsumerRecord.Offset} on partition {ce.ConsumerRecord.Partition.Value}");
                        _consumer.Commit();
                    }
                    catch (Exception ex)
                    {
                        _log.Error(ex, $"An error occurred processing messages from topic {_topic}");
                        _onError(ex);
                        if (!_configuration.ContinueOnError)
                        {
                            ResetOffset(consumeResult);
                        }
                    }
                }
            }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap());
        }
Exemple #2
0
        public Task StartAsync(CancellationToken token)
        {
            _log.Information($"Starting stream for topic {_topic}");

            if (Configuration.TopicCreationEnabled)
            {
                _topicCreator.CreateAll(Configuration.TopicConfigurations).Wait(token);
            }

            _consumer.Subscribe(_topic);

            return(Task.Factory.StartNew(async() =>
            {
                while (!token.IsCancellationRequested)
                {
                    try
                    {
                        var consumeResult = _consumer.Consume(100);
                        if (consumeResult != null)
                        {
                            var consumeContext = new ConsumeContext <TKey, TMessage>(consumeResult, _consumer, Configuration.ConsumerGroup);

                            _log.Debug($"Begin consuming offset {consumeContext.Offset} on partition {consumeContext.Partition} ");
                            await _pipeline.ExecuteAsync(consumeContext, token).ConfigureAwait(false);
                            _log.Debug($"Finished consuming offset {consumeContext.Offset} on partition {consumeContext.Partition} ");
                        }
                    }
                    catch (ConsumeException ce) when(ce.InnerException is MalformedMessageException && _configuration.ShouldSkipMalformedMessages)
                    {
                        _log.Error(ce, $"A malformed message was encountered on topic { _topic}. Skipping message. Skipping offset {ce.ConsumerRecord.Offset} on partition {ce.ConsumerRecord.Partition.Value}");
                        _consumer.Commit();
                    }
                    catch (Exception ex)
                    {
                        _log.Error(ex, $"An error occurred processing messages from topic {_topic}");
                        _onError(ex);
                    }
                }
            }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap());
        }