This topic provides sample code to show you how to send and subscribe to scheduled messages by using SDK for C/C++ over TCP.

Delayed messages are delivered to a client after a delay. For example, if the delay period is 3 seconds, messages are forwarded from the Message Queue for Apache RocketMQ broker to the client for consumption after a 3 second delay. Delayed messages can be used when a time window is required between the production and consumption of the messages, or when tasks need to be triggered after a delay. Delayed messages are similar to delayed queues.

For information about the concepts and precautions of delayed messages, see Scheduled messages and delayed messages.


You have completed the following operations:

Send scheduled messages

The sample code for sending scheduled messages is as follows:

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

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

    // Create a producer and set the parameters that are required to send messages. 
    ONSFactoryProperty factoryInfo;
    //The group ID you created in the Message Queue for Apache RocketMQ console.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::ProducerId, "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::NAMESRV_ADDR, "XXX"); 
    //The topic you created in the console.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::PublishTopics,"XXX" );
    //The message content.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::MsgContent, "XXX");
    // The AccessKey ID is used as an identifier of Alibaba Cloud service users. For more information, see Create an AccessKey pair.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::AccessKey, "XXX");
    // The AccessKey Secret is used to verify the identity of Alibaba Cloud service users. For more information, see Create an AccessKey pair.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::SecretKey, "XXX" ); 

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

    // Before you send messages, call the start method once to start the producer. 

    Message msg(
            //Message Topic
            // The tag of the message, which is similar to a Gmail tag. Message tags are used to sort messages and filter messages for the consumer on the Message Queue for Apache RocketMQ broker based on specified conditions.       
            // The body of the message. This parameter is required. Message Queue for Apache RocketMQ does not process the message body. The producer and consumer must agree on the methods to serialize and deserialize the message body. 

    // The key of the message. The key is the business-specific attribute of the message and must be globally unique whenever possible. 
    // The key helps you query and resend a message in the Message Queue for Apache RocketMQ console if the message fails to be received. 
    // Note: Messages can be sent and received even if you do not specify message keys. 

    // The time when the Message Queue for Apache RocketMQ broker delivers the message to the consumer. Unit: milliseconds. The message can be consumed only after the specified time. In this example, the message can be consumed 3 seconds later. 
    long deliverTime = GetTickCount64() + 3000;

    // Send the message. If no exception occurs, the message is sent.      
        SendResultONS sendResult = pProducer->send(msg);
    catch(ONSClientException & e)
        // Specify the logic to process the exception. 

    // Before you exit the application, shut down the producer. Otherwise, issues such as memory leaks occur. 

    return 0;

Subscribe to scheduled messages

The mode for subscribing to scheduled messages is the same as that for subscribing to normal messages. For more information, see Subscribe to messages.