Normal messages in ApsaraMQ for RocketMQ have no special delivery semantics -- they differ from scheduled, delayed, ordered, and transactional messages.
Each topic supports only one message type. A topic created for normal messages cannot send or receive other message types.
The following sections walk through sending and receiving normal messages with the TCP client SDK for C/C++.
Prerequisites
Before you begin, make sure that you have:
Created the required resources -- an instance, a topic, and a consumer group -- in the ApsaraMQ for RocketMQ console
Created an AccessKey pair for your Alibaba Cloud account
Send normal messages
The following example creates a producer, connects to the broker, and sends a single normal message.
Replace the placeholders in the code with your actual values:
| Placeholder | Description | Where to find it |
|---|---|---|
<your-group-id> | Producer group ID | ApsaraMQ for RocketMQ console |
<your-tcp-endpoint> | TCP endpoint URL | TCP Endpoint section on the Instance Details page |
<your-topic> | Topic name | ApsaraMQ for RocketMQ console |
<your-message-content> | Message body content | Defined by your application |
#include "ONSFactory.h"
#include "ONSClientException.h"
using namespace ons;
int main()
{
// Configure producer properties
ONSFactoryProperty factoryInfo;
factoryInfo.setFactoryProperty(ONSFactoryProperty::ProducerId, "<your-group-id>");
factoryInfo.setFactoryProperty(ONSFactoryProperty::NAMESRV_ADDR, "<your-tcp-endpoint>");
factoryInfo.setFactoryProperty(ONSFactoryProperty::PublishTopics, "<your-topic>");
factoryInfo.setFactoryProperty(ONSFactoryProperty::MsgContent, "<your-message-content>");
// Load credentials from environment variables.
// Make sure ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
factoryInfo.setFactoryProperty(ONSFactoryProperty::AccessKey, getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
factoryInfo.setFactoryProperty(ONSFactoryProperty::SecretKey, getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// Create and start the producer.
// Call start() exactly once before sending any messages.
Producer *pProducer = ONSFactory::getInstance()->createProducer(factoryInfo);
pProducer->start();
// Build the message
Message msg(
factoryInfo.getPublishTopics(), // Topic (must be a normal-message topic)
"TagA", // Tag -- consumers use tags to filter messages
factoryInfo.getMessageContent() // Body -- cannot be empty
);
// Set a message key for traceability.
// Keys must be globally unique whenever possible (for example, an order ID).
// Use the key to look up messages in the ApsaraMQ for RocketMQ console.
msg.setKey("ORDERID_100");
// Send the message. If no exception is thrown, the message is sent.
try
{
SendResultONS sendResult = pProducer->send(msg);
}
catch(ONSClientException & e)
{
// Handle the send failure (log, retry, alert, etc.)
}
// Shut down the producer before your application exits to prevent issues such as memory leaks.
pProducer->shutdown();
return 0;
}Message properties
| Property | Details |
|---|---|
| Tag | A secondary classifier on each message. Consumers use tags to filter messages at the broker side, so only relevant messages are delivered. |
| Key | A business identifier (such as an order ID) that must be globally unique whenever possible. Use it to query specific messages in the ApsaraMQ for RocketMQ console. Setting a key is optional -- messages can be sent and received without one. |
| Body | The message payload. ApsaraMQ for RocketMQ does not process or parse the body. Producer and consumer must agree on the serialization and deserialization format. The body cannot be empty. |
Producer lifecycle
| Phase | Method | Notes |
|---|---|---|
| Start | pProducer->start() | Call once before sending any messages. |
| Send | pProducer->send(msg) | If no exception is thrown, the message is sent. |
| Shut down | pProducer->shutdown() | Call before the application exits. Skipping this step may cause issues such as memory leaks. |
Subscribe to normal messages
For consumer setup and sample code using the TCP client SDK for C/C++, see Subscribe to messages.