Siddhi Bansal
Siddhi Bansal

Reputation: 11

Unable to Retrieve MessageGroupId from SQS FIFO Queue Messages in Java SDK

[I am working with an AWS SQS FIFO queue and have sent several messages with different MessageGroupIds. However, when I try to retrieve the MessageGroupId from the received messages using the AWS SDK for Java, I am unable to get the value and it returns null.

Here is the code I’m using to receive the messages and attempt to extract the MessageGroupId:

private static final SqsClient sqsClient = SqsClient.builder()
            .region(Region.US_WEST_2)
            .credentialsProvider(DefaultCredentialsProvider.create())
            .build();
private static final String queueUrl = "fifo_url";
ReceiveMessageRequest receiveMessageRequest = ReceiveMessageRequest.builder()
                    .queueUrl(queueUrl)
                    .maxNumberOfMessages(10)
                    .waitTimeSeconds(20)
                    .build();
ReceiveMessageResponse response = sqsClient.receiveMessage(receiveMessageRequest);
List<Message> messages = response.messages();
for (Message message : messages) {
    Map<MessageSystemAttributeName, String> messageAttributes = message.attributes();
    String messageGroupId = messageAttributes.get("MessageGroupId");
}

In another attempt, I tried accessing the MessageGroupId from the message attributes like this:

private static String getMessageGroupId(Message message) {
        Map<String, MessageAttributeValue> attributes = message.messageAttributes();
        if (attributes != null && attributes.containsKey("MessageGroupId")) {
            return attributes.get("MessageGroupId").stringValue();
        }
        return "No Group ID";  
    }

](https://i.sstatic.net/yP8aBX0w.png)

What I have tried:

  1. I’ve verified that the queue is a FIFO queue.
  2. I have used both message.attributes() and message.messageAttributes() to get the MessageGroupId.
  3. I’ve confirmed that messages were sent with different MessageGroupIds.

Upvotes: 1

Views: 35

Answers (0)

Related Questions