Send and receive delayed messages

更新时间:
复制 MD 格式

This topic provides the sample code that is used to send and receive delayed messages by using the TCP SDK for Java.

Prerequisites

You have completed the following operations:

Background information

Delayed messages are used to specify a period of time after messages are sent to the MSMQ server before they are delivered to the client for consumption (for example, three seconds before they are consumed). Delayed messages are suitable for scenarios where a time window is required for message production and consumption, or for scenarios where delayed tasks are triggered by messages, similar to delayed queues.

For more information about the concept of delayed messages and the precautions for using delayed 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 delayed messages

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

The following code provides an example on how to send a delayed message:

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());
        // Delayed messages, in milliseconds (ms). Delayed messages are delivered after the specified delay time (after the current time). For example, messages are delivered after 5 seconds.
        message.setStartDeliverTime(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(5));
        SendResult sendResult = producer.send(message);
        System.out.println(sendResult);
    }
}

Subscribe to delayed messages

The subscription of delayed messages is the same as that of normal messages. For more information, see Subscribe to messages.