All Products
Document Center

Send and receive delayed messages

Last Updated: Sep 13, 2019

Delayed messages are delivered to a consumer after a delay (3 seconds, for example) from when they are sent to the MQ broker. Send this type of message when a time window is required between the production and consumption of the message, or when tasks need to be triggered after a delay. Delayed messages are similar to delayed queues.

For more information about the concept behind delayed messages and for precautions for use of these messages, see Delayed messages.

Send delayed messages

The sample code for sending delayed messages 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 AccessKeyId you created in the Alibaba Cloud console for identity authentication.
  11. properties.put(PropertyKeyConst.AccessKey, "XXX");
  12. // The AccessKeySecret you created in the Alibaba Cloud console for identity authentication.
  13. properties.put(PropertyKeyConst.SecretKey, "XXX");
  14. // Set the TCP endpoint: Go to the **Instances** page in the MQ console, and view the endpoint in the **Endpoint Information** area.
  15. properties.put(PropertyKeyConst.NAMESRV_ADDR,
  16. "XXX ");
  17. Producer producer = ONSFactory.createProducer(properties);
  18. //Before sending a message, call the start() method once to start the producer.
  19. producer.start();
  20. Message msg = new Message( //
  21. // The topic you created in the console.
  22. "Topic",
  23. // The message tag, which is similar to a Gmail tag. It is used to sort messages, enabling the consumer to filter messages on the MQ broker based on the specified criteria.
  24. "tag",
  25. // The message body in any binary format. MQ does not process the message body. The producer and consumer must negotiate the consistent serialization and deserialization methods.
  26. "Hello MQ".getBytes());
  27. // Set a key service property representing the message, that is, the message key, and try to keep it globally unique.
  28. // A unique identifier enables you to query a message and resend it in the console if you fail to receive the message.
  29. // Note: Messages can still be sent and received if you do not set this attribute.
  30. msg.setKey("ORDERID_100");
  31. try {
  32. // The message delivery delay time, in milliseconds (ms). Messages are delivered after the specified delay (against the current time), for example, 3 seconds.
  33. long delayTime = System.currentTimeMillis() + 3000;
  34. // Set the message delivery time.
  35. msg.setStartDeliverTime(delayTime);
  36. SendResult sendResult = producer.send(msg);
  37. // Send the message in synchronous mode. The message is sent if 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. // The message failed to be sent and requires a retry. The system can resend the message or store message data persistently.
  43. System.out.println(new Date() + " Send mq message failed. Topic is:" + msg.getTopic());
  44. e.printStackTrace();
  45. }
  46. // Destroy the producer object before exiting from the application.<br>
  47. // Note: You can choose not to destroy the producer object.
  48. producer.shutdown();
  49. }
  50. }

Subscribe to delayed messages

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