我正在探索 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 的上述代码片段中遗漏了任何东西?
在此先感谢您的任何建议。