I have 2 issues regarding Spring AWS-SQS sdk, (Or maybe I am doing it wrong).
First is that I used CLI earlier and I managed to get the message Id of the sent message example:
aws sqs send-message --queue-url https://sqs.us-west-2.amazonaws.com/testqueue --message-body hooray
'{
"MD5OfMessageBody": "d3101ad",
"MessageId": "jdhj-933"
}
Now I tried with spring-cloud-starter-aws-messaging
and I setup a Queue Messaging template like this
private final QueueMessagingTemplate queueMessagingTemplate;
public SqsQueueService(@Qualifier("amazonSQSAsync") final AmazonSQSAsync amazonSQS) {
this.queueMessagingTemplate = new QueueMessagingTemplate(amazonSQS);
}
public void sendMessage(String queueName, String queueMessage) {
Map<String, Object> headers = new HashMap<>();
queueMessagingTemplate.convertAndSend(queueName, queueMessage, headers);
}
I can seem to get the message Id of the sent message using queueMessagingTemplate.convertAndSend(queueName, queueMessage, headers);
I need the messageId to fulfil some business logic.
The second issue is my listener can receive messages however the messageID is null as well;
@Async
@SqsListener(value = "${notification.sqs-queue-url}", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
public void listen(Acknowledgment acknowledgment, String message, String messageId) {
//messageId is equal to message here. which is wrong for me
}
The message is always equal to messageId, which is confusing, Any advice on where I maybe going wrong?
I changed the listner method signature to
@Async
@SqsListener(value = "${queue-url}", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
public void listen(Acknowledgment acknowledgment, String message, @Headers MessageHeaders headers) throws ExecutionException, InterruptedException {
String messageId = (String) headers.get("MessageId");
acknowledgment.acknowledge().get();
}
Then extracted the messageId from the headers map