Bu konuda, ApsaraMQ for RocketMQ tarafından sağlanan .NET TCP istemci SDK'sını kullanarak mesajlara nasıl abone olunacağı açıklanmaktadır.
Aynı grup kimliği (group ID) ile tanımlanan tüm tüketici örnekleri için abonelik tutarlılığını korumanız gerekir. Daha fazla bilgi için bkz. Abonelik tutarlılığı.
ApsaraMQ for RocketMQ aşağıdaki abonelik modlarını destekler:
-
Kümeleme aboneliği
Aynı grup kimliği ile tanımlanan tüm tüketiciler eşit sayıda mesaj tüketir. Örneğin, bir konu dokuz mesaj içeriyorsa ve bir tüketici grubu üç tüketici barındırıyorsa kümeleme tüketim modunda her tüketici üç mesaj tüketir.
// Configure clustering subscription, which is the default mode. factoryInfo.setFactoryProperty(ONSFactoryProperty.MessageModel, ONSFactoryProperty.CLUSTERING); -
Yayın aboneliği
Aynı grup kimliği ile tanımlanan her tüketici, tüm mesajları bir kez tüketir. Örneğin, bir konu dokuz mesaj içeriyorsa ve bir tüketici grubu üç tüketici barındırıyorsa yayın tüketim modunda her tüketici dokuz mesajın tamamını tüketir.
// Configure broadcasting subscription. factoryInfo.setFactoryProperty(ONSFactoryProperty.MessageModel, ONSFactoryProperty.BROADCASTING);
Örnek kod:
using System;
using System.Threading;
using System.Text;
using ons;
// The callback function that is executed when a message is pulled from the ApsaraMQ for RocketMQ broker.
public class MyMsgListener : MessageListener
{
public MyMsgListener()
{
}
~MyMsgListener()
{
}
public override ons.Action consume(Message value, ConsumeContext context)
{
Byte[] text = Encoding.Default.GetBytes(value.getBody());
Console.WriteLine(Encoding.UTF8.GetString(text));
return ons.Action.CommitMessage;
}
}
public class ConsumerExampleForEx
{
public ConsumerExampleForEx()
{
}
static void Main(string[] args) {
ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
// Make sure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are configured.
// The AccessKey ID that is used for authentication.
factoryInfo.setFactoryProperty(ONSFactoryProperty::AccessKey, getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
// The AccessKey secret that is used for authentication.
factoryInfo.setFactoryProperty(ONSFactoryProperty::SecretKey, getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// The ID of the consumer group that you created in the ApsaraMQ for RocketMQ console.
factoryInfo.setFactoryProperty(ONSFactoryProperty.ConsumerId, "GID_example");
// The topic that you created in the ApsaraMQ for RocketMQ console.
factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, "T_example_topic_name");
// The TCP endpoint. You can obtain the endpoint in the TCP Endpoint section of the Instance Details page in the ApsaraMQ for RocketMQ console.
factoryInfo.setFactoryProperty(ONSFactoryProperty.NAMESRV_ADDR, "NameSrv_Addr");
// The log path.
factoryInfo.setFactoryProperty(ONSFactoryProperty.LogPath, "C://log");
// Clustering consumption.
// factoryInfo.setFactoryProperty(ONSFactoryProperty:: MessageModel, ONSFactoryProperty.CLUSTERING);
// Broadcasting consumption.
// factoryInfo.setFactoryProperty(ONSFactoryProperty:: MessageModel, ONSFactoryProperty.BROADCASTING);
// Create the consumer instance.
PushConsumer consumer = ONSFactory.getInstance().createPushConsumer(factoryInfo);
// Subscribe to the topic.
consumer.subscribe(factoryInfo.getPublishTopics(), "*", new MyMsgListener());
// Start the consumer instance.
consumer.start();
// This setting is used only in this demo. In actual production environments, you cannot exit the process.
Thread.Sleep(300000);
// Before you exit the process, terminate the consumer instance.
consumer.shutdown();
}
}