static void Main(string[] args)
        {
            var config = new Dictionary <string, object>
            {
                { "group.id", "sample-consumer" },
                { "bootstrap.servers", "127.0.0.1:9092" },
                { "enable.auto.commit", "false" }
            };
            var topic = "my-topic";

            Console.WriteLine($"reads messages in {topic}");
            using (var consumer = new Consumer <Null, string>(config, null, new StringDeserializer(Encoding.UTF8)))
            {
                consumer.Subscribe(new string[] { topic });

                consumer.OnError += (_, e) =>
                {
                    Console.WriteLine(e.Reason);
                };

                consumer.OnMessage += (_, msg) =>
                {
                    Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {msg.Value}");
                    consumer.CommitAsync(msg);
                };

                while (true)
                {
                    consumer.Poll(100);
                }
            }
        }
        public Task ConsumeNewMessageAsync()
        {
            _tcs = new TaskCompletionSource <int>();
            Task <int> t = _tcs.Task;

            _consumer.Poll(TimeSpan.FromMilliseconds(100));

            return(t);
        }
Пример #3
0
        static void Main(string[] args)
        {
            var typeName = typeof(Program).Assembly.GetName();

            Console.WriteLine($"Starting {typeName.Name} v{typeName.Version}...");

            // Create the consumer configuration
            var config = new Dictionary <string, object>
            {
                { "group.id", Common.GetConfigValue("group.id") },
                { "bootstrap.servers", Common.GetConfigValue("bootstrap.servers") },
                { "auto.commit.interval.ms", Common.GetConfigValue("auto.commit.interval.ms") },
                { "auto.offset.reset", Common.GetConfigValue("auto.offset.reset") }
            };

            Console.WriteLine($"Connecting consumer to '{Common.GetConfigValue("bootstrap.servers")}' kafka endpoint...");
            // Create the consumer
            using (var consumer = new Consumer <Null, string>(config, null, new StringDeserializer(Encoding.UTF8)))
            {
                // Subscribe to the OnMessage event
                consumer.OnMessage += (obj, msg) =>
                {
                    Console.WriteLine($"Received: {msg.Value}. Offset: {msg.Offset}. Partition: {msg.Partition}");
                };

                // Subscribe to the Kafka topic
                consumer.Subscribe(new List <string>()
                {
                    Common.GetConfigValue("topic")
                });

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

                Console.WriteLine("Ctrl-C to exit.");

                // Poll for messages
                while (!cancelled)
                {
                    consumer.Poll(TimeSpan.FromMilliseconds(100));
                }
            }
        }
Пример #4
0
        /// <summary>
        //      In this example:
        ///         - offsets are auto commited.
        ///         - consumer.Poll / OnMessage is used to consume messages.
        ///         - no extra thread is created for the Poll loop.
        /// </summary>
        public static void Run_Poll(string brokerList, List <string> topics)
        {
            var config = new Dictionary <string, object>
            {
                { "bootstrap.servers", brokerList },
                { "group.id", "csharp-consumer" },
                { "enable.auto.commit", true },  // this is the default
                { "auto.commit.interval.ms", 5000 },
                { "statistics.interval.ms", 60000 },
                { "session.timeout.ms", 6000 },
                { "auto.offset.reset", "smallest" }
            };

            using (var consumer = new Consumer <Ignore, string>(config, null, new StringDeserializer(Encoding.UTF8)))
            {
                // Note: All event handlers are called on the main thread.

                consumer.OnMessage += (_, msg)
                                      => Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {msg.Value}");

                consumer.OnPartitionEOF += (_, end)
                                           => Console.WriteLine($"Reached end of topic {end.Topic} partition {end.Partition}, next message will be at offset {end.Offset}");

                consumer.OnError += (_, error)
                                    => Console.WriteLine($"Error: {error}");

                // Raised on deserialization errors or when a consumed message has an error != NoError.
                consumer.OnConsumeError += (_, msg)
                                           => Console.WriteLine($"Error consuming from topic/partition/offset {msg.Topic}/{msg.Partition}/{msg.Offset}: {msg.Error}");

                consumer.OnOffsetsCommitted += (_, commit)
                                               => Console.WriteLine(
                    commit.Error
                                ? $"Failed to commit offsets: {commit.Error}"
                                : $"Successfully committed offsets: [{string.Join(", ", commit.Offsets)}]");

                // Raised when the consumer is assigned a new set of partitions.
                consumer.OnPartitionsAssigned += (_, partitions) =>
                {
                    Console.WriteLine($"Assigned partitions: [{string.Join(", ", partitions)}], member id: {consumer.MemberId}");
                    // If you don't add a handler to the OnPartitionsAssigned event,
                    // the below .Assign call happens automatically. If you do, you
                    // must call .Assign explicitly in order for the consumer to
                    // start consuming messages.
                    consumer.Assign(partitions);
                };

                // Raised when the consumer's current assignment set has been revoked.
                consumer.OnPartitionsRevoked += (_, partitions) =>
                {
                    Console.WriteLine($"Revoked partitions: [{string.Join(", ", partitions)}]");
                    // If you don't add a handler to the OnPartitionsRevoked event,
                    // the below .Unassign call happens automatically. If you do,
                    // you must call .Unassign explicitly in order for the consumer
                    // to stop consuming messages from it's previously assigned
                    // partitions.
                    consumer.Unassign();
                };

                consumer.OnStatistics += (_, json)
                                         => Console.WriteLine($"Statistics: {json}");

                consumer.Subscribe(topics);

                Console.WriteLine($"Subscribed to: [{string.Join(", ", consumer.Subscription)}]");

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

                Console.WriteLine("Ctrl-C to exit.");
                while (!cancelled)
                {
                    consumer.Poll(TimeSpan.FromMilliseconds(100));
                }
            }
        }