Пример #1
0
        void DoConsume()
        {
            tokenSource = new CancellationTokenSource();
            task        = Task.Factory.StartNew(async token =>
            {
                var cancellationToken = (CancellationToken)token;

                using var consumer = new ConsumerBuilder <Guid, string>(settings.ConsumerConfig).Build();
                consumer.Subscribe(subscriptionsManager.GetEventNames());

                while (!cancellationToken.IsCancellationRequested)
                {
                    ConsumeResult <Guid, string> result = default;
                    try
                    {
                        result = consumer.Consume(cancellationToken);
                    }
                    catch (ConsumeException e)
                    {
                        logger.LogError($"Error occured: {e.Error.Reason}");
                        continue;
                    }

                    logger.LogInformation($"Consumed message '{result.Message}' at '{result.TopicPartitionOffset}'.");

                    var eventName    = result.Topic;
                    var eventMessage = result.Message.Value;
                    await ProcessEventAsync(eventName, eventMessage);
                }
            }, tokenSource.Token);
        }