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.
Prerequisites
- The C/C++ SDK has been downloaded. For more information about versions, see Release notes.
- The environment has been prepared. You need to prepare the environment according to the SDK version you are using:
Send scheduled messages
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.
pProducer->start();
Message msg(
// The message topic.
factoryInfo.getPublishTopics(),
// 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.
"TagA",
// 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.
factoryInfo.getMessageContent()
);
// 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.
msg.setKey("ORDERID_100");
// 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;
msg.setStartDeliverTime(deliverTime);
// The message sending result, which is successful if no exception occurs.
try
{
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.
pProducer->shutdown();
return 0;
}
Subscribe to scheduled messages
For instructions and sample code of subscribing to scheduled messages, see Subscribe to messages.