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 predefined timestamp. Scheduled messages can be used in scenarios in which a time window between message production and consumption is required, or in which scheduled tasks are triggered by using messages.
For information about the terms that are used for scheduled messages and the precautions that you must take when you use scheduled messages, see Scheduled messages and delayed messages.
Prerequisites
Before you start, make sure that the following operations are performed:
The SDK for C or C++ is downloaded. For more information, see Release notes.
The environment is prepared. For more information, see Environment preparation (V1.x.x).
The resources that you want to specify in the code are created in the ApsaraMQ for RocketMQ console. The resources include instances, topics, and consumer groups. For more information, see Create resources.
The AccessKey pair of your Alibaba Cloud account is obtained. For more information, see Create an AccessKey pair.
Send scheduled messages
The following sample code provides an example on how to send scheduled messages by using the TCP client SDK for Java:
#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.