edit-icon download-icon

Send and receive delayed messages

Last Updated: Jun 19, 2018

Delayed messages can be delivered to the client and consumed after being sent to the MQ server for a specified period (for example, 3 seconds). They are used in scenarios where there are time window requirements for message production and consumption, or when messages are used to trigger delayed tasks, similar to delay queues.

To learm more about delayed messages, see Scheduled and delayed messages.

Note: Please refer to TCP access instruction for TCP access point domain names.

Send Delayed Messages

The sample code for sending delayed message is as follows:

  1. import com.aliyun.openservices.ons.api.Message;
  2. import com.aliyun.openservices.ons.api.ONSFactory;
  3. import com.aliyun.openservices.ons.api.Producer;
  4. import com.aliyun.openservices.ons.api.PropertyKeyConst;
  5. import com.aliyun.openservices.ons.api.SendResult;
  6. import java.util.Properties;
  7. public class ProducerDelayTest {
  8. public static void main(String[] args) {
  9. Properties properties = new Properties();
  10. // The producer ID you created on the console
  11. properties.put(PropertyKeyConst.ProducerId, "XXX");
  12. // AccessKey, Alibaba Cloud ID verification, which is created on Alibaba Cloud Management Console
  13. properties.put(PropertyKeyConst.AccessKey, "XXX");
  14. // SecretKey, Alibaba Cloud ID verification, which is created on Alibaba Cloud Management Console
  15. properties.put(PropertyKeyConst.SecretKey, "XXX");
  16. // Set a TCP access domain name (the following uses public cloud production environment as an example)
  17. properties.put(PropertyKeyConst.ONSAddr,
  18. "http://onsaddr-internal.aliyun.com:8080/rocketmq/nsaddr4client-internal");
  19. Producer producer = ONSFactory.createProducer(properties);
  20. // Before sending messages, the start method must be called once to start the producer.
  21. producer.start();
  22. Message msg = new Message( //
  23. // The topic you created on the console
  24. "Topic",
  25. //Message tag, which is similar to tag in Gmail, and is used to classify messages. Consumers can then set filtering conditions for messages to be filtered in MQ broker.
  26. tag",
  27. // Message body, which can be any data in binary format. Consistent serialization and deserialization methods should be consistent between the producer and the consumer.
  28. "Hello MQ".getBytes());
  29. // The setting represents the key service property of the message, so please set it as globally unique as possible.
  30. // You can query a message and resend it through the MQ console when you cannot receive the message properly.
  31. // Note: Normal sessage sending and receiving will not be affected if message key is not configured.
  32. msg.setKey("ORDERID_100");
  33. try {
  34. // Delay time (ms), which specifies a time point, after which the message is delivered and consumed. The example means that the message will be consumed in 3 seconds.
  35. long delayTime = System.currentTimeMillis() + 3000;
  36. // Set the time when the message is to be delivered.
  37. msg.setStartDeliverTime(delayTime);
  38. SendResult sendResult = producer.send(msg);
  39. // Synchronous message sending will succeed as long as no exception is thrown.
  40. if (sendResult != null) {
  41. System.out.println(new Date() + " Send mq message success. Topic is:" + msg.getTopic() + " msgId is: " + sendResult.getMessageId());
  42. }
  43. } catch (Exception e) {
  44. // If the message sending fails and a retry is needed, the message can be re-sent or persisted for compensated processing.
  45. System.out.println(new Date() + " Send mq message failed. Topic is:" + msg.getTopic());
  46. e.printStackTrace();
  47. }
  48. // The producer object will be destroyed before exiting the application.<br>
  49. // Note: It's ok if the producer object is not destroyed.
  50. producer.shutdown();
  51. }
  52. }

Subscribe to Delayed Messages

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

Thank you! We've received your feedback.