Unlike scheduled messages, delayed messages, ordered messages, and transactional messages, normal messages are messages without any special features in Message Queue for Apache RocketMQ. This topic provides the sample code for sending and subscribing to normal messages through the C/C++ SDK over TCP.


You have completed the following operations:

Send normal messages

Note For more information about the sample code, see Message Queue for Apache RocketMQ code library.

Follow the sample code to send 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();   
    //The group ID you created in the Message Queue for Apache RocketMQ console.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::ProducerId, "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::NAMESRV_ADDR, "XXX"); 
    //The topic you created in the console.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::PublishTopics,"XXX" );
    //The message content.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::MsgContent, "XXX");
    // The AccessKey ID is used as an identifier of Alibaba Cloud service users. For more information, see Create an AccessKey pair.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::AccessKey, "XXX");
    // The AccessKey Secret is used to verify the identity of Alibaba Cloud service users. For more information, see Create an AccessKey pair.
    factoryInfo.setFactoryProperty(ONSFactoryProperty::SecretKey, "XXX" );

    // Create a producer instance.
    Producer *pProducer = ONSFactory::getInstance()->createProducer(factoryInfo);

    // Before sending a message, call the start method once to start the producer.

    Message msg(
            // The message topic.
            // 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.       
            // 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.

    // 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.

    // The message sending result, which is successful if no exception occurs.     
        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.

    return 0;

Subscribe to normal messages

For instructions and sample code of subscribing to normal messages, see Subscribe to messages.