使用Java sdk删除AWS SQS消息

时间:2017-04-21 12:28:13

标签: java amazon-web-services amazon-sqs aws-java-sdk

我正在探索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错过了任何内容吗?

提前感谢任何建议。

2 个答案:

答案 0 :(得分:1)

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

请参阅Exeption on AmazonSQSExtendedClient.deleteMessage了解(目前尚未解决的)此问题,以及SQS Extended Client Lib not compatible with Core SDK 1.11.xx更常见的问题。

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

答案 1 :(得分:1)

https://github.com/awslabs/amazon-sqs-java-extended-client-lib(下载源代码并编译)库文件夹中成功删除SQS消息。

然而,当从maven存储库中挑选时,如下所示 它在最初发布的问题中提到了问题。

<dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>amazon-sqs-java-extended-client-lib</artifactId>
        <version>1.0.0</version>
</dependency>

我在github(https://github.com/awslabs/amazon-sqs-java-extended-client-lib/issues/18)中提出了同样的问题。