This is an automated email from the ASF dual-hosted git repository. aaronai pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git
commit 690ff181e4d92ef555b77cb718ec107cb3e9fa54 Author: Aaron Ai <[email protected]> AuthorDate: Tue Feb 28 13:52:01 2023 +0800 Polish example --- csharp/examples/ProducerBenchmark.cs | 9 +- csharp/examples/ProducerDelayMessageExample.cs | 9 +- csharp/examples/ProducerFifoMessageExample.cs | 11 +-- csharp/examples/ProducerNormalMessageExample.cs | 10 +- .../examples/ProducerTransactionMessageExample.cs | 9 +- csharp/rocketmq-client-csharp/Message.cs | 38 ++++++-- csharp/rocketmq-client-csharp/PublishingMessage.cs | 2 +- csharp/tests/MessageTest.cs | 107 ++++++++++++++++++++- 8 files changed, 144 insertions(+), 51 deletions(-) diff --git a/csharp/examples/ProducerBenchmark.cs b/csharp/examples/ProducerBenchmark.cs index c20d6f09..59a3a0a6 100644 --- a/csharp/examples/ProducerBenchmark.cs +++ b/csharp/examples/ProducerBenchmark.cs @@ -79,17 +79,12 @@ namespace examples // Define your message body. var bytes = Encoding.UTF8.GetBytes("foobar"); const string tag = "yourMessageTagA"; - // You could set multiple keys for the single message. - var keys = new List<string> - { - "yourMessageKey-7044358f98fc", - "yourMessageKey-f72539fbc246" - }; var message = new Message.Builder() .SetTopic(topic) .SetBody(bytes) .SetTag(tag) - .SetKeys(keys) + // You could set multiple keys for the single message actually. + .SetKeys("yourMessageKey-7044358f98fc") .Build(); DoStats(); diff --git a/csharp/examples/ProducerDelayMessageExample.cs b/csharp/examples/ProducerDelayMessageExample.cs index 6c879bc2..72e2cadd 100644 --- a/csharp/examples/ProducerDelayMessageExample.cs +++ b/csharp/examples/ProducerDelayMessageExample.cs @@ -53,17 +53,12 @@ namespace examples // Define your message body. var bytes = Encoding.UTF8.GetBytes("foobar"); const string tag = "yourMessageTagA"; - // You could set multiple keys for the single message. - var keys = new List<string> - { - "yourMessageKey-2f00df144e48", - "yourMessageKey-49df1dd332b7" - }; var message = new Message.Builder() .SetTopic(topic) .SetBody(bytes) .SetTag(tag) - .SetKeys(keys) + // You could set multiple keys for the single message actually. + .SetKeys("yourMessageKey-2f00df144e48") .SetDeliveryTimestamp(DateTime.UtcNow + TimeSpan.FromSeconds(30)) .Build(); diff --git a/csharp/examples/ProducerFifoMessageExample.cs b/csharp/examples/ProducerFifoMessageExample.cs index c0a8337d..1eeaa2e2 100644 --- a/csharp/examples/ProducerFifoMessageExample.cs +++ b/csharp/examples/ProducerFifoMessageExample.cs @@ -54,24 +54,21 @@ namespace examples // Define your message body. var bytes = Encoding.UTF8.GetBytes("foobar"); const string tag = "yourMessageTagA"; - // You could set multiple keys for the single message. - var keys = new List<string> - { - "yourMessageKey-7044358f98fc", - "yourMessageKey-f72539fbc246" - }; const string messageGroup = "yourMessageGroup"; var message = new Message.Builder() .SetTopic(topic) .SetBody(bytes) .SetTag(tag) - .SetKeys(keys) + // You could set multiple keys for the single message actually. + .SetKeys("yourMessageKey-7044358f98fc") + // Message group decides the message delivery order. .SetMessageGroup(messageGroup) .Build(); var sendReceipt = await producer.Send(message); Logger.Info($"Send message successfully, sendReceipt={sendReceipt}"); Thread.Sleep(9999999); + // Or you could close the producer manually. // await producer.DisposeAsync(); } } diff --git a/csharp/examples/ProducerNormalMessageExample.cs b/csharp/examples/ProducerNormalMessageExample.cs index db2ccd18..27b805ff 100644 --- a/csharp/examples/ProducerNormalMessageExample.cs +++ b/csharp/examples/ProducerNormalMessageExample.cs @@ -53,18 +53,12 @@ namespace examples // Define your message body. var bytes = Encoding.UTF8.GetBytes("foobar"); const string tag = "yourMessageTagA"; - // You could set multiple keys for the single message. - var keys = new List<string> - { - "yourMessageKey-7044358f98fc", - "yourMessageKey-f72539fbc246" - }; - // Set topic for current message. var message = new Message.Builder() .SetTopic(topic) .SetBody(bytes) .SetTag(tag) - .SetKeys(keys) + // You could set multiple keys for the single message actually. + .SetKeys("yourMessageKey-7044358f98fc") .Build(); var sendReceipt = await producer.Send(message); diff --git a/csharp/examples/ProducerTransactionMessageExample.cs b/csharp/examples/ProducerTransactionMessageExample.cs index 54cebcdd..b19f95d7 100644 --- a/csharp/examples/ProducerTransactionMessageExample.cs +++ b/csharp/examples/ProducerTransactionMessageExample.cs @@ -62,17 +62,12 @@ namespace examples // Define your message body. var bytes = Encoding.UTF8.GetBytes("foobar"); const string tag = "yourMessageTagA"; - // You could set multiple keys for the single message. - var keys = new List<string> - { - "yourMessageKey-7044358f98fc", - "yourMessageKey-f72539fbc246" - }; var message = new Message.Builder() .SetTopic(topic) .SetBody(bytes) .SetTag(tag) - .SetKeys(keys) + // You could set multiple keys for the single message actually. + .SetKeys("yourMessageKey-7044358f98fc") .Build(); var sendReceipt = await producer.Send(message, transaction); diff --git a/csharp/rocketmq-client-csharp/Message.cs b/csharp/rocketmq-client-csharp/Message.cs index fe2dee79..14d932a2 100644 --- a/csharp/rocketmq-client-csharp/Message.cs +++ b/csharp/rocketmq-client-csharp/Message.cs @@ -23,13 +23,13 @@ namespace Org.Apache.Rocketmq public class Message { private Message(string topic, byte[] body, string tag, List<string> keys, - Dictionary<string, string> userProperties, DateTime? deliveryTimestamp, string messageGroup) + Dictionary<string, string> properties, DateTime? deliveryTimestamp, string messageGroup) { Topic = topic; Tag = tag; Keys = keys; Body = body; - UserProperties = userProperties; + Properties = properties; DeliveryTimestamp = deliveryTimestamp; MessageGroup = messageGroup; } @@ -40,7 +40,7 @@ namespace Org.Apache.Rocketmq Tag = message.Tag; Keys = message.Keys; Body = message.Body; - UserProperties = message.UserProperties; + Properties = message.Properties; MessageGroup = message.MessageGroup; DeliveryTimestamp = message.DeliveryTimestamp; } @@ -52,7 +52,7 @@ namespace Org.Apache.Rocketmq public string Tag { get; } public List<string> Keys { get; } - public Dictionary<string, string> UserProperties { get; } + public Dictionary<string, string> Properties { get; } public DateTime? DeliveryTimestamp { get; } @@ -64,37 +64,51 @@ namespace Org.Apache.Rocketmq private byte[] _body; private string _tag; private List<string> _keys = new(); - private Dictionary<string, string> _userProperties = new(); + private readonly Dictionary<string, string> _properties = new(); private DateTime? _deliveryTimestamp; private string _messageGroup; public Builder SetTopic(string topic) { + Preconditions.CheckArgument(null != topic, "topic should not be null"); _topic = topic; return this; } public Builder SetBody(byte[] body) { + Preconditions.CheckArgument(null != body, "body should not be null"); _body = body; return this; } public Builder SetTag(string tag) { + Preconditions.CheckArgument(!string.IsNullOrWhiteSpace(tag), "tag should not be null or white space"); _tag = tag; return this; } - public Builder SetKeys(List<string> keys) + public Builder SetKeys(params string[] keys) { - _keys = keys; + _keys = new List<string>(); + foreach (var key in keys) + { + Preconditions.CheckArgument(!string.IsNullOrWhiteSpace(key), + "key should not be null or white space"); + _keys.Add(key); + } + return this; } - public Builder SetUserProperties(Dictionary<string, string> userProperties) + public Builder AddProperty(string key, string value) { - _userProperties = userProperties; + Preconditions.CheckArgument(!string.IsNullOrWhiteSpace(key), + "key should not be null or white space"); + Preconditions.CheckArgument(!string.IsNullOrWhiteSpace(value), + "value should not be null or white space"); + _properties[key!] = value; return this; } @@ -106,13 +120,17 @@ namespace Org.Apache.Rocketmq public Builder SetMessageGroup(string messageGroup) { + Preconditions.CheckArgument(!string.IsNullOrWhiteSpace(messageGroup), + "messageGroup should not be null or white space"); _messageGroup = messageGroup; return this; } public Message Build() { - return new Message(_topic, _body, _tag, _keys, _userProperties, _deliveryTimestamp, _messageGroup); + Preconditions.CheckArgument(null != _topic, "topic has not been set yet"); + Preconditions.CheckArgument(null != _body, "body has not been set yet"); + return new Message(_topic, _body, _tag, _keys, _properties, _deliveryTimestamp, _messageGroup); } } } diff --git a/csharp/rocketmq-client-csharp/PublishingMessage.cs b/csharp/rocketmq-client-csharp/PublishingMessage.cs index efdc568f..369b7270 100644 --- a/csharp/rocketmq-client-csharp/PublishingMessage.cs +++ b/csharp/rocketmq-client-csharp/PublishingMessage.cs @@ -109,7 +109,7 @@ namespace Org.Apache.Rocketmq Topic = topicResource, Body = ByteString.CopyFrom(Body), SystemProperties = systemProperties, - UserProperties = { UserProperties } + UserProperties = { Properties } }; } } diff --git a/csharp/tests/MessageTest.cs b/csharp/tests/MessageTest.cs index 6ba00b8e..59db04a6 100644 --- a/csharp/tests/MessageTest.cs +++ b/csharp/tests/MessageTest.cs @@ -16,6 +16,7 @@ */ using System; +using System.Collections.Generic; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Text; @@ -28,10 +29,108 @@ namespace Org.Apache.Rocketmq [ExpectedException(typeof(ArgumentException))] public void TestIllegalTopic0() { - // const string topic = "\t\n"; - // const string bodyString = "body"; - // var body = Encoding.ASCII.GetBytes(bodyString); - // var _ = new Message(topic, body); + const string topic = null; + new Message.Builder().SetTopic(topic); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void TestIllegalTag0() + { + new Message.Builder().SetTag(null); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void TestIllegalTag1() + { + new Message.Builder().SetTag(""); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void TestIllegalTag2() + { + new Message.Builder().SetTag("\t"); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void TestIllegalTag3() + { + new Message.Builder().SetTag("\t\n"); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void TestIllegalMessageGroup0() + { + new Message.Builder().SetMessageGroup(null); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void TestIllegalMessageGroup1() + { + new Message.Builder().SetMessageGroup(""); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void TestIllegalMessageGroup2() + { + new Message.Builder().SetMessageGroup("\t"); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void TestIllegalMessageGroup3() + { + new Message.Builder().SetMessageGroup("\t\n"); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void TestIllegalProperty0() + { + new Message.Builder().AddProperty(null, "b"); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void TestIllegalProperty1() + { + new Message.Builder().AddProperty("a", null); + } + + [TestMethod] + public void TestAddProperty() + { + var message = new Message.Builder() + .SetTopic("topic") + .AddProperty("a", "b") + .SetBody(Encoding.UTF8.GetBytes("foobar")) + .Build(); + var properties = new Dictionary<string, string> + { + ["a"] = "b" + }; + Assert.AreEqual(1, message.Properties.Count); + Assert.AreEqual(properties["a"], message.Properties["a"]); + + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void TestIllegalKey() + { + new Message.Builder().SetKeys("\t"); + } + + [TestMethod] + public void TestKeys() + { + new Message.Builder().SetKeys("a", "b"); } } } \ No newline at end of file
