gpt4 book ai didi

java - 使用 Java sdk 删除 AWS SQS 消息

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:11:46 26 4
gpt4 key购买 nike

我正在探索 AWS SQS 服务。当尝试使用 java sdk 从队列中删除消息时,我遇到了一些问题。

在 SQS 中创建了一个队列,它有三个消息。该队列由 AWS S3 存储支持,用于处理大型消息。

这里是通过多次轮询接收消息的方法。

接收 SQS 消息:

private static void receiveMessage(String queueUrl) {
// Receive messages
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl).withMaxNumberOfMessages(10)
.withWaitTimeSeconds(10).withVisibilityTimeout(100);
List<Message> messages = sqsExtended.receiveMessage(receiveMessageRequest).getMessages();
System.out.println("Number of mesasges : First poll : " + messages.size());
while (messages.size() > 0) {
messages = pollForMessages(messages, receiveMessageRequest, queueUrl);
System.out.println("Messages in next poll : " + messages.size());
}
if (messages.size() > 0) {
System.out.println("displaying last set of messages ");
for (int i = 0; i < messages.size(); i++) {
System.out.println("\nMessage received:");
System.out.println(" ID: " + messages.get(i).getMessageId());
System.out.println(" Receipt handle: " + messages.get(i).getReceiptHandle());
System.out.println("approx. num of messages : "
+ messages.get(i).getAttributes().get("ApproximateNumberOfMessages"));
System.out.println(" Message body (first sentence): " + messages.get(i).getBody().substring(0, 56));
// deleteMessage(messages.get(i), queueUrl);
}
deleteMessage(messages, queueUrl);
}
}

轮询消息的方法:

private static List<Message> pollForMessages(List<Message> messages, ReceiveMessageRequest receiveMessageRequest,
String queueUrl) {
// display the received messages
for (int i = 0; i < messages.size(); i++) {
System.out.println("\nMessage received:");
System.out.println(" ID: " + messages.get(i).getMessageId());
System.out.println(" Receipt handle: " + messages.get(i).getReceiptHandle());
System.out.println(
"approx. num of messages : " + messages.get(i).getAttributes().get("ApproximateNumberOfMessages"));
System.out.println(" Message body (first sentence): " + messages.get(i).getBody().substring(0, 56));
// deleteMessage(messages.get(i), queueUrl);
}
deleteMessage(messages, queueUrl);
messages = sqsExtended.receiveMessage(receiveMessageRequest).getMessages();
return messages;
}

删除 SQS 消息:

private static void deleteMessage(List<Message> messages, String queueUrl) {
// Delete the messages
for (Message message : messages) {
DeleteMessageRequest dmr = new DeleteMessageRequest();
dmr.withQueueUrl(queueUrl).withReceiptHandle(message.getReceiptHandle());
sqsExtended.deleteMessage(dmr);
System.out.println("Deleted the message with Id : " + message.getMessageId());
}

}

当调用deleteMessage方法时,程序遇到以下异常:

java.lang.AbstractMethodError: com.amazon.sqs.javamessaging.AmazonSQSExtendedClient.deleteMessage(Lcom/

日志消息:

Apr 21, 2017 5:32:56 PM com.amazon.sqs.javamessaging.ExtendedClientConfiguration setLargePayloadSupportEnabled
INFO: Large-payload support enabled.
Number of mesasges : First poll : 1

Message received:
ID: f4f00368-b308-4763-84cf-8e33e1931fed
Receipt handle: AQEBwhLDOIMobJazre1KoSYoMlHajHD1A6j7gZClk1h4FYWAmFd0p6wIFU69rSuhhVdQef+qF6E6RQIQLWaLqS2r0/Kfw3rUrmppIkTcowsVRW36uJ3p4UqnNjZu0cxncNMw78kHfHRej0gwH0gvvMedZXvEimoXUYZq1+K3Gz35etKsUR9EPKuroYaHZ6Aghdu0Osb4cQu+iLxlBrANhFl63762V7EDEEjSeULyQm52wS/OQq7JiktRWFgyGkp/41tHlN3ARB0v2MM3wXaoZMBt8bAyGURXb+Nx+5e3UoSI5J8pQccSwFnk70N1lV7XUswBTk4549uJ0FPlEMoFef6cxeFi1nxEXkfwIJQ9e93wMJUGLnhsx7rNWhS9w6UCVv4yZoARBsMrQbg287LkACLLnw==
approx. num of messages : null
Message body (first sentence): {
"Type" : "Notification",
"MessageId" : "fc1b19bc-6
Exception in thread "main" java.lang.AbstractMethodError: com.amazon.sqs.javamessaging.AmazonSQSExtendedClient.deleteMessage(Lcom/amazonaws/services/sqs/model/DeleteMessageRequest;)Lcom/amazonaws/services/sqs/model/DeleteMessageResult;
at org.nfjs.aws.sqs.SQSExtendedClient.deleteMessage(SQSExtendedClient.java:149)
at org.nfjs.aws.sqs.SQSExtendedClient.pollForMessages(SQSExtendedClient.java:139)
at org.nfjs.aws.sqs.SQSExtendedClient.receiveMessage(SQSExtendedClient.java:108)
at org.nfjs.aws.sqs.SQSExtendedClient.main(SQSExtendedClient.java:63)

我无法准确找出异常的原因。我是否遗漏了上述使用 java sdk 的代码片段中的任何内容?

提前感谢您的任何建议。

最佳答案

我没有使用 Amazon SQS Extended Client Library for Java我自己,所以我无法真正判断细节,但看起来它与 AWS Java SDK 不同步。

参见 Exeption on AmazonSQSExtendedClient.deleteMessage对于一个(到目前为止 Unresolved )问题,以及 SQS Extended Client Lib not compatible with Core SDK 1.11.xx对于更一般的。

后者指向最终有用的 support aws-sdk-java 1.11.8拉取请求,尽管那个请求现在也已经过时了。

关于java - 使用 Java sdk 删除 AWS SQS 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43542746/

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