static async Task Main(string[] args)
        {
            var typeName = typeof(Program).Assembly.GetName();

            Log($"Start {typeName.Name} (v{typeName.Version})");

            var currentConfig = CurrentConfiguration.Build();

            // Create the consumer configuration
            var config = new ConsumerConfig
            {
                GroupId          = "starwars-consumer-group",
                BootstrapServers = currentConfig.BootstrapServers,
                AutoOffsetReset  = AutoOffsetReset.Earliest
            };


            Log($"Connect consumer to kafka > {currentConfig.BootstrapServers}");

            // Create the consumer
            using (var consumer = new ConsumerBuilder <Ignore, string>(config).Build())
            {
                // Subscribe to the Kafka topic
                consumer.Subscribe(new List <string>()
                {
                    currentConfig.Topic
                });

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

                Log("Press Ctrl+C to exit");

                // Poll for messages
                try
                {
                    while (true)
                    {
                        try
                        {
                            var cr = consumer.Consume(cts.Token);
                            Log($"  >> Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'.");
                        }
                        catch (ConsumeException e)
                        {
                            Log($"  >> Error occured: {e.Error.Reason}");
                        }
                    }
                }
                catch (OperationCanceledException)
                {
                    // Ensure the consumer leaves the group cleanly and final offsets are committed.
                    consumer.Close();
                }
            }
        }
Example #2
0
        static async Task Main(string[] args)
        {
            var typeName = typeof(Program).Assembly.GetName();

            Log($"Start {typeName.Name} (v{typeName.Version})");

            var currentConfig = CurrentConfiguration.Build();

            var config = new ProducerConfig
            {
                BootstrapServers = currentConfig.BootstrapServers
            };

            Log($"Connect to kafka > {currentConfig.BootstrapServers}");

            using (var producer = new ProducerBuilder <Null, string>(config).Build())
            {
                var topic = currentConfig.Topic;
                try
                {
                    for (int i = 0; i < 5; i++)
                    {
                        var message = $"May the Force be with you {i}";

                        var deliveryResult = await producer.ProduceAsync(topic, new Message <Null, string> {
                            Value = message
                        });

                        Log($"  >> Delivered '{deliveryResult.Value}' to '{deliveryResult.TopicPartitionOffset}'");
                    }

                    // wait for up to 10 seconds for any inflight messages to be delivered.
                    producer.Flush(TimeSpan.FromSeconds(10));
                }
                catch (ProduceException <Null, string> e)
                {
                    Log($"  >> Delivery failed: {e.Error.Reason}");
                }
            }
        }