使用Node轮询Amazon SQS队列的最有效方法

时间:2020-12-29 17:38:02

My question is short, but I think is interesting:

我的问题很简短,但我觉得很有意思:

I've a queue from Amazon SQS service, and I'm polling the queue every second. When there's a message I process the message and after processing, go back to polling the queue.

我有一个来自Amazon SQS服务的队列,我每秒都在轮询队列。当有消息我处理消息并在处理之后,返回轮询队列。

Is there a better way for this?, some sort of trigger? or which approach will be the best in your opinion, and why.

有没有更好的方法呢?某种触发器?或者您认为哪种方法最好,以及为什么。

Thanks!

3 个解决方案

#1


5  

A useful and easily to use library for consuming messages from SQS is sqs-consumer

用于消费来自SQS的消息的有用且易于使用的库是sqs-consumer

const Consumer = require('sqs-consumer');

const app = Consumer.create({
  queueUrl: 'https://sqs.eu-west-1.amazonaws.com/account-id/queue-name',
  handleMessage: (message, done) => {
    console.log('Processing message: ', message);
    done();
  }
});

app.on('error', (err) => {
  console.log(err.message);
});

app.start();

It's well documented if you need more information. You can find the docs at: https://github.com/bbc/sqs-consumer

如果您需要更多信息,请将其记录在案。您可以在以下网址找到文档:https://github.com/bbc/sqs-consumer

#2


3  

yes there is: http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html

是的,有:http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html

you can configure the SQS queues to have a "receive message wait time" and do long polling.

您可以将SQS队列配置为具有“接收消息等待时间”并进行长轮询。

so you can set it to say 10 seconds, and the call will come back only if you have a message or after the 10 sec timeout expires. you can continuously poll the queue in this scenario.

因此您可以将其设置为10秒,只有在您有消息或10秒超时到期后,呼叫才会返回。您可以在此方案中连续轮询队列。

#3


1  

As mentioned by Mircea, long polling is one option.

如Mircea所述,长期投票是一种选择。

When you ask for a 'trigger', I believe you are looking for something other than continuously polling SQS on your own. If that is the case, I'd suggest you look at AWS Lambda. It allows you to put code in the cloud, which automatically gets triggered on your configured events, such as SNS event, a file pushed to S3 etc.

当你要求“触发器”时,我相信你正在寻找除了自己连续轮询SQS之外的其他东西。如果是这种情况,我建议您查看AWS Lambda。它允许您将代码放入云中,这会自动在您配置的事件上触发,例如SNS事件,推送到S3的文件等。

http://aws.amazon.com/lambda/

#1


5  

A useful and easily to use library for consuming messages from SQS is sqs-consumer

用于消费来自SQS的消息的有用且易于使用的库是sqs-consumer

const Consumer = require('sqs-consumer');

const app = Consumer.create({
  queueUrl: 'https://sqs.eu-west-1.amazonaws.com/account-id/queue-name',
  handleMessage: (message, done) => {
    console.log('Processing message: ', message);
    done();
  }
});

app.on('error', (err) => {
  console.log(err.message);
});

app.start();

It's well documented if you need more information. You can find the docs at: https://github.com/bbc/sqs-consumer

如果您需要更多信息,请将其记录在案。您可以在以下网址找到文档:https://github.com/bbc/sqs-consumer

#2


3  

yes there is: http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html

是的,有:http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html

you can configure the SQS queues to have a "receive message wait time" and do long polling.

您可以将SQS队列配置为具有“接收消息等待时间”并进行长轮询。

so you can set it to say 10 seconds, and the call will come back only if you have a message or after the 10 sec timeout expires. you can continuously poll the queue in this scenario.

因此您可以将其设置为10秒,只有在您有消息或10秒超时到期后,呼叫才会返回。您可以在此方案中连续轮询队列。

#3


1  

As mentioned by Mircea, long polling is one option.

如Mircea所述,长期投票是一种选择。

When you ask for a 'trigger', I believe you are looking for something other than continuously polling SQS on your own. If that is the case, I'd suggest you look at AWS Lambda. It allows you to put code in the cloud, which automatically gets triggered on your configured events, such as SNS event, a file pushed to S3 etc.

当你要求“触发器”时,我相信你正在寻找除了自己连续轮询SQS之外的其他东西。如果是这种情况,我建议您查看AWS Lambda。它允许您将代码放入云中,这会自动在您配置的事件上触发,例如SNS事件,推送到S3的文件等。

http://aws.amazon.com/lambda/