diff --git a/README.md b/README.md index e5ff067..ac5d9eb 100644 --- a/README.md +++ b/README.md @@ -46,10 +46,10 @@ using Memphis.Client; // Connecting to the broker var options = MemphisClientFactory.GetDefaultOptions(); -options.Host = "aws-us-east-1.cloud.memphis.dev"; -options.AccountId = int.Parse(Environment.GetEnvironmentVariable("memphis_account_id")); -options.Username = "test_user"; -options.Password = Environment.GetEnvironmentVariable("memphis_pass"); +options.Host = ""; +options.AccountId = ; +options.Username = ""; +options.Password = ""; var memphisClient = await MemphisClientFactory.CreateClient(options); ``` @@ -67,8 +67,8 @@ var headers = new NameValueCollection(); var msgBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message)); await memphisClient.ProduceAsync(new Memphis.Client.Producer.MemphisProducerOptions { - StationName = "test_station", - ProducerName = "producer" + StationName = "", + ProducerName = "" }, msgBytes, headers); @@ -77,6 +77,8 @@ public class Message { public string Hello { get; set; } } + +memphisClient.Dispose(); ``` Lastly, to consume this message, call the `memphisClient.FetchMessages` function or create a consumer and call its `consumer.Fetch` function: @@ -84,8 +86,8 @@ Lastly, to consume this message, call the `memphisClient.FetchMessages` function ```C# var messages = await memphisClient.FetchMessages(new Memphis.Client.Consumer.FetchMessageOptions { - StationName = "test_station", - ConsumerName = "consumer", + StationName = "", + ConsumerName = "", Prefetch = false }); @@ -98,6 +100,8 @@ foreach (MemphisMessage message in messages) message.Ack(); } + +memphisClient.Dispose(); ``` > Remember to call `memphisClient.Dispose()` to close the connection! diff --git a/examples/Consumer/Program.cs b/examples/Consumer/Program.cs index e7666d9..d52695e 100644 --- a/examples/Consumer/Program.cs +++ b/examples/Consumer/Program.cs @@ -2,45 +2,52 @@ using Memphis.Client.Core; using System.Text.Json; +MemphisClient? memphisClient = null; + try { var options = MemphisClientFactory.GetDefaultOptions(); - options.Host = "aws-us-east-1.cloud.memphis.dev"; - options.AccountId = int.Parse(Environment.GetEnvironmentVariable("memphis_account_id")); - options.Username = "test_user"; - options.Password = Environment.GetEnvironmentVariable("memphis_pass"); + options.Host = ""; + // options.AccountId = ""; + options.Username = ""; + options.Password = ""; - var memphisClient = await MemphisClientFactory.CreateClient(options); + memphisClient = await MemphisClientFactory.CreateClient(options); var consumer = await memphisClient.CreateConsumer( new Memphis.Client.Consumer.MemphisConsumerOptions { - StationName = "test_station", - ConsumerName = "consumer" + StationName = "", + ConsumerName = "" }); - var messages = consumer.Fetch(3, false); - - foreach (MemphisMessage message in messages) - { - var messageData = message.GetData(); - var messageOBJ = JsonSerializer.Deserialize(messageData); + while (true) { + var messages = consumer.Fetch(3, false); - // Do something with the message here - Console.WriteLine(JsonSerializer.Serialize(messageOBJ)); + if (!messages.Any()) + { + continue; + } - message.Ack(); - } + foreach (MemphisMessage message in messages) + { + var messageData = message.GetData(); + var messageOBJ = JsonSerializer.Deserialize(messageData); - memphisClient.Dispose(); + // Do something with the message here + Console.WriteLine(JsonSerializer.Serialize(messageOBJ)); + message.Ack(); + } + } } catch (Exception ex) { Console.Error.WriteLine(ex.Message); + memphisClient?.Dispose(); } public class Message { - public string Hello { get; set; } + public string? Hello { get; set; } } \ No newline at end of file diff --git a/examples/Producer/Program.cs b/examples/Producer/Program.cs index 8bc48b1..1326931 100644 --- a/examples/Producer/Program.cs +++ b/examples/Producer/Program.cs @@ -3,21 +3,24 @@ using System.Text; using System.Text.Json; +MemphisClient? memphisClient = null; + try { var options = MemphisClientFactory.GetDefaultOptions(); - options.Host = "aws-us-east-1.cloud.memphis.dev"; - options.AccountId = int.Parse(Environment.GetEnvironmentVariable("memphis_account_id")); - options.Username = "test_user"; - options.Password = Environment.GetEnvironmentVariable("memphis_pass"); + + options.Host = ""; + // options.AccountId = ; + options.Username = ""; + options.Password = ""; - var memphisClient = await MemphisClientFactory.CreateClient(options); + memphisClient = await MemphisClientFactory.CreateClient(options); var producer = await memphisClient.CreateProducer( new Memphis.Client.Producer.MemphisProducerOptions { - StationName = "test_station", - ProducerName = "producer" + StationName = "", + ProducerName = "" }); Message message = new() @@ -41,9 +44,10 @@ await producer.ProduceAsync( catch (Exception ex) { Console.Error.WriteLine(ex.Message); + memphisClient?.Dispose(); } public class Message { - public string Hello { get; set; } + public string? Hello { get; set; } } \ No newline at end of file diff --git a/src/Memphis.Client/Exception/MemphisExceptions.cs b/src/Memphis.Client/Exception/MemphisExceptions.cs index aad7336..f09daae 100644 --- a/src/Memphis.Client/Exception/MemphisExceptions.cs +++ b/src/Memphis.Client/Exception/MemphisExceptions.cs @@ -12,7 +12,7 @@ public static class MemphisExceptions{ public static readonly MemphisException InvalidSchemaNameException = new("Only alphanumeric and the '_', '-', '.' characters are allowed in schema name"); - public static readonly MemphisException InvalidSchemaStartEndCharsException = new("Schema name can not start or end with non alphanumeric character"); + public static readonly MemphisException InvalidSchemaStartEndCharsException = new("Schema name can not start or end with non-alphanumeric character"); public static readonly MemphisException EmptySchemaTypeException = new("Schema type can not be empty");