SQS receiveMessage在第一条消息后停止工作

时间:2016-04-14 15:07:52

标签: node.js amazon-web-services amazon-sqs

我是SQS和nodejs的新手。我希望在队列不空的时候接收消息,但是在收到第一条消息后它停止工作。我的代码仅在启动服务器时读取第一条消息。我怎样才能一直阅读消息?

sqs.receiveMessage({
    QueueUrl: queueUri,
    WaitTimeSeconds: 3,
    MaxNumberOfMessages: 1
 }, function(err, data) {
   // If there are any messages to get
   if (data.Messages) {
      // Get the first message (should be the only one since we said to only get one above)
      var message = data.Messages[0],
          body = JSON.parse(message.Body);
      var bucketName = body.Records[0].s3.bucket.name;
      var fullName = body.Records[0].s3.object.key;
      var fileName = Path.basename(fullName);
      var folderName = Path.dirname(fullName);
      var _45px = {
        width: 45,
        dstnKey: fileName,
        destinationPath: "thumbnails"
      };

      removeFromQueue(message);
 }
 });

1 个答案:

答案 0 :(得分:1)

该代码将读取一条消息并停止。如果要继续阅读消息,则需要执行某种循环以继续调用sqs.receiveMessage()。当然,这可能很困难,因为它是异步调用。我会调查Async library