This topic provides the sample code for sending and subscribing to scheduled messages through the .NET SDK over TCP. The currently supported regions include the Internet Region, China (Hangzhou), China (Beijing), China (Shanghai), and China (Shenzhen).

Scheduled messages are consumed after a specified timestamp. These messages are sent when a time window is required between message production and consumption, or when tasks need to be triggered at a scheduled time.

For the concepts and use precautions of scheduled messages, see Scheduled messages and delayed messages.


Send scheduled messages

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

The following is the sample code for sending scheduled messages:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using ons;

namespace ons
    class onscsharp
        static void Main(string[] args)
            // The parameter required for the creation and normal operations of a producer, which must be set.
            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.
            ONSFactory onsfactory = new ONSFactory();
            Producer pProducer = onsfactory.getInstance().createProducer(factoryInfo);

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

            Message msg = new Message(
                // The message topic.
                // The message tag.
                // The message body.

            // 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 delivery time, in ms. After the time is specified, a message can be consumed only after this time. In this example, a message can be consumed 3 seconds later.
            long deliverTime = Current system time (ms) + 3000;

            // The message sending result, which is successful if no exception occurs.
                SendResultONS sendResult = pProducer.send(msg);
            catch(ONSClientException e)
                //Handle the sending failure.

            // Destroy the producer object before exiting the application. Otherwise, memory leakage may occur.


Subscribe to scheduled messages

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