All Products
Search
Document Center

ApsaraMQ for RocketMQ:Send and receive scheduled messages

Last Updated:Aug 18, 2023

This topic provides sample code on how to send and receive scheduled messages by using the TCP client SDK for C or C++.

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

For information about the concepts and usage notes of scheduled messages, see Scheduled messages and delayed messages.

Prerequisites

Before you start, make sure that the following operations are performed:

Send scheduled messages

The following code provides an example on how to send scheduled messages:

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

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

    // Create the producer and configure the parameters that are required to send messages. 
    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. You can obtain the endpoint 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 that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are configured. 
    // The AccessKey ID that is used for authentication. 
    factoryInfo.setFactoryProperty(ONSFactoryProperty::AccessKey, getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
		// The AccessKey secret that is used for authentication. 
    factoryInfo.setFactoryProperty(ONSFactoryProperty::SecretKey, getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));

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

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

    Message msg(
            //Message Topic
            factoryInfo.getPublishTopics(),
            // The message tag. A message tag is similar to a tag in Gmail and is used by consumers to filter messages on the ApsaraMQ for RocketMQ broker.       
            "TagA",
            // The message body. You cannot leave this parameter empty. ApsaraMQ for RocketMQ does not process message bodies. The producer and consumer must agree on the methods that are used to serialize and deserialize message bodies. 
            factoryInfo.getMessageContent()
    );

    // The message key. The key is the business-specific attribute of a message and must be globally unique whenever possible. 
    // If you cannot receive a message as expected, you can use the key to query the message in the ApsaraMQ for RocketMQ console. 
    // Note: You can send and receive a message even if you do not specify the key. 
    msg.setKey("ORDERID_100");

    // The time after which the broker delivers the message to the consumer. Unit: milliseconds. The message can be consumed only after the specified time elapses. In this example, the message can be consumed after a delay of 3 seconds. 
    long deliverTime = GetTickCount64() + 3000;
    msg.setStartDeliverTime(deliverTime);

    // Send the message. If no exception is thrown, the message is sent.      
    try
    {
        SendResultONS sendResult = pProducer->send(msg);
    }
    catch(ONSClientException & e)
    {
        // Specify the logic to handle exceptions. 
    }

    // Before you exit the application, destroy the producer. Otherwise, issues such as memory leaks occur. 
    pProducer->shutdown();

    return 0;
}
            

Subscribe to scheduled messages

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