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()); } }
public static async Task Main(string[] args) { var config = new ProducerConfig { BootstrapServers = "localhost:9092" }; // A Producer for sending messages with null keys and UTF-8 encoded values. using (var p = new Producer <string, string>(config)) { string message = string.Empty; do { Console.WriteLine("Digite uma mensagem para o kafka"); message = Console.ReadLine(); var messageKafka = new Message <string, string> { Value = message }; messageKafka.Key = "cliente-assinatura-123456"; messageKafka.Headers = new Headers(); messageKafka.Headers.Add("clientId", "181781723".ToByte()); try { var dr = await p.ProduceAsync("topic_test", messageKafka); Console.WriteLine($"Delivered '{dr.Value}' to '{dr.TopicPartitionOffset}'"); } catch (KafkaException e) { Console.WriteLine($"Delivery failed: {e.Error.Reason}"); } } while (message != "exit"); } }