public static async Task PublishEventSystemPerformance(LocalSystemPerformance input, SchemaRegistryConfig schemaRegistryConfig, ClientConfig kafkaConfig, string[] topics) { using (var schemaRegistry = new CachedSchemaRegistryClient(schemaRegistryConfig)) { //using (var producer = new Producer<string, string>(config, new StringSerializer(Encoding.UTF8), new StringSerializer(Encoding.UTF8))) // Produce events to the topic var producer = new ProducerBuilder <string, LocalSystemPerformance>(kafkaConfig) .SetValueSerializer(new JsonSerializer <LocalSystemPerformance>(schemaRegistry, new JsonSerializerConfig { BufferBytes = 100 })) .Build(); var localMessage = new Message <string, LocalSystemPerformance>(); localMessage.Key = "LocalSystemPerformance"; localMessage.Value = input; // Synchronous producer, does not work with Json serialisation //producer.Produce(topics[0], localMessage, SyncHandler); // Create asynchronous task (and wait for it) var delivery = producer.ProduceAsync(topics[1], localMessage); await delivery.ContinueWith(AsyncHandlerLocalSystemPerformance); producer.Flush(TimeSpan.FromSeconds(10)); } return; }
static async Task Main() { // Setting up the configuration for the Kafka client and Schema registry, saved in a local file GlobalParameters.clientConfig = await ConfluentHelper.LoadKafkaConfiguration(@"C:\Github\confluent-configuration.txt", null); GlobalParameters.schemaRegistryConfig = await ConfluentHelper.LoadSchemaRegistryConfiguration(@"C:\Github\schemaregistry-configuration.txt"); // Clear topic, if existing (reset environment) await ConfluentHelper.DeleteTopic(GlobalParameters.topics[0], GlobalParameters.clientConfig); // Create topic, if not existing yet await ConfluentHelper.CreateTopicIfNotExists(GlobalParameters.topics[0], 1, 3, GlobalParameters.clientConfig); await ConfluentHelper.CreateTopicIfNotExists(GlobalParameters.topics[1], 1, 3, GlobalParameters.clientConfig); // Start a file watcher to monitor input directory for mapping Json files // Event handles on file detection trigger the publishing actions WatchForFiles(AppDomain.CurrentDomain.BaseDirectory + @"\examples-publication"); for (int i = 0; i < 100; i++) { var bla = new LocalSystemPerformance(); await PublishEventSystemPerformance(bla, GlobalParameters.schemaRegistryConfig, GlobalParameters.clientConfig, GlobalParameters.topics); } // Start waiting until Escape is pressed Console.WriteLine(); Console.WriteLine("Press ESC to quit."); do { while (!Console.KeyAvailable) { // Wait for anything to happen in the designated directory for the file watcher } } while (Console.ReadKey(true).Key != ConsoleKey.Escape); // END OF APPLICATION }