3

我正在探索 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 的上述代码片段中遗漏了任何东西?

在此先感谢您的任何建议。

4

2 回答 2

1

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

请参阅AmazonSQSExtendedClient.deleteMessage 上的 Exeption以了解有关此问题的(迄今为止未解决的)问题,以及与 Core SDK 1.11.xx 不兼容的 SQS Extended Client Lib 以了解更一般的问题。

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

于 2017-04-21T13:21:28.747 回答
1

https://github.com/awslabs/amazon-sqs-java-extended-client-lib当从库 jar 中挑选(下载源并编译)时,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 ) 中提出了同样的问题。

于 2017-04-22T09:50:43.540 回答