static void Main(string[] args) { var config = new Dictionary <string, object> { { "bootstrap.servers", "0.0.0.0:9092" } }; var topic = "my-topic"; Console.WriteLine($"Producer app, sends messages to {topic}, type exit to stop the app"); using (var producer = new Producer <Null, string>(config, null, new StringSerializer(Encoding.UTF8))) { string text = null; producer.OnError += (_, e) => { Console.WriteLine(e.Reason); }; while (text != "exit") { text = Console.ReadLine(); producer.ProduceAsync(topic, null, text); } producer.Flush(100); } }
static void Main(string[] args) { var config = new Dictionary <string, object> { { "bootstrap.servers", "localhost:9092" } }; using (var producer = new Producer <Null, string>(config, null, new StringSerializer(Encoding.UTF8))) { string text = null; while (text != "exit") { System.Console.Write("Text: "); text = Console.ReadLine(); var result = producer.ProduceAsync("hello-topic", null, text).Result; System.Console.WriteLine($"Error: {result.Error}"); } producer.Flush(100); } }
static void Main(string[] args) { try { var topicName = "test"; var brokerList = "kafka:9092"; var config = new Dictionary <string, object> { { "bootstrap.servers", brokerList } }; using (var producer = new Producer <Null, string>(config, null, new StringSerializer(Encoding.UTF8))) { Console.WriteLine($"{producer.Name} producing on {topicName}. q to exit."); while (true) { Thread.Sleep(1000); var guid = Guid.NewGuid().ToString(); Console.WriteLine($"Sending {guid}"); var deliveryReport = producer.ProduceAsync(topicName, null, guid); deliveryReport.ContinueWith(task => { Console.WriteLine($"Partition: {task.Result.Partition}, Offset: {task.Result.Offset}"); }); } // Tasks are not waited on synchronously (ContinueWith is not synchronous), // so it's possible they may still in progress here. producer.Flush(); } } catch (System.Exception e) { Console.WriteLine(e.ToString()); } }