我正在尝试SQS。据我了解,可见性超时使该可见性超时的时间对其他使用者不可用。但是,以我的经验来看,事实并非如此。可见性超时使该队列中的所有消息都不可用。

我有确认这一点的代码:

        SendMessageRequest messageRequest = new SendMessageRequest()
            .withMessageBody("first one")
            .withMessageDeduplicationId(UUID.randomUUID().toString())
            .withQueueUrl(queueAddress)
            .withMessageGroupId("test1");

    sqs.sendMessage(messageRequest);

    messageRequest = new SendMessageRequest()
            .withMessageBody("second one")
            .withMessageDeduplicationId(UUID.randomUUID().toString())
            .withQueueUrl(queueAddress)
            .withMessageGroupId("test1");

    sqs.sendMessage(messageRequest);

    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueAddress);

    new Thread(() -> {
        System.out.println("t1");
        List<Message> messages = sqs.receiveMessage(receiveMessageRequest
                .withMaxNumberOfMessages(1)
                .withMessageAttributeNames("All")
                .withVisibilityTimeout(5)
                .withWaitTimeSeconds(1)
        ).getMessages();
        System.out.println(messages.get(0).getBody());
    }).start();

    new Thread(() -> {
        System.out.println("t2");
        List<Message> messages = sqs.receiveMessage(receiveMessageRequest
                .withMaxNumberOfMessages(1)
                .withMessageAttributeNames("All")
                .withVisibilityTimeout(5)
                .withWaitTimeSeconds(1)
        ).getMessages();
        System.out.println(messages.get(0).getBody());
    }).start();


发生的是第二个线程抛出IndexOutOfBoundsException。这是因为没有可用的消息。这确认可见性超时会影响整个队列,而不仅是消息。

不幸的是,这似乎与亚马逊的文件不符。

如果有人对为什么会这样有任何见解,或者如果我滥用SQS SDK,请告诉我:)

最佳答案

好吧,看来我无法正确阅读documentation)::

When you receive a message with a message group ID, no more messages for the same message group ID are returned unless you delete the message or it becomes visible.

10-06 04:56