Beispiel #1
0
        public void StartConsume(List <string> messageList)
        {
            using var c = new ConsumerBuilder <Ignore, string>(conf).Build();
            c.Subscribe("my-replicated-topic");

            CancellationTokenSource cts = new CancellationTokenSource();

            Console.CancelKeyPress += (_, e) => {
                e.Cancel = true; // prevent the process from terminating.
                cts.Cancel();
            };

            try
            {
                while (true)
                {
                    try
                    {
                        var cr = c.Consume(cts.Token);
                        messageList.Add(cr.Message.Value);
                        _messageHub.AddMessage(cr.Message.Value);
                        Console.WriteLine($"Consumed message '{cr.Offset}' at: '{cr.TopicPartitionOffset}'.");
                    }
                    catch (ConsumeException e)
                    {
                        Console.WriteLine($"Error occured: {e.Error.Reason}");
                    }
                }
            }
            catch (OperationCanceledException)
            {
                c.Close();
            }
        }