我正在探索AWS SQS服務。當試圖從使用java sdk的隊列中刪除消息時,我正面臨着一些問題。使用Java sdk刪除AWS SQS消息
隊列在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缺少任何東西?
在此先感謝您的任何建議。
謝謝Henrik的建議。這個問題似乎是從maven倉庫中挑選出來的。 – Vignesh