private static void Test() { // The Kafka endpoint address string kafkaEndpoint = "127.0.0.1:9092"; // The Kafka topic we'll be using string kafkaTopic = "testtopic"; // Create the producer configuration Dictionary <string, object> producerConfig = new Dictionary <string, object> { { "bootstrap.servers", kafkaEndpoint } }; // Create the producer using (Producer <Null, string> producer = new Producer <Null, string>(producerConfig, null, new StringSerializer(Encoding.UTF8))) { // Send 10 messages to the topic for (int i = 0; i < 10; i++) { string message = $"Event {i}"; Message <Null, string> result = producer.ProduceAsync(kafkaTopic, null, message).GetAwaiter().GetResult(); Console.WriteLine($"Event {i} sent on Partition: {result.Partition} with Offset: {result.Offset}"); } } // Create the consumer configuration Dictionary <string, object> consumerConfig = new Dictionary <string, object> { { "group.id", "myconsumer" }, { "bootstrap.servers", kafkaEndpoint }, { "auto.offset.reset", "earliest" } }; // Create the consumer using (Consumer <Null, string> consumer = new Consumer <Null, string>(consumerConfig, null, new StringDeserializer(Encoding.UTF8))) { // Subscribe to the OnMessage event consumer.OnMessage += (obj, msg) => { Console.WriteLine($"Received: {msg.Value}"); }; // Subscribe to the Kafka topic consumer.Subscribe(new List <string>() { kafkaTopic }); // Handle Cancel Keypress bool 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(); } } }
static void Main(string[] args) { if (!File.Exists("Offset.txt")) { var stream = File.Create("Offset.txt"); stream.Close(); StreamWriter arq = new StreamWriter("Offset.txt"); arq.WriteLine("0"); arq.Close(); } StreamReader file2 = new StreamReader("Offset.txt"); offint = int.Parse(file2.ReadLine()); file2.Close(); //////////////////////////////////////////////////////////////////////////////// string topic = "ComandoTopic4"; Uri uri = new Uri("http://localhost:9092"); var options = new KafkaOptions(uri); var router = new BrokerRouter(options); var client = new Producer(router); ///////////////////////////////////////////////////////////////////////////////// String data; int comand; int chave; Comando comando; Message msg2; Task threadReceive; threadReceive = new Task(ThreadReceive); threadReceive.Start(); while (true) { if (!Receive) { Console.WriteLine("\nQual comando(ADD-1/READ-2/UPDATE-3/DELETE-4/LISTAR-5/DESLIGAR-6/SNAPSHOT-8)"); if (!int.TryParse(Console.ReadLine(), out comand) || comand <= 0 || comand > 8) { Console.WriteLine("Comando Inválido!"); continue; } switch (comand) { case (int)Comandos.ADD: Console.WriteLine("Insira chave"); if (!int.TryParse(Console.ReadLine(), out chave)) { Console.WriteLine("Chave inválida"); continue; } Console.WriteLine("Insira dado"); data = Console.ReadLine(); comando = new Comando(Comandos.ADD, chave, data); msg2 = new Message(JsonConvert.SerializeObject(comando)); client.SendMessageAsync(topic, new List <Message> { msg2 }); Receive = true; break; case (int)Comandos.READ: Console.WriteLine("Insira chave"); if (!int.TryParse(Console.ReadLine(), out chave)) { Console.WriteLine("Chave inválida"); continue; } comando = new Comando(Comandos.READ, chave, "a"); msg2 = new Message(JsonConvert.SerializeObject(comando)); client.SendMessageAsync(topic, new List <Message> { msg2 }); Receive = true; break; case (int)Comandos.UPDATE: Console.WriteLine("Insira chave"); if (!int.TryParse(Console.ReadLine(), out chave)) { Console.WriteLine("Chave inválida"); continue; } Console.WriteLine("Insira dado"); data = Console.ReadLine(); comando = new Comando(Comandos.UPDATE, chave, data); msg2 = new Message(JsonConvert.SerializeObject(comando)); client.SendMessageAsync(topic, new List <Message> { msg2 }); Receive = true; break; case (int)Comandos.DELETE: Console.WriteLine("Insira chave"); if (!int.TryParse(Console.ReadLine(), out chave)) { Console.WriteLine("Chave inválida"); continue; } comando = new Comando(Comandos.DELETE, chave, "c"); msg2 = new Message(JsonConvert.SerializeObject(comando)); client.SendMessageAsync(topic, new List <Message> { msg2 }); Receive = true; break; case (int)Comandos.DESLIGAR: comando = new Comando(Comandos.DESLIGAR, 0, "c"); msg2 = new Message(JsonConvert.SerializeObject(comando)); client.SendMessageAsync(topic, new List <Message> { msg2 }).Wait(); Receive = true; break; case (int)Comandos.LISTAR: comando = new Comando(Comandos.LISTAR, 0, "c"); msg2 = new Message(JsonConvert.SerializeObject(comando)); client.SendMessageAsync(topic, new List <Message> { msg2 }); Receive = true; break; case (int)Comandos.SNAPSHOT: Console.WriteLine("Insira o tempo(segundos): "); if (!int.TryParse(Console.ReadLine(), out chave)) { Console.WriteLine("Tempo inválido"); continue; } comando = new Comando(Comandos.SNAPSHOT, chave, "c"); msg2 = new Message(JsonConvert.SerializeObject(comando)); client.SendMessageAsync(topic, new List <Message> { msg2 }); Receive = true; break; } } } }
public void Producer(string msg) { using (Producer client = new Producer(_router)) { client.SendMessageAsync(CsTopic, new[] { new Message(msg) }).Wait(); } }