Tüm Ürünler
Search
Belge Merkezi

ApsaraMQ for RocketMQ:Send and receive normal messages

Son Güncelleme:Oct 04, 2026

Normal mesajlar, ApsaraMQ for RocketMQ içindeki temel mesaj türüdür. Zamanlanmış, gecikmeli, sıralı ve işlemsel mesajlardan farklı olarak normal mesajların özel teslimat veya sıralama semantiği bulunmaz. Zamanlama veya sıralama kısıtlaması gerektirmeyen güvenilir eşzamansız iletişim senaryolarında normal mesajları kullanın.

Bu sayfadaki örnekler .NET için TCP istemci SDK'sını kullanır.

Ön koşullar

Başlamadan önce aşağıdaki ön koşulları karşıladığınızdan emin olun:

  • .NET için SDK yüklü olmalıdır. Daha fazla bilgi için bkz. Sürüm notları

  • Geliştirme ortamınız hazırlanmış olmalıdır. Daha fazla bilgi için bkz. Ortamı hazırlayın

  • ApsaraMQ for RocketMQ kaynakları (örnekler, konular ve tüketici grupları) konsolda oluşturulmuş olmalıdır. Daha fazla bilgi için bkz. Kaynakları oluşturun

  • Alibaba Cloud hesabınız için bir AccessKey çifti bulunmalıdır. Daha fazla bilgi için bkz. AccessKey çifti oluşturun

Normal mesaj gönderin

Not

Tam örnek proje için ApsaraMQ for RocketMQ kod deposuna bakın.

Aşağıdaki örnek, .NET için TCP istemci SDK'sını kullanarak normal mesajlar gönderir. Yer tutucuları kendi değerlerinizle değiştirin.

using System;
using ons;

public class ProducerExampleForEx
{
    public ProducerExampleForEx()
    {
    }

    static void Main(string[] args) {
        // Configure the producer properties.
        ONSFactoryProperty factoryInfo = new ONSFactoryProperty();

        // Load credentials from environment variables.
        // Set ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET
        // before running this example.
        factoryInfo.setFactoryProperty(ONSFactoryProperty::AccessKey, getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
        factoryInfo.setFactoryProperty(ONSFactoryProperty::SecretKey, getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));

        // Specify the Group ID created in the ApsaraMQ for RocketMQ console.
        factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, "<GROUP_ID>");

        // Specify the topic created in the ApsaraMQ for RocketMQ console.
        factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, "<TOPIC>");

        // Specify the TCP endpoint from the Instance Details page
        // in the ApsaraMQ for RocketMQ console.
        factoryInfo.setFactoryProperty(ONSFactoryProperty.NAMESRV_ADDR, "<TCP_ENDPOINT>");

        // Specify the log file path.
        factoryInfo.setFactoryProperty(ONSFactoryProperty.LogPath, "C://log");

        // Create a producer instance.
        // Producer instances are thread-safe. A single instance can send
        // messages to multiple topics. In most cases, one instance per thread is sufficient.
        Producer producer = ONSFactory.getInstance().createProducer(factoryInfo);

        // Start the producer.
        producer.start();

        // Build a message with a topic, tag, and body.
        Message msg = new Message(factoryInfo.getPublishTopics(), "tagA", "Example message body");
        msg.setKey(Guid.NewGuid().ToString());

        // Send 32 messages in a loop.
        for (int i = 0; i < 32; i++) {
            try
            {
                SendResultONS sendResult = producer.send(msg);
                Console.WriteLine("send success {0}", sendResult.getMessageId());
            }
            catch (Exception ex)
            {
                Console.WriteLine("send failure{0}", ex.ToString());
            }
        }

        // Shut down the producer before exiting the thread.
        producer.shutdown();
    }
}

Aşağıdaki yer tutucuları kendi değerlerinizle değiştirin:

Yer Tutucu Açıklama Örnek
<GROUP_ID> ApsaraMQ for RocketMQ konsolunda oluşturduğunuz Grup Kimliği GID_example
<TOPIC> ApsaraMQ for RocketMQ konsolunda oluşturduğunuz konu T_example_topic_name
<TCP_ENDPOINT> Instance Details sayfasının TCP Endpoint bölümündeki TCP uç noktası --

Kod incelemesi

  1. Özellikleri yapılandırın -- ONSFactoryProperty aracılığıyla kimlik bilgilerini, Grup Kimliğini, konuyu, TCP uç noktasını ve günlük yolunu ayarlayın. AccessKey ID ve AccessKey Secret değerlerini ALIBABA_CLOUD_ACCESS_KEY_ID ve ALIBABA_CLOUD_ACCESS_KEY_SECRET ortam değişkenlerinden yükleyin.

  2. Üreticiyi oluşturun ve başlatın -- ONSFactory.getInstance().createProducer(factoryInfo) çağrısı, iş parçacığı açısından güvenli bir üretici örneği döndürür. Birden fazla konuya mesaj gönderirken bile çoğu durumda her iş parçacığı için bir örnek yeterlidir.

  3. Mesajları oluşturun ve gönderin -- Konu, etiket ve gövde içeren bir Message nesnesi oluşturun. Mesaj izleme için Guid.NewGuid() ile benzersiz bir anahtar atayın. Başarılı olduğunda producer.send(msg) çağrısı, mesaj kimliğini içeren bir SendResultONS döndürür.

  4. Kapatma işlemi -- Üretici örneğini sonlandırmak için iş parçacığı çıkmadan önce producer.shutdown() yöntemini çağırın.

Normal mesajlara abone olun

.NET için TCP istemci SDK'sını kullanarak normal mesajlara abone olma talimatları ve örnek kod için bkz. Mesajlara abone olun.

Sonraki adımlar

  • Mesajlara abone olun -- .NET için TCP istemci SDK'sını kullanarak mesajları alan bir tüketici yapılandırın

  • Geliştirici Referansındaki diğer mesaj türlerini keşfedin: zamanlanmış mesajlar, gecikmeli mesajlar, sıralı mesajlar ve işlemsel mesajlar farklı teslimat semantiklerini destekler