This topic provides sample code on how to send and receive scheduled messages by using the TCP client SDK for Java.
Background information
Scheduled messages are consumed after a predefined timestamp. Scheduled messages can be used in scenarios in which a time window between message production and consumption is required, or in which scheduled tasks are triggered by using messages.
For information about the terms that are used for scheduled messages and the precautions that you must take when you use scheduled messages, see Scheduled messages and delayed messages.
Note
If you are a new user of ApsaraMQ for RocketMQ, we recommend that you refer to the Demo project to build an ApsaraMQ for RocketMQ project to send and receive messages.
Prerequisites
Before you start, make sure that the following operations are performed:
The SDK for Java is installed. For more information, see Prepare the environment.
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.
Optional. Logging settings are configured. for more information, see Logging settings.
Send scheduled messages
For information about the sample code, see ApsaraMQ for RocketMQ code library.
The following sample code provides an example on how to send scheduled messages by using the TCP client SDK for Java:
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.SendResult;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Properties;
public class ProducerDelayTest {
public static void main(String[] args) {
Properties properties = new Properties();
properties.put(PropertyKeyConst.AccessKey, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
properties.put(PropertyKeyConst.SecretKey, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
properties.put(PropertyKeyConst.NAMESRV_ADDR,
"XXX");
Producer producer = ONSFactory.createProducer(properties);
producer.start();
Message msg = new Message(
"Topic",
"tag",
"Hello MQ".getBytes());
msg.setKey("ORDERID_100");
try {
long timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2016-03-07 16:21:00").getTime();
msg.setStartDeliverTime(timeStamp);
SendResult sendResult = producer.send(msg);
System.out.println("Message Id:" + sendResult.getMessageId());
}
catch (Exception e) {
System.out.println(new Date() + " Send mq message failed. Topic is:" + msg.getTopic());
e.printStackTrace();
}
producer.shutdown();
}
}
Subscribe to scheduled messages
The sample code for subscribing scheduled messages is the same as that for subscribing to normal messages. For more information, see Subscribe to messages.