Tüm Ürünler
Search
Belge Merkezi

ApsaraMQ for RocketMQ:Send and subscribe to scheduled messages

Son Güncelleme:Oct 04, 2026

Zamanlanmış mesajlar, hemen değil yalnızca belirtilen bir zaman damgasından sonra tüketicilere iletilir. Bu konuda, C/C++ için TCP istemci SDK'sını kullanarak zamanlanmış mesajların nasıl gönderileceği ve bunlara nasıl abone olunacağı gösterilmektedir.

Zamanlanmış mesajlar için iki yaygın kullanım senaryosu şunlardır:

  • Gecikmeli görev yürütme: Ödenmemiş bir siparişi 30 dakika sonra iptal etme veya belirli bir zamanda periyodik bir temizleme işini tetikleme.

  • Zaman penceresi tabanlı işleme: Mesaj üretimi ile tüketimi arasında kontrollü bir gecikme sağlama -- örneğin, kullanıcı kaydından 24 saat sonra hatırlatma bildirimi gönderme.

Zamanlanmış mesajlar ile gecikmeli mesajlar arasındaki farklar hakkında arka plan bilgisi için bkz. Zamanlanmış mesajlar ve gecikmeli mesajlar.

Önkoşullar

Başlamadan önce aşağıdakileri yaptığınızdan emin olun:

  • C/C++ SDK indirilmiş ve yüklenmiş olmalıdır

  • Geliştirme ortamınız Ortam hazırlığı (V1.x.x) bölümündeki talimatlara göre yapılandırılmış olmalıdır

  • Gerekli kaynaklara (örnek, konu ve tüketici grubu) sahip bir ApsaraMQ for RocketMQ örneğiniz bulunmalıdır

  • Alibaba Cloud hesabınız için bir AccessKey çifti oluşturulmuş olmalıdır

Zamanlanmış mesajları gönderin

Aşağıdaki örnekte, 3 saniyelik teslimat gecikmesine sahip bir zamanlanmış mesaj gönderilmektedir. setStartDeliverTime yöntemi, milisaniye cinsinden mutlak bir zaman damgası kabul eder. Aracı (broker), mesajı bu zaman damgasına kadar bekletir ve ardından tüketicilere iletir.

#include "ONSFactory.h"
#include "ONSClientException.h"

#include <windows.h>
using namespace ons;
int main()
{

    // Create the producer and set the required properties.
    ONSFactoryProperty factoryInfo;
    // The ID of the consumer group that you created in the ApsaraMQ for RocketMQ console.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::ProducerId, "XXX");
    // The TCP endpoint. Find this in the TCP Endpoint section of the Instance Details page in the ApsaraMQ for RocketMQ console.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::NAMESRV_ADDR, "XXX");
    // The topic that you created in the ApsaraMQ for RocketMQ console.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::PublishTopics,"XXX" );
    // The message content.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::MsgContent, "XXX");
    // Make sure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
    // AccessKey ID for authentication.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::AccessKey, getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
    // AccessKey secret for authentication.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::SecretKey, getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));

    // Create the producer.
    Producer *pProducer = ONSFactory::getInstance()->createProducer(factoryInfo);

    // Call start() once before sending messages.
    pProducer->start();

    Message msg(
            // Message topic.
            factoryInfo.getPublishTopics(),
            // Message tag, used by consumers to filter messages on the broker.
            "TagA",
            // Message body. Must not be empty. The producer and consumer must agree on the serialization and deserialization methods.
            factoryInfo.getMessageContent()
    );

    // The message key is a business-specific identifier. Keep it globally unique
    // so you can query the message by key in the ApsaraMQ for RocketMQ console.
    // Setting a key is optional.
    msg.setKey("ORDERID_100");

    // Set the delivery time as an absolute timestamp in milliseconds.
    // The broker delivers the message to consumers only after this time.
    // In this example, the message is delivered 3 seconds from now.
    long deliverTime = GetTickCount64() + 3000;
    msg.setStartDeliverTime(deliverTime);

    // Send the message. If no exception is thrown, the send succeeded.
    try
    {
        SendResultONS sendResult = pProducer->send(msg);
    }
    catch(ONSClientException & e)
    {
        // Handle the exception.
    }

    // Shut down the producer before exiting to avoid memory leaks.
    pProducer->shutdown();

    return 0;
}

XXX yer tutucularını gerçek değerlerinizle değiştirin:

Yer Tutucu Açıklama Nereden Bulunur
ProducerId Tüketici grubu kimliği ApsaraMQ for RocketMQ console > örneğiniz > Consumer Groups
NAMESRV_ADDR TCP uç noktası ApsaraMQ for RocketMQ console > Instance Details > TCP Endpoint
PublishTopics Konu adı ApsaraMQ for RocketMQ console > örneğiniz > Topics
MsgContent Mesaj gövdesi içeriği Uygulamanız tarafından tanımlanır

setStartDeliverTime için zaman biçimi kuralları ve sınırlamaları hakkında ayrıntılı bilgi için bkz. Zamanlanmış mesajlar ve gecikmeli mesajlar.

Zamanlanmış mesajlara abone olun

Zamanlanmış mesajlara abone olmak, normal mesajlara abone olmakla aynı şekilde çalışır. Tüketici tarafında ek bir yapılandırma gerekmez.

Tam abonelik kodu için bkz. Mesajlara abone olma.

Ayrıca bakınız