From 4570c434537cee65fcf5ed5def7423ba90a2f22b Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Fri, 14 Jun 2024 11:08:48 -0500 Subject: [PATCH] Changing data model | allowing multiple messages to be sent via producer --- consumer/Consumer.cs | 2 +- producer/Producer.cs | 62 +++++++++++++++++++++++++++------------ shared/Model/DataModel.cs | 6 +--- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/consumer/Consumer.cs b/consumer/Consumer.cs index fb4b3ff..d714881 100644 --- a/consumer/Consumer.cs +++ b/consumer/Consumer.cs @@ -38,7 +38,7 @@ class Consumer var cr = consumer.Consume(cts.Token); dataRepository.Save(cr.Message.Value); - Console.WriteLine($"Consumed message with id '{cr.Message.Value.id}' at: '{cr.TopicPartitionOffset}'."); + Console.WriteLine($"Consumed message with id '{cr.Message.Value.id}'. Saving value to database"); } catch (ConsumeException e) { diff --git a/producer/Producer.cs b/producer/Producer.cs index ba4c9e8..a2df803 100644 --- a/producer/Producer.cs +++ b/producer/Producer.cs @@ -7,35 +7,59 @@ using Model; class Producer { + public static string TOPIC_NAME = "test-topic"; public static async Task Main (string[] args) { var config = new ProducerConfig { BootstrapServers = "localhost:29092" }; - using (var producerBuilder = new ProducerBuilder(config) + string introText = "\nType a string of text then press Enter. Type '+' anywhere in the text to quit:\n"; + var input = ""; + + using (var producer = new ProducerBuilder(config) .SetValueSerializer(new JsonSerializer()) .Build()) { - try - { - var msg = new Message + + Console.WriteLine(introText); + do { - Key = System.Guid.NewGuid().ToString(), - Value = new DataModel(System.Guid.NewGuid().ToString()) + input = Console.ReadLine(); + try + { + if (input != String.Empty) { - name = "Name", - description = "Description", - notes = "This is a test object" + SendMessage(producer, input); } - }; - - var dr = await producerBuilder.ProduceAsync("test-topic", msg); - Console.WriteLine($"Delivered '{dr.Value}' to '{dr.TopicPartitionOffset}'"); - } - catch (ProduceException e) - { - Console.WriteLine($"Delivery failed: {e.Error.Reason}"); - } + + } + catch (OverflowException e) + { + Console.WriteLine("An error has occurred", e.Message, input); + Console.WriteLine(introText); + } + } while (input != "+"); + } + } + + private static async void SendMessage(IProducer producer, string str) + { + try + { + var msg = new Message + { + Key = System.Guid.NewGuid().ToString(), + Value = new DataModel(System.Guid.NewGuid().ToString()) + { + message = str + } + }; + + var dr = await producer.ProduceAsync(TOPIC_NAME, msg); + Console.WriteLine($"Delivered message with id '{dr.Value.id}'"); + } + catch (ProduceException e) + { + Console.WriteLine($"Delivery failed: {e.Error.Reason}"); } - } } \ No newline at end of file diff --git a/shared/Model/DataModel.cs b/shared/Model/DataModel.cs index 7e473f3..0f93887 100644 --- a/shared/Model/DataModel.cs +++ b/shared/Model/DataModel.cs @@ -9,10 +9,6 @@ namespace Model public string id { get; set; } - public string? name { get; set; } - - public string? description { get; set; } - - public string? notes { get; set; } + public string? message { get; set; } } } \ No newline at end of file