edit-icon download-icon

Send and receive delayed messages

Last Updated: Oct 09, 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.

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

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.