Send and receive scheduled messages

更新时间:
复制 MD 格式

This topic describes how to use the TCP SDK for Java to send and subscribe to scheduled messages.

Precondition

Make sure that you have completed the following operations:

Background information

After a message is sent, it can be delivered to a consumer for consumption at a point in time after the current point in time. This method is suitable for scenarios where a time window is required for message production and consumption, or a scheduled task is triggered by the message.

For more information about scheduled messages, see Scheduled and delayed messages.

Note

For new users, we recommend that you read the demo project to learn how to build a MSMQ project before sending and receiving messages.

Send scheduled messages

For more information about the sample code, see the MSMQ code library.

The following code provides an example on how to send scheduled messages:

import java.util.Properties;
import java.util.concurrent.TimeUnit;
import com.alipay.sofa.sofamq.client.PropertyKeyConst;
import io.openmessaging.api.Message;
import io.openmessaging.api.MessagingAccessPoint;
import io.openmessaging.api.OMS;
import io.openmessaging.api.OMSBuiltinKeys;
import io.openmessaging.api.Producer;
import io.openmessaging.api.SendResult;

public class DelayProducerTest {
    public static void main(String... args) {
        Properties credentials = new Properties();
        // The AccessKey pair of an Alibaba Cloud account has permissions to access all API operations. This is a high risk. We strongly recommend that you create and use a RAM user for API access or routine O&M. Log on to the RAM console to create a RAM user. 
        // Save the AccessKey pair and AccessKeySecret in environment variables. 
        // We strongly recommend that you do not save the AccessKey and AccessKeySecret in the code. This may cause key leakage.
        credentials.setProperty(OMSBuiltinKeys.ACCESS_KEY, "SOFA_AK_ENV");        
        credentials.setProperty(OMSBuiltinKeys.SECRET_KEY, "SOFA_SK_ENV");
        // Set the TCP endpoint and go to the Overview page in the console to view the endpoint configuration.
        MessagingAccessPoint accessPoint = OMS.builder().driver("sofamq").endpoint("$endpoint")
                .withCredentials(credentials).build();
        Properties properties = new Properties();
        // Specify a user instance and go to the Overview page in the console to view the endpoint configuration.
        properties.setProperty(PropertyKeyConst.INSTANCE_ID, "$instanceId");
        properties.setProperty(PropertyKeyConst.GROUP_ID, "YOUR_GROUP");
        Producer producer = accessPoint.createProducer(properties);
        producer.start();
        Message message = new Message("$topic", "YOUR_TAG", "hello world".getBytes());
        // The scheduled message, in milliseconds (ms). The message is delivered at the specified timestamp (after the current time), for example, 2020-03-13 18:27:00
        long timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-03-13 18:27:00").getTime();
        message.setStartDeliverTime(timeStamp);
        SendResult sendResult = producer.send(message);
        System.out.println(sendResult);
    }
}

Subscribe to scheduled messages

The subscription method for scheduled messages is the same as that for normal messages. For more information, see Subscribe to messages.