razrog
razrog

Reputation: 41

Amazon SQS Listener - Consuming message and setting attributes before failing the message

I've a fifo queue, and i'm using org.springframework.cloud.aws.messaging and here's what i'd like to do:

  1. Consume the message
  2. Try to handle it (inner logic)
  3. If handles fails - write new attribute on the message (without sending the message again to the queue)

I don't want to send the messages to a new queue (i need to keep the order of the messages). also, i don't want to use the deadletter queue for handling errors (same reason as above).

the reason i want to use message attributes is due to the fact that i need to implement in-house retry mechanism, meaning: when consuming the message i'll check the last_try_timestamp and if it's passed my validation then i'll try to handle it, else i'll throw an error. (I know that the message will continue to be consumed until the MaxRetention and i'm fine with it)

Is something like that possible?

 @SqsListener(value = "#{'${my.queue.fifo}'}", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
    public void deadLetterQueueListener(@Headers Map<String, String> headers, String message) throws Exception {
        log.info("consuming message");
        if(!this.handleMessage(message)){
            //Set message attributes (timestamp)
            throw new Exception("Failed to handle message");
        }
    }

Upvotes: 1

Views: 1862

Answers (1)

Maciej Walkowiak
Maciej Walkowiak

Reputation: 12932

As far as I know there is no way to do it purely with SQS. If you modify the message attribute you would need to resend this message to propagate this change to SQS.

It can be implemented on the application side by storing metadata like last_try_timestamp in an external datasource like a DynamoDB where you could map message id to any metadata you need.

Upvotes: 1

Related Questions