如何为 FIFO SNS 传递 MessageGroupId

How can I pass MessageGroupId for FIFO SNS

我试过以下代码:

  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);
}

在 SQS 中传递那些 headers 工作正常,但在 SNS 中它不工作并给出错误:

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]

我必须更改 headers 还是有其他方法?

我通过使用不带 spring 的 sdk 找到了自己的方法,因为 SNS 的 FIFO 是新的,Spring 还没有针对这个问题实施任何解决方案,我找不到办法通过Spring给题目传递参数,这里是帮我解决的link:https://docs.aws.amazon.com/sns/latest/dg/fifo-topic-code-examples.html

下面是我的方法是如何完成的:

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);
    }
}