SQS 可见性超时锁定整个队列,而不仅仅是特定的消息

SQS visibility timeout locks out the entire queue, instead of just that particular message

我正忙于试验 SQS。据我了解,可见性超时会使其他消费者在该可见性超时期间无法使用消息。不过,根据我的经验,情况似乎并非如此。可见性超时似乎使该队列中的 all 消息不可用。

我有验证这一点的代码:

        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.