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

Prerequisites

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;
    factoryInfo.setFactoryProperty(ONSFactoryProperty::ProducerId, "XXX");// The ID of the group that you created in the Message Queue for Apache RocketMQ console. 
    factoryInfo.setFactoryProperty(ONSFactoryProperty.NAMESRV_ADDR, "XXX"); // The TCP endpoint of your instance. To obtain the TCP endpoint, log on to the Message Queue for Apache RocketMQ console. In the left-side navigation pane, click Instances. On the Instances page, click the name of your instance. On the Instance Details page, view the endpoint on the Endpoint Information tab. 
    factoryInfo.setFactoryProperty(ONSFactoryProperty::PublishTopics,"XXX" );// The topic that you created in the Message Queue for Apache RocketMQ console. 
    factoryInfo.setFactoryProperty(ONSFactoryProperty::MsgContent, "xxx");// The content of the message. 
    factoryInfo.setFactoryProperty(ONSFactoryProperty::AccessKey, "xxx");// The AccessKey ID that you created in the Alibaba Cloud Management Console for identity authentication. 
    factoryInfo.setFactoryProperty(ONSFactoryProperty::SecretKey, "xxx" );// The AccessKey secret that you created in the Alibaba Cloud Management Console for identity authentication. 

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

    // Before you send messages, call the start method once to start the producer. 
    pProducer->start();

    Message msg(
            //Message Topic
            factoryInfo.getPublishTopics(),
            // 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.       
            "TagA",
            // 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. 
            factoryInfo.getMessageContent()
    );

    // 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. 
    msg.setKey("ORDERID_100");

    // 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;
    msg.setStartDeliverTime(deliverTime);

    // Send the message. If no exception occurs, the message is sent.      
    try
    {
        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. 
    pProducer->shutdown();

    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.