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

时间:2017-11-20 12:38:32

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

我正忙着尝试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,请告知我们:)

1 个答案:

答案 0 :(得分:2)

好吧,看起来我无法正确阅读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.