Release Notes
Announcements
dotnet add package RocketMQ.Client --version 5.2.0-rc1
using System.Text;using System.Threading.Tasks;using Microsoft.Extensions.Logging;using Org.Apache.Rocketmq;namespace examples{internal static class ProducerNormalMessageDemo{static readonly ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());static ILogger logger = factory.CreateLogger("Program_Producer");internal static async Task QuickStart(){// Enable the switch if you use .NET Core 3.1 and want to disable TLS/SSL.AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);const string accessKey = "yourAccessKey";const string secretKey = "yourSecretKey";// Credential provider is optional for client configuration.var credentialsProvider = new StaticSessionCredentialsProvider(accessKey, secretKey);const string endpoints = "Tencent Cloud official website access address:8080";var clientConfig = new ClientConfig.Builder().SetEndpoints(endpoints).SetCredentialsProvider(credentialsProvider).Build();const string topic = "topicName";// In most case, you don't need to create too many producers, single pattern is recommended.// Producer here will be closed automatically.var producer = await new Producer.Builder()// Set the topic name(s), which is optional but recommended.// It makes producer could prefetch the topic route before message publishing..SetTopics(topic).SetClientConfig(clientConfig).Build();// Define your message body.var bytes = Encoding.UTF8.GetBytes("foobar");const string tag = "yourMessageTagA";var message = new Message.Builder().SetTopic(topic).SetBody(bytes).SetTag(tag)// You could set multiple keys for the single message actually..SetKeys("yourMessageKey-7044358f98fc").Build();var sendReceipt = await producer.Send(message);logger.LogInformation($"Send message successfully, messageId={sendReceipt.MessageId}");// Close the producer if you don't need it anymore.await producer.DisposeAsync();}}}
Parameter | Description |
accessKey | Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console. |
secretKey | Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console. |
endpoints | Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console. |
topic | Topic name. You can copy the name from the Topic Management page in the console. |
using System;using System.Collections.Generic;using System.Threading.Tasks;using Microsoft.Extensions.Logging;using Org.Apache.Rocketmq;namespace examples{internal static class SimpleConsumerExample{static readonly ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());static ILogger logger = factory.CreateLogger("Program_Consumer");internal static async Task QuickStart(){// Enable the switch if you use .NET Core 3.1 and want to disable TLS/SSL.AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);const string accessKey = "yourAccessKey";const string secretKey = "yourSecretKey";// Credential provider is optional for client configuration.var credentialsProvider = new StaticSessionCredentialsProvider(accessKey, secretKey);const string endpoints = "rmq-xxx.rocketmq.gz.qcloud.tencenttdmq.com:8080"; // Tencent Cloud access point.var clientConfig = new ClientConfig.Builder().SetEndpoints(endpoints).SetCredentialsProvider(credentialsProvider).Build();// Add your subscriptions.const string consumerGroup = "yourConsumerGroup";const string topic = "yourTopic";var subscription = new Dictionary<string, FilterExpression>{ { topic, new FilterExpression("*") } };// In most case, you don't need to create too many consumers, single pattern is recommended.var simpleConsumer = await new SimpleConsumer.Builder().SetClientConfig(clientConfig).SetConsumerGroup(consumerGroup).SetAwaitDuration(TimeSpan.FromSeconds(15)).SetSubscriptionExpression(subscription).Build();while (true){var messageViews = await simpleConsumer.Receive(16, TimeSpan.FromSeconds(15));foreach (var message in messageViews){logger.LogInformation($"Received a message, topic={message.Topic}, message-id={message.MessageId}, body-size={message.Body.Length}");await simpleConsumer.Ack(message);logger.LogInformation($"Message is acknowledged successfully, message-id={message.MessageId}");// await simpleConsumer.ChangeInvisibleDuration(message, TimeSpan.FromSeconds(15));// Logger.LogInformation($"Changing message invisible duration successfully, message=id={message.MessageId}");}}// Close the simple consumer if you don't need it anymore.// await simpleConsumer.DisposeAsync();}}}
using System;using System.Collections.Generic;using System.Threading;using System.Threading.Tasks;using Microsoft.Extensions.Logging;using Org.Apache.Rocketmq;namespace examples{public class PushConsumerExample{private static readonly ILogger Logger = MqLogManager.CreateLogger(typeof(PushConsumerExample).FullName);private static readonly string Endpoint = Environment.GetEnvironmentVariable("ROCKETMQ_ENDPOINT");internal static async Task QuickStart(){const string accessKey = "yourAccessKey";const string secretKey = "yourSecretKey";// Enable the switch if you use .NET Core 3.1 and want to disable TLS/SSL.// AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);// Credential provider is optional for client configuration.var credentialsProvider = new StaticSessionCredentialsProvider(accessKey, secretKey);const string endpoints = "rmq-xxx.rocketmq.gz.qcloud.tencenttdmq.com:8080"; // Tencent Cloud access point.var clientConfig = new ClientConfig.Builder().SetEndpoints(Endpoint).SetCredentialsProvider(credentialsProvider).Build();// Add your subscriptions.const string consumerGroup = "yourConsumerGroup";const string topic = "yourTopic";var subscription = new Dictionary<string, FilterExpression>{ { topic, new FilterExpression("*") } };var pushConsumer = await new PushConsumer.Builder().SetClientConfig(clientConfig).SetConsumerGroup(consumerGroup).SetSubscriptionExpression(subscription).SetMessageListener(new CustomMessageListener()).Build();Thread.Sleep(Timeout.Infinite);// Close the push consumer if you don't need it anymore.// await pushConsumer.DisposeAsync();}private class CustomMessageListener : IMessageListener{public ConsumeResult Consume(MessageView messageView){// Handle the received message and return consume result.Logger.LogInformation($"Consume message={messageView}");return ConsumeResult.SUCCESS;}}}}
Parameter | Description |
accessKey | Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console. |
secretKey | Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console. |
endpoints | Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console. |
consumerGroup | Consumer group name. You can copy the name from the Group Management page in the console. |
topic | Topic name. You can copy the name from the Topic Management page in the console. |
フィードバック