Exemplo n.º 1
0
        public static async Task RegisterHandler <T1, T2>(
            Consumer <T1, T2> consumer,
            IEnumerable <string> topics,
            Func <string, T2, Task> handler)
        {
            using (consumer)
            {
                KafkaHelper.SubscribeToEvents <T1, T2>(consumer, topics);

                while (true)
                {
                    try
                    {
                        var cr = await consumer.ConsumeAsync();

                        if (!cr.Error.IsError)
                        {
                            await handler(cr.Topic, cr.Value);
                        }
                        else
                        {
                            Console.WriteLine("No message");
                        }
                    }
                    catch (ConsumeException e)
                    {
                        Console.WriteLine($"Error occured: {e.Error.Reason}");
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static async Task Consume()
        {
            var conf = new Dictionary <string, object>
            {
                { "group.id", "consumer-group-1" },
                { "bootstrap.servers", "localhost:9092" },
                { "auto.offset.reset", "earliest" }
            };

            using (var c = new Consumer <Ignore, string>(conf, null, new StringDeserializer(Encoding.UTF8)))
            {
                await KafkaHelper.RegisterHandler(
                    c,
                    GetLogEvents(),
                    async (topic, message) => await InsertEvent(KafkaHelper.GetEventTypeFromTopic(topic), message));
            }
        }