简体   繁体   English

从 aws sqs 获取所有消息

[英]Get all message from aws sqs

I have a ScheduledEvent on my lamda function for every 24 hours and then inside function, I am calling SQS to get my messages.我每 24 小时在我的lamda function上有一个ScheduledEvent ,然后在 function 内,我打电话给SQS来获取我的消息。

export class EmailNotificationProcessor {
    public static async run(): Promise<void> {
        console.log('event');
        await this.getNotificationFromSqs();
    }

    private static async getNotificationFromSqs(): Promise<void> {
        const messagesToDelete: DeleteMessageBatchRequestEntryList = [];
        const messageRequest: ReceiveMessageRequest = {
            QueueUrl: process.env.DID_NOTIFICATION_SQS_QUEUE,
            MaxNumberOfMessages:10,
            WaitTimeSeconds:20
        }
        const { Messages }: ReceiveMessageResult = await receiveMessage(messageRequest);
        console.log('Messages', Messages);
        console.log('Total Messages ', Messages.length);
        if (Messages && Messages.length > 0) {
            for (const message of Messages) {
                console.log('body is ', message.Body);
                messagesToDelete.push({
                    Id: message.MessageId,
                    ReceiptHandle: message.ReceiptHandle,
                } as DeleteMessageBatchRequestEntry);
            }
        }
        await deleteMessages(messagesToDelete);
    }
}

I am expecting 1 to 30 messages inside my queue and want to process all messages before sending an email which consists of the content that I will parse from sqs body .我预计队列中有 1 到 30 条消息,并希望在发送 email 之前处理所有消息,其中包含我将从sqs body解析的内容。

My function for receiving messages我的 function 用于接收消息

export const receiveMessage = async (request: SQS.ReceiveMessageRequest): Promise<PromiseResult<SQS.ReceiveMessageResult, AWSError>> =>{
    console.log('inside receive');
    return sqs.receiveMessage(request).promise();
}

Now I am not able to receive all messages at once and only getting 3 messages or sometimes 1 message at a time.现在我无法一次收到所有消息,一次只能收到 3 条消息,有时甚至 1 条消息。

I know limit for API call is 10 in one single request but is there any way to wait and get all your message.我知道 API 调用的限制是一个请求中的 10 个,但是有什么方法可以等待并获取您的所有消息。

First of all, There is no configuration to get more then 10 messages from queue.首先,没有配置可以从队列中获取超过 10 条消息。

ReceiveMessage: Retrieves one or more messages (up to 10), from the specified queue ReceiveMessage:从指定队列中检索一条或多条消息(最多 10 条)

For your other problems: I think you are using Short poll ReceiveMessage call.If the number of messages in the queue is extremely small, you might not receive any messages in a particular ReceiveMessage response.对于您的其他问题:我认为您正在使用短轮询ReceiveMessage 调用。如果队列中的消息数量非常少,您可能不会在特定的 ReceiveMessage 响应中收到任何消息

Try Long Polling: Long polling helps reduce the cost of using Amazon SQS by eliminating the number of empty responses (when there are no messages available for a ReceiveMessage request) and false empty responses (when messages are available but aren't included in a response ).尝试长轮询:长轮询通过消除空响应(当没有可用于 ReceiveMessage 请求的消息时)和错误空响应(当消息可用但未包含在响应中时)的数量来帮助降低使用 Amazon SQS 的成本)。

Note* For getting more messages you need to wrap the call to SQS in a loop and keep requesting more messages until the queue is empty, which can lead you to get duplicate messages as well so try VisibilityTimeout in that problem.注意* 要获得更多消息,您需要将 SQS 调用包装在一个循环中,并继续请求更多消息,直到队列为空,这也会导致您获得重复消息,因此请在该问题中尝试 VisibilityTimeout。

Try VisibilityTimeout: The duration (in seconds) that the received messages are hidden from subsequent retrieve requests after being retrieved by a ReceiveMessage request.尝试 VisibilityTimeout:接收到的消息在被 ReceiveMessage 请求检索后对后续检索请求隐藏的持续时间(以秒为单位)。

Sample Wrap up SQS call code:示例总结 SQS 调用代码:

function getMessages(params, count = 0, callback) {
let allMessages = [];
sqs.receiveMessage(params, function (err, data) {
    if (err || (data && !data.Messages || data.Messages.length <= 0)) {
        
        if(++count >= config.SQSRetries ){
            return callback(null, allMessages);
        }

        return setTimeout(() => {
            return getMessages(params, count, callback);
        }, 500);
        
    } else if (++count !== config.SQSRetries ){
        allMessages.push(data);

        return setTimeout(() => {
            return getMessages(params, count, callback);
        }, 500);
    } else {
        allMessages.push(data);

    callback(null, allMessages);
    }
});

In config.SQSRetries , we have set the value according to our requirement but as your SQS have 1 to 30 messages, Then '7' will be good for you!config.SQSRetries中,我们根据我们的要求设置了值,但是由于您的 SQS 有 1 到 30 条消息,那么“7”对您有好处!

Links: RecieveMessage UserGuide链接: RecieveMessage 用户指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM