Spring-cloud-aws: AWS sqs sending messages with delay seconds

Created on 30 Sep 2015  路  6Comments  路  Source: spring-cloud/spring-cloud-aws

Is there a way to set delay seconds while sending messages to SQS.

I am using the below code to send the domain object.
this.queueMessagingTemplate.convertAndSend(queueURL, payload);

sqs feature

Most helpful comment

You need to use the SqsMessageHeaders.SQS_DELAY_HEADER header.

Here's a simplest way to do it:

sendMessageDelayed( Object payload, String queueName, long delaySeconds) {
Map headers = Stream.of(
new AbstractMap.SimpleEntry<>(MessageHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE),
new AbstractMap.SimpleEntry<>(SqsMessageHeaders.SQS_DELAY_HEADER, (int) delaySeconds))
.collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));

    this.queueMessagingTemplate.convertAndSend(queueName,payload, headers);
}

All 6 comments

Hi @unniksk the interface org.springframework.messaging.core.DestinationResolvingMessageSendingOperations currently does not support sending messages with delayseconds. We might consider adding an additional method to org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate in order to allow setting the delayseconds.
Another option might be a specialized message header, we will discuss internally and come back with an proposal.

I think this one is now fixed at least in 1.1.3

@demboos - can you clarify "fixed"? II'm on 1.2.2 and see nothing in the APIs that indicates delaying individual SQS messages is possible.

You need to use the SqsMessageHeaders.SQS_DELAY_HEADER header.

Here's a simplest way to do it:

sendMessageDelayed( Object payload, String queueName, long delaySeconds) {
Map headers = Stream.of(
new AbstractMap.SimpleEntry<>(MessageHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE),
new AbstractMap.SimpleEntry<>(SqsMessageHeaders.SQS_DELAY_HEADER, (int) delaySeconds))
.collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));

    this.queueMessagingTemplate.convertAndSend(queueName,payload, headers);
}

very much obliged @demboos

Thanks @demboos for providing example.

Was this page helpful?
0 / 5 - 0 ratings