简体   繁体   English

如何在aws sqs节点js中接收消息

[英]how to receive message in aws sqs node js

i am implementing sqs in my Node js project.我正在我的 Node js 项目中实现 sqs。 what i am doing is sending msg in SQS and receiving it.我正在做的是在 SQS 中发送 msg 并接收它。 but when i receive it is just ResponseMetadata object但是当我收到它只是 ResponseMetadata 对象

   "ResponseMetadata": {
    "RequestId": "8659872b-10f0-57b6-9d57-d1852aba1a64"
    }

there's no Message object in response.没有消息对象作为响应。 what should i do?我该怎么办? i have many possibilities like changing param values etc but nothing works.我有很多可能性,例如更改参数值等,但没有任何效果。

my code我的代码

   const AWS = require('aws-sdk');
   AWS.config.update({ accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, region: 'eu-west-1' })
   const sqs = new AWS.SQS({apiVersion: '2012-11-05'});

   onst queueUrl = "https://eu-west-1.queue.amazonaws.com/******/test-queue";

send msg发送信息

     let params = {
            MessageBody: 'Hello world!',
            MessageAttributes: {
                "Title": {
                    DataType: "String",
                    StringValue: "The Whistler"
                },
                "Author": {
                    DataType: "String",
                    StringValue: "John Grisham"
                },
                "WeeksOn": {
                    DataType: "Number",
                    StringValue: "6"
                }
            },
            QueueUrl: queueUrl,
            DelaySeconds: 0
        };

        sqs.sendMessage(params, function (err, data) {
            if (err) {
                res.send(err);
            } else {
                res.send(data);
            }
        });

response is回应是

     "ResponseMetadata": {
    "RequestId": "da3af650-2642-5460-86b7-a0fe1f9ced6f"
},
"MD5OfMessageBody": "86fb269d190d2c85f6e0468ceca42a20",
"MD5OfMessageAttributes": "1864106991a54cca8b8c732a1841833a",
"MessageId": "13f228b0-7df1-4a9e-bc2b-48535725955e"

receive msg接收消息

       sqs.getQueueUrl('queue-name', function(err, data) {
            if (err) {
                console.log("Error", err);
            } else {
                let params = {
                    AttributeNames: [
                        "SentTimestamp"
                    ],
                    MaxNumberOfMessages: 10,
                    VisibilityTimeout: 20,
                    MessageAttributeNames: ["All"],
                    QueueUrl: data.QueueUrl,
                    WaitTimeSeconds: 0
                };

                sqs.receiveMessage(params, function (err, data) {
                    if (err) {
                        res.send(err);
                    } else {
                        res.send(data);
                    }
                });
            }
        });

can someone help?有人可以帮忙吗? is there anything i am missing?有什么我想念的吗? thanks is advance谢谢是提前

The getQueueUrl call is not correct. getQueueUrl调用不正确。 You need to wrap the queue name in an object like this您需要将队列名称包装在这样的对象中

sqs.getQueueUrl({"QueueName": "queue name"}, function (err, data) {

also, it is worth using promise version instead of callback.此外,值得使用承诺版本而不是回调。

const data = sqs.getQueueUrl({"QueueName": "queue name"}).promise();

// Similary receiveMessage

You need to set WaitTimeSeconds: 20 to make a long polling您需要设置WaitTimeSeconds: 20进行长轮询

sqs.getQueueUrl('queue-name', function (err, data) {
    if (err) {
        console.log("Error", err);
    } else {
        let params = {
            AttributeNames: [
                "SentTimestamp"
            ],
            MaxNumberOfMessages: 10,
            VisibilityTimeout: 20,
            MessageAttributeNames: ["All"],
            QueueUrl: data.QueueUrl,
            WaitTimeSeconds: 20
        };

        sqs.receiveMessage(params, function (err, data) {
            if (err) {
                res.send(err);
            } else {
                res.send(data);
            }
        });
    }
});

If you have more than 1000 messages in your queue, you need to set WaitTimeSeconds larger than 0. Default is 0.如果队列中的消息超过 1000 条,则需要将WaitTimeSeconds设置为大于 0。默认值为 0。

See the difference between short polling and long polling here:在此处查看短轮询和长轮询之间的区别:

https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-short-and-long-polling.html#sqs-long-polling https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-short-and-long-polling.html#sqs-long-polling

Short polling may return empty even you have messages in the queue.即使队列中有消息,短轮询也可能返回空。

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

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