gpt4 book ai didi

java - 如何从SQS消息中获取 `ApproximateReceiveCount`

转载 作者:行者123 更新时间:2023-12-01 20:15:18 25 4
gpt4 key购买 nike

我正在使用 Amazon SQS。我的目标是使用 Java SDK(v2.10.4、Java 11)从 ReceiveMessage API action 读取 ApproximateReceiveCount 属性。

我尝试了以下代码,但 message.attributes() 不包含所需的 key :

String getApproximateReceiveCount() {
var receiveMessageRequest = ReceiveMessageRequest.builder()
.queueUrl("https://sqs.eu-west-1.amazonaws.com/012345678910/my-example-queue")
.build();
var sqsClient = SqsClient.builder().endpointOverride(URI.create("http://localhost:4576")).build();
var response = sqsClient.receiveMessage(receiveMessageRequest);
var message = response.messages().get(0);
return message.attributes().get(MessageSystemAttributeName.APPROXIMATE_RECEIVE_COUNT);
}

如何在此映射中接收 MessageSystemAttributeName.APPROXIMATE_RECEIVE_COUNT 键的条目?

最佳答案

根据您链接的 ReceiveMessagethe documentation page,有一个名为 AttributeName.N 的参数,描述为

A list of attributes that need to be returned along with each message. These attributes include:

  • [...]
  • ApproximateReceiveCount – Returns the number of times a message has been received from the queue but not deleted.

因此,您需要在请求中请求该属性,以便它在响应中可用。为此,请使用 ReceiveMessageRequestBuilder.attributeNamesWithStrings() 方法,如下所示:

String getApproximateReceiveCount() {
var receiveMessageRequest = ReceiveMessageRequest.builder()
.queueUrl("https://sqs.eu-west-1.amazonaws.com/012345678910/my-example-queue")
.attributeNamesWithStrings(MessageSystemAttributeName.APPROXIMATE_RECEIVE_COUNT.toString())
.build();
var sqsClient = SqsClient.builder().endpointOverride(URI.create("http://localhost:4576")).build();
var response = sqsClient.receiveMessage(receiveMessageRequest);
var message = response.messages().get(0);
return message.attributes().get(MessageSystemAttributeName.APPROXIMATE_RECEIVE_COUNT);
}

请注意,有两个名称相似的方法,您不能使用它们:

  • .attributeNames() - 参数枚举未列出所需的键,
  • .messageAttributeNames() - 对应于与消息正文一起发送的属性。

关于java - 如何从SQS消息中获取 `ApproximateReceiveCount`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58955911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com