This topic provides the sample code for sending and subscribing to scheduled messages through the C/C++ SDK over TCP. The currently supported regions include the Internet Region, China (Hangzhou), China (Beijing), China (Shanghai), and China (Shenzhen).

Scheduled messages are consumed after a specified timestamp. These messages are sent when a time window is required between message production and consumption, or when tasks need to be triggered at a scheduled time.

For the concepts and use precautions of scheduled messages, see Scheduled messages and delayed messages.


Send scheduled messages

Note For more information about the sample code, see Message Queue for Apache RocketMQ code library.

The following is the sample code for sending scheduled messages:

#include "ONSFactory.h"
#include "ONSClientException.h"
using namespace ons;
int main()

    // Create a producer instance and configure the information required to send messages.
    ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
    factoryInfo.setFactoryProperty(ONSFactoryProperty::ProducerId, "XXX");//The group ID you created in the console.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::NAMESRV_ADDR, "XXX"); //The TCP endpoint. Go to the Instances page in the Message Queue for Apache RocketMQ console, and view the endpoint in the Endpoint Information section.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::PublishTopics,"XXX" );//The topic you created in the console.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::MsgContent, "xxx");//The message content.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::AccessKey, "xxx");//The AccessKey ID you created in the Alibaba Cloud console for identity authentication.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::SecretKey, "xxx" );//The AccessKey secret you created in the Alibaba Cloud console for identity authentication.

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

    // Before sending a message, call the start method once to start the producer.

    Message msg(
            // The message topic.
            // The message tag, which is similar to a Gmail tag. It is used to sort messages, enabling the consumer to filter messages on the Message Queue for Apache RocketMQ broker based on the specified criteria.       
            // The message body, which cannot be empty. Message Queue for Apache RocketMQ does not process the message body. The producer and consumer must negotiate consistent serialization and deserialization methods.

    // The message key, which must be globally unique.
    // A unique identifier enables you to query a message and resend it in the console if you fail to receive the message.
    // Note: Messages can still be sent and received even if this attribute is not set.

    // The delivery time, in ms. After the time is specified, a message can be consumed only after this time. In this example, a message can be consumed 3 seconds later.
    long deliverTime = Current system time (ms) + 3000;

    // The message sending result, which is successful if no exception occurs.     
        SendResultONS sendResult = pProducer->send(msg);
    catch(ONSClientException & e)
        // Customize exception handling details.

    // Destroy the producer object before exiting the application. Otherwise, memory leakage may occur.

    return 0;

Subscribe to scheduled messages

For instructions and sample code of subscribing to scheduled messages, see Subscribe to messages.