All Products
Search
Document Center

ApsaraMQ for RocketMQ:Send and receive normal messages

Last Updated:Mar 11, 2026

Normal messages in ApsaraMQ for RocketMQ have no special delivery semantics -- they differ from scheduled, delayed, ordered, and transactional messages.

Important

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:

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:

PlaceholderDescriptionWhere to find it
<your-group-id>Producer group IDApsaraMQ for RocketMQ console
<your-tcp-endpoint>TCP endpoint URLTCP Endpoint section on the Instance Details page
<your-topic>Topic nameApsaraMQ for RocketMQ console
<your-message-content>Message body contentDefined 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

PropertyDetails
TagA secondary classifier on each message. Consumers use tags to filter messages at the broker side, so only relevant messages are delivered.
KeyA 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.
BodyThe 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

PhaseMethodNotes
StartpProducer->start()Call once before sending any messages.
SendpProducer->send(msg)If no exception is thrown, the message is sent.
Shut downpProducer->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.