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


The following commit(s) were added to refs/heads/master by this push:
     new 984687c9 Divide example by the message & producer/consumer type
984687c9 is described below

commit 984687c943df081973c48bd6015d1b5d5bdd6798
Author: Aaron Ai <[email protected]>
AuthorDate: Fri Dec 2 19:33:30 2022 +0800

    Divide example by the message & producer/consumer type
---
 csharp/examples/ProducerDelayMessageExample.cs  |  67 ++++++++++
 csharp/examples/ProducerFifoMessageExample.cs   |  70 ++++++++++
 csharp/examples/ProducerNormalMessageExample.cs |  64 +++++++++
 csharp/examples/Program.cs                      | 164 ------------------------
 csharp/examples/QuickStart.cs                   |  32 +++++
 csharp/examples/SimpleConsumerExample.cs        |  67 ++++++++++
 csharp/examples/examples.csproj                 |  16 +--
 7 files changed, 307 insertions(+), 173 deletions(-)

diff --git a/csharp/examples/ProducerDelayMessageExample.cs 
b/csharp/examples/ProducerDelayMessageExample.cs
new file mode 100644
index 00000000..58c8bcd0
--- /dev/null
+++ b/csharp/examples/ProducerDelayMessageExample.cs
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Threading.Tasks;
+using Org.Apache.Rocketmq;
+
+namespace examples
+{
+    static class ProducerDelayMessageExample
+    {
+        internal static async Task QuickStart()
+        {
+            string accessKey = "yourAccessKey";
+            string secretKey = "yourSecretKey";
+            // Credential provider is optional for client configuration.
+            var credentialsProvider = new StaticCredentialsProvider(accessKey, 
secretKey);
+            string endpoints = "foobar.com:8080";
+
+            var producer = new Producer(endpoints)
+            {
+                CredentialsProvider = credentialsProvider
+            };
+            string topic = "yourDelayTopic";
+            // Set the topic name(s), which is optional. It makes producer 
could prefetch the topic route before 
+            // message publishing.
+            producer.AddTopicOfInterest(topic);
+
+            await producer.Start();
+            // Define your message body.
+            var bytes = Encoding.UTF8.GetBytes("foobar");
+            string tag = "yourMessageTagA";
+            // You could set multiple keys for the single message.
+            var keys = new List<string>
+            {
+                "yourMessageKey-2f00df144e48",
+                "yourMessageKey-49df1dd332b7"
+            };
+            // Set topic for current message.
+            var message = new Message(topic, bytes)
+            {
+                Tag = tag,
+                Keys = keys,
+                // Essential for DELAY message.
+                DeliveryTimestamp = DateTime.UtcNow + TimeSpan.FromSeconds(30)
+            };
+            await producer.Send(message);
+            await producer.Shutdown();
+        }
+    }
+}
\ No newline at end of file
diff --git a/csharp/examples/ProducerFifoMessageExample.cs 
b/csharp/examples/ProducerFifoMessageExample.cs
new file mode 100644
index 00000000..4938cb1c
--- /dev/null
+++ b/csharp/examples/ProducerFifoMessageExample.cs
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System.Collections.Generic;
+using System.Text;
+using System.Threading.Tasks;
+using NLog;
+using Org.Apache.Rocketmq;
+
+namespace examples
+{
+    static class ProducerFifoMessageExample
+    {
+        private static readonly Logger Logger = 
MqLogManager.Instance.GetCurrentClassLogger();
+
+        internal static async Task QuickStart()
+        {
+            string accessKey = "yourAccessKey";
+            string secretKey = "yourSecretKey";
+            // Credential provider is optional for client configuration.
+            var credentialsProvider = new StaticCredentialsProvider(accessKey, 
secretKey);
+            string endpoints = "foobar.com:8080";
+
+            var producer = new Producer(endpoints)
+            {
+                CredentialsProvider = credentialsProvider
+            };
+            string topic = "yourFifoTopic";
+            // Set the topic name(s), which is optional. It makes producer 
could prefetch the topic route before 
+            // message publishing.
+            producer.AddTopicOfInterest(topic);
+
+            await producer.Start();
+            // Define your message body.
+            byte[] bytes = Encoding.UTF8.GetBytes("foobar");
+            string tag = "yourMessageTagA";
+            // You could set multiple keys for the single message.
+            var keys = new List<string>
+            {
+                "yourMessageKey-6cc8b65ed1c8",
+                "yourMessageKey-43783375d9a5"
+            };
+            // Set topic for current message.
+            var message = new Message(topic, bytes)
+            {
+                Tag = tag,
+                Keys = keys,
+                // Essential for FIFO message, messages that belongs to the 
same message group follow the FIFO semantics.
+                MessageGroup = "yourMessageGroup0"
+            };
+            var sendReceipt = await producer.Send(message);
+            Logger.Info($"Send message successfully, 
sendReceipt={sendReceipt}.");
+            await producer.Shutdown();
+        }
+    }
+}
\ No newline at end of file
diff --git a/csharp/examples/ProducerNormalMessageExample.cs 
b/csharp/examples/ProducerNormalMessageExample.cs
new file mode 100644
index 00000000..7acb4571
--- /dev/null
+++ b/csharp/examples/ProducerNormalMessageExample.cs
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System.Collections.Generic;
+using System.Text;
+using System.Threading.Tasks;
+using Org.Apache.Rocketmq;
+
+namespace examples
+{
+    static class ProducerNormalMessageExample
+    {
+        internal static async Task QuickStart()
+        {
+            string accessKey = "yourAccessKey";
+            string secretKey = "yourSecretKey";
+            // Credential provider is optional for client configuration.
+            var credentialsProvider = new StaticCredentialsProvider(accessKey, 
secretKey);
+            string endpoints = "foobar.com:8080";
+
+            var producer = new Producer(endpoints)
+            {
+                CredentialsProvider = credentialsProvider
+            };
+            string topic = "yourNormalTopic";
+            // Set the topic name(s), which is optional. It makes producer 
could prefetch the topic route before 
+            // message publishing.
+            producer.AddTopicOfInterest(topic);
+
+            await producer.Start();
+            // Define your message body.
+            byte[] bytes = Encoding.UTF8.GetBytes("foobar");
+            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(topic, bytes)
+            {
+                Tag = tag,
+                Keys = keys
+            };
+            await producer.Send(message);
+            await producer.Shutdown();
+        }
+    }
+}
\ No newline at end of file
diff --git a/csharp/examples/Program.cs b/csharp/examples/Program.cs
deleted file mode 100644
index abc89ce3..00000000
--- a/csharp/examples/Program.cs
+++ /dev/null
@@ -1,164 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-using System;
-using System.Collections.Generic;
-using System.Threading.Tasks;
-using Org.Apache.Rocketmq;
-
-namespace examples
-{
-    class Program
-    {
-        private const string ACCESS_URL = 
"rmq-cn-tl32uly8x0n.cn-hangzhou.rmq.aliyuncs.com:8080";
-        private const string STANDARD_TOPIC = "sdk_standard";
-        private const string FIFO_TOPIC = "sdk_fifo";
-        private const string TIMED_TOPIC = "sdk_timed";
-        private const string TRANSACTIONAL_TOPIC = "sdk_transactional";
-
-        private const string CONCURRENT_GROUP = "sdk_concurrency";
-        
-        private static async Task<SendReceipt> SendStandardMessage(Producer 
producer)
-        {
-            byte[] body = new byte[1024];
-            Array.Fill(body, (byte)'x');
-            // Associate the message with one or multiple keys
-            var keys = new List<string>
-            {
-                "k1",
-                "k2"
-            };
-            
-            var msg = new Message(STANDARD_TOPIC, body)
-            {
-                // Tag the massage. A message has at most one tag.
-                Tag = "Tag-0",
-                Keys = keys
-            };
-            
-            msg.Keys = keys;
-
-            return await producer.Send(msg);
-        }
-
-        private static async Task<SendReceipt> SendFifoMessage(Producer 
producer)
-        {
-            byte[] body = new byte[1024];
-            Array.Fill(body, (byte)'x');
-            // Associate the message with one or multiple keys
-            var keys = new List<string>
-            {
-                "k1",
-                "k2"
-            };
-            
-            var msg = new Message(FIFO_TOPIC, body)
-            {
-                // Tag the massage. A message has at most one tag.
-                Tag = "Tag-0",
-                Keys = keys
-            };
-            
-            msg.Keys = keys;
-
-            // Messages of the same message-group will be published orderly.
-            msg.MessageGroup = "SampleMessageGroup";
-
-            return await producer.Send(msg);
-        }
-
-        private static async Task<SendReceipt> SendTimedMessage(Producer 
producer)
-        {
-            byte[] body = new byte[1024];
-            Array.Fill(body, (byte)'x');
-            // Associate the message with one or multiple keys
-            var keys = new List<string>
-            {
-                "k1",
-                "k2"
-            };
-            
-            var msg = new Message(TIMED_TOPIC, body)
-            {
-                // Tag the massage. A message has at most one tag.
-                Tag = "Tag-0",
-                Keys = keys
-            };
-            
-            msg.Keys = keys;
-            msg.DeliveryTimestamp = DateTime.UtcNow + TimeSpan.FromSeconds(30);
-            return await producer.Send(msg);
-        }
-
-        private static async Task ConsumeAndAckMessages(SimpleConsumer 
simpleConsumer)
-        {
-            var messages = await simpleConsumer.Receive(32, 
TimeSpan.FromSeconds(60));
-            if (null != messages)
-            {
-                var tasks = new List<Task>();
-                foreach (var message in messages)
-                {
-                    Console.WriteLine($"Receive a message, 
topic={message.Topic}, message-id={message.MessageId}");
-                    var task = simpleConsumer.Ack(message);
-                    tasks.Add(task);
-                }
-                await Task.WhenAll(tasks);
-                Console.WriteLine($"{tasks.Count} messages have been 
acknowledged");
-            }
-        }
-        
-        static async Task Main(string[] args)
-        {
-            var credentialsProvider = new ConfigFileCredentialsProvider();
-            var producer = new Producer(ACCESS_URL)
-            {
-                CredentialsProvider = credentialsProvider
-            };
-            producer.AddTopicOfInterest(STANDARD_TOPIC);
-            producer.AddTopicOfInterest(FIFO_TOPIC);
-            producer.AddTopicOfInterest(TIMED_TOPIC);
-            producer.AddTopicOfInterest(TRANSACTIONAL_TOPIC);
-            
-            await producer.Start();
-            
-            var sendReceiptOfStandardMessage = await 
SendStandardMessage(producer);
-            Console.WriteLine($"Standard message-id: 
{sendReceiptOfStandardMessage.MessageId}");
-            
-            var sendReceiptOfFifoMessage = await SendFifoMessage(producer);
-            Console.WriteLine($"FIFO message-id: 
{sendReceiptOfFifoMessage.MessageId}");
-            
-            var sendReceiptOfTimedMessage = await SendTimedMessage(producer);
-            Console.WriteLine($"Timed message-id: 
{sendReceiptOfTimedMessage.MessageId}");
-            
-            await producer.Shutdown();
-
-            Console.WriteLine("Now start a simple consumer");
-            var simpleConsumer = new SimpleConsumer(ACCESS_URL, 
CONCURRENT_GROUP)
-            {
-                CredentialsProvider = credentialsProvider
-            };
-            
-            simpleConsumer.Subscribe(STANDARD_TOPIC, new FilterExpression("*", 
ExpressionType.TAG));
-            await simpleConsumer.Start();
-
-            await ConsumeAndAckMessages(simpleConsumer);
-
-            await simpleConsumer.Shutdown();
-
-            Console.ReadKey();
-        }
-    }
-}
diff --git a/csharp/examples/QuickStart.cs b/csharp/examples/QuickStart.cs
new file mode 100644
index 00000000..ff28d3e3
--- /dev/null
+++ b/csharp/examples/QuickStart.cs
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System.Threading.Tasks;
+
+namespace examples
+{
+    static class QuickStart
+    {
+        public static async Task Main()
+        {
+            await ProducerNormalMessageExample.QuickStart();
+            await ProducerFifoMessageExample.QuickStart();
+            await ProducerDelayMessageExample.QuickStart();
+            await SimpleConsumerExample.QuickStart();
+        }
+    }
+}
\ No newline at end of file
diff --git a/csharp/examples/SimpleConsumerExample.cs 
b/csharp/examples/SimpleConsumerExample.cs
new file mode 100644
index 00000000..e5fdf418
--- /dev/null
+++ b/csharp/examples/SimpleConsumerExample.cs
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using NLog;
+using Org.Apache.Rocketmq;
+
+namespace examples
+{
+    static class SimpleConsumerExample
+    {
+        private static readonly Logger Logger = 
MqLogManager.Instance.GetCurrentClassLogger();
+
+        internal static async Task QuickStart()
+        {
+            string accessKey = "yourAccessKey";
+            string secretKey = "yourSecretKey";
+            // Credential provider is optional for client configuration.
+            var credentialsProvider = new StaticCredentialsProvider(accessKey, 
secretKey);
+            string endpoints = "foobar.com:8080";
+
+            string consumerGroup = "yourConsumerGroup";
+            SimpleConsumer simpleConsumer = new SimpleConsumer(endpoints, 
consumerGroup)
+            {
+                CredentialsProvider = credentialsProvider
+            };
+
+            string topic = "yourTopic";
+            string tag = "tagA";
+            // Set topic subscription for consumer.
+            simpleConsumer.Subscribe(topic, new FilterExpression(tag, 
ExpressionType.TAG));
+            await simpleConsumer.Start();
+
+            int maxMessageNum = 16;
+            TimeSpan invisibleDuration = TimeSpan.FromSeconds(15);
+            var messages = await simpleConsumer.Receive(maxMessageNum, 
invisibleDuration);
+            Logger.Info($"{messages.Count} messages has been received.");
+
+            var tasks = new List<Task>();
+            foreach (var message in messages)
+            {
+                Logger.Info($"Received a message, topic={message.Topic}, 
message-id={message.MessageId}.");
+                var task = simpleConsumer.Ack(message);
+                tasks.Add(task);
+            }
+
+            await Task.WhenAll(tasks);
+            Logger.Info($"{tasks.Count} messages have been acknowledged.");
+        }
+    }
+}
\ No newline at end of file
diff --git a/csharp/examples/examples.csproj b/csharp/examples/examples.csproj
index 03cc4092..ebdf0af4 100644
--- a/csharp/examples/examples.csproj
+++ b/csharp/examples/examples.csproj
@@ -1,12 +1,10 @@
 <Project Sdk="Microsoft.NET.Sdk">
+    <ItemGroup>
+        <ProjectReference 
Include="..\rocketmq-client-csharp\rocketmq-client-csharp.csproj" />
+    </ItemGroup>
 
-  <ItemGroup>
-    <ProjectReference 
Include="..\rocketmq-client-csharp\rocketmq-client-csharp.csproj" />
-  </ItemGroup>
-
-  <PropertyGroup>
-    <OutputType>Exe</OutputType>
-    <TargetFramework>net5.0</TargetFramework>
-  </PropertyGroup>
-
+    <PropertyGroup>
+        <OutputType>Exe</OutputType>
+        <TargetFramework>net5.0</TargetFramework>
+    </PropertyGroup>
 </Project>

Reply via email to