Retrieve multiple messages from SQS

后端 未结 8 1973
逝去的感伤
逝去的感伤 2020-12-10 00:55

I have multiple messages in SQS. The following code always returns only one, even if there are dozens visible (not in flight). setMaxNumberOfMessages I th

8条回答
  •  悲&欢浪女
    2020-12-10 01:09

    Here's a workaround, you can call receiveMessageFromSQS method asynchronously.

       bulkReceiveFromSQS (queueUrl, totalMessages, asyncLimit, batchSize, visibilityTimeout, waitTime, callback) {
        batchSize = Math.min(batchSize, 10);
    
        let self = this,
            noOfIterations = Math.ceil(totalMessages / batchSize);
    
        async.timesLimit(noOfIterations, asyncLimit, function(n, next) {
            self.receiveMessageFromSQS(queueUrl, batchSize, visibilityTimeout, waitTime,
                function(err, result) {
                    if (err) {
                        return next(err);
                    }
    
                    return next(null, _.get(result, 'Messages'));
                });
        }, function (err, listOfMessages) {
            if (err) {
                return callback(err);
            }
            listOfMessages = _.flatten(listOfMessages).filter(Boolean);
    
            return callback(null, listOfMessages);
        });
    }
    

    It will return you an array with a given number of messages

提交回复
热议问题