如何将 MessageGroupId 参数添加到消息中?

时间:2021-07-16 11:23:50

标签: java amazon-sqs spring-jms

我正在尝试使用 JmsTemplate 向 AwsSQS fifo 队列发送消息。

代码:

Message message = new Message().withBody("Hi");
jmsTemplate.convertAndSend(message);

当我运行上面的代码时出现错误

Caused by: com.amazonaws.services.sqs.model.AmazonSQSException: The request must contain the parameter MessageGroupId. (Service: AmazonSQS; Status Code: 400; Error Code: MissingParameter; Request ID: 94f6257b-0f2e-5e3d-9469-7c1066d45816; Proxy: null)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1819) ~[aws-java-sdk-core-1.12.24.jar:na]
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleServiceErrorResponse(AmazonHttpClient.java:1403) ~[aws-java-sdk-core-1.12.24.jar:na]
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1372) ~[aws-java-sdk-core-1.12.24.jar:na]
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1145) ~[aws-java-sdk-core-1.12.24.jar:na]
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:802) ~[aws-java-sdk-core-1.12.24.jar:na]
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:770) ~[aws-java-sdk-core-1.12.24.jar:na]
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:744) ~[aws-java-sdk-core-1.12.24.jar:na]
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:704) ~[aws-java-sdk-core-1.12.24.jar:na]
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:686) ~[aws-java-sdk-core-1.12.24.jar:na]
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:550) ~[aws-java-sdk-core-1.12.24.jar:na]
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:530) ~[aws-java-sdk-core-1.12.24.jar:na]
    at com.amazonaws.services.sqs.AmazonSQSClient.doInvoke(AmazonSQSClient.java:2271) ~[aws-java-sdk-sqs-1.12.24.jar:na]
    at com.amazonaws.services.sqs.AmazonSQSClient.invoke(AmazonSQSClient.java:2238) ~[aws-java-sdk-sqs-1.12.24.jar:na]
    at com.amazonaws.services.sqs.AmazonSQSClient.invoke(AmazonSQSClient.java:2227) ~[aws-java-sdk-sqs-1.12.24.jar:na]
    at com.amazonaws.services.sqs.AmazonSQSClient.executeSendMessage(AmazonSQSClient.java:1817) ~[aws-java-sdk-sqs-1.12.24.jar:na]
    at com.amazonaws.services.sqs.AmazonSQSClient.sendMessage(AmazonSQSClient.java:1786) ~[aws-java-sdk-sqs-1.12.24.jar:na]
    at com.amazon.sqs.javamessaging.AmazonSQSMessagingClientWrapper.sendMessage(AmazonSQSMessagingClientWrapper.java:202) ~[amazon-sqs-java-messaging-lib-1.0.8.jar:na]
    ... 16 common frames omitted

我试过像这样添加 MessageGroupId 但这也不起作用

Message message = new Message().withBody("Hi")
                .withAttributes(Map.of("MessageGroupId", "1"));
jmsTemplate.convertAndSend(message);

还有其他方法可以使用 JmsTemplate 将消息发送到 SQS fifo 队列吗?

1 个答案:

答案 0 :(得分:2)

aws documentation开始,您需要在标题或属性中设置JMSXGroupID

<块引用>

JMSXGroupID(FIFO 队列需要,标准队列不允许)

另外,请查看 key terms,您可能还需要重复数据删除 ID。