Delayed Messages

When messages are sent to a topic, their delivery to a consumer can be delayed. There are options for deliverAfter() or deliverAt(). In this example we will use deliverAt() to have a group of messages all delivered to a consumer at the same time. This could be an example of scheduling a marketing email to be sent at a future time.

Using project DelayedMessages, execute DelayedProducer and SNConsumer. The following code will truncate the current time to the earliest minute and add 60s. Therefore all messages in a given minute will be delivered to the consumer at the end of the minute.

Long deliveryTime = Instant.now().truncatedTo(ChronoUnit.MINUTES).toEpochMilli()+60000;

producer.newMessage().deliverAt(deliveryTime).value(message).sendAsync();

You should see the consumer receives ~60 messages at the end of the minute.

Note that Delayed Messages should only be used for Shared subscriptions.