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.

Prerequisites

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();   
    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 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 normal messages

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