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

Run the following code to send messages. Set parameters according to the instructions.

using System;
using ons;

public class ProducerExampleForEx
{
    public ProducerExampleForEx()
    {
    }

    static void Main(string[] args) {
        // Configure your account according to the settings in the console.
        ONSFactoryProperty factoryInfo = new ONSFactoryProperty();
        // The AccessKey ID you created in the Alibaba Cloud console for identity authentication.
        factoryInfo.setFactoryProperty(ONSFactoryProperty.AccessKey, "Your access key");
        // The AccessKey secret you created in the Alibaba Cloud console for identity authentication.
        factoryInfo.setFactoryProperty(ONSFactoryProperty.SecretKey, "Your access secret");
        // The group ID you created in the console.
        factoryInfo.setFactoryProperty(ONSFactoryProperty.ProducerId, "GID_example");
        // The topic you created in the console.
        factoryInfo.setFactoryProperty(ONSFactoryProperty.PublishTopics, "T_example_topic_name");
        // 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, "NameSrv_Addr");
        // The log path.
        factoryInfo.setFactoryProperty(ONSFactoryProperty.LogPath, "C://log");

        // Create producer instances.
        // Note: Producer instances are thread-secure and can be used to send messages of different topics. Each thread
        // needs only one producer instance.
        Producer producer = ONSFactory.getInstance().createProducer(factoryInfo);

        // Start the consumer instance.
        producer.start();

        // Create message objects.
        Message msg = new Message(factoryInfo.getPublishTopics(), "tagA", "Example message body");
        msg.setKey(Guid.NewGuid().ToString());
        for (int i = 0; i < 32; i++) {
            try
            {
                SendResultONS sendResult = producer.send(msg);
                Console.WriteLine("send success {0}", sendResult.getMessageId());
            }
            catch (Exception ex)
            {
                Console.WriteLine("send failure{0}", ex.ToString());
            }
        }

        // Disable the producer instance when the thread is about to exit.
        producer.shutdown();

    }
}    

Subscribe to normal messages

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