All Products
Search
Document Center

ApsaraMQ for RocketMQ:Send and receive normal messages

Last Updated:Sep 24, 2024

Normal messages are featureless messages provided by ApsaraMQ for RocketMQ. Normal messages are different from featured messages, including scheduled messages, delayed messages, ordered messages, and transactional messages. Each topic can be used to send and receive messages of a specific type. For example, a topic that is used to send and receive normal messages cannot be used to send or receive messages of other types. This topic provides sample code on how to send and receive normal messages by using the TCP client SDK for C or C++.

Prerequisites

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 normal messages

The following sample code provides an example on how to send normal messages by using the TCP client SDK for C or C++:

#include "ONSFactory.h"
#include "ONSClientException.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(
            // The topic in which the normal message is produced. A topic that is used to send and receive normal messages cannot be used to send or receive messages of other types. 
            factoryInfo.getPublishTopics(),
            // The message tag. A message tag is similar to a Gmail tag and is used by consumers to filter messages in 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");

    // 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 your application, destroy the producer. If you do not destroy the producer, issues such as memory leaks may occur. 
    pProducer->shutdown();

    return 0;
}

Subscribe to normal messages

For instructions and sample code on how to subscribe to normal messages by using the TCP client SDK for C or C++, see Subscribe to messages.