amazon-snsspring-messaging

How can I pass MessageGroupId for FIFO SNS


I've tried the following code:

  private final NotificationMessagingTemplate notificationMessagingTemplate;

public void send(final T payload, final Object groupId) {
    final ImmutableMap<String, Object> headers = ImmutableMap.of("message-group-id", groupId.toString(),
            "message-deduplication-id", UUID.randomUUID().toString());
    notificationMessagingTemplate.convertAndSend(topicName, payload, headers);
}

Passing those headers in SQS works fine but in SNS it's not working and it gives the error:

Caused by: com.amazonaws.services.sns.model.InvalidParameterException: Invalid parameter: The MessageGroupId parameter is required for FIFO topics (Service: AmazonSNS; Status Code: 400; Error Code: InvalidParameter; Request ID: 1aa83814-abc8-56e9-ae15-619723438fe9; Proxy: null)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1819) ~[aws-java-sdk-core-1.11.933.jar:na]

Do I have to change the headers or there is another way arround?


Solution

  • I found a way around myself by using sdk without spring, because FIFO for SNS is new, Spring did not implemented any solution for this problem yet and I could not find a way to pass parameters to the topic through Spring, here is the link that help me solve it: https://docs.aws.amazon.com/sns/latest/dg/fifo-topic-code-examples.html

    And here is how my method was done:

    private final String topicArn;
    private final AmazonSNS amazonSNS;
    private final ObjectMapper objectMapper;
    
    public void send(final T payload, final Object groupId) {
    
        try {
            amazonSNS.publish(new PublishRequest()
                    .withTopicArn(topicArn)
                    .withMessageDeduplicationId(UUID.randomUUID().toString())
                    .withMessage(objectMapper.writeValueAsString(payload))
                    .withMessageGroupId(groupId.toString()));
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }