如果收到消息,AWS SQS Long poll是否会提前返回?

如何解决如果收到消息,AWS SQS Long poll是否会提前返回?

如果我使用SQS长轮询并将“ WaitTimeSeconds”设置为10秒,将“ MaxNumberOfMessages”设置为1,并且在0.1秒后将单个消息传递到队列,则对sqs.receiveMessage()的调用将返回紧接在那一点,还是应该等到“ WaitTimeSeconds”的10秒过去才返回?

在我的测试中,直到经过“ WaitTimeSeconds”的整个持续时间之后,才返回对sqs.receiveMessage()的调用。

代码如下:

// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");

const fmReqQ = "https://sqs.ap-southeast-2.amazonaws.com/myactid/fmReqQ";
const fmRspQ = "https://sqs.ap-southeast-2.amazonaws.com/myactid/fmRspQ";

const SydneyRegion = "ap-southeast-2";

var credentials = new AWS.SharedIniFileCredentials({ profile: "myprofile" });
AWS.config.credentials = credentials;

// Set the region
AWS.config.update({ region: SydneyRegion });

// Create an SQS service object
var sqs = new AWS.SQS({ apiVersion: "2012-11-05" });

async function sendRequest() {
  var sendParams = {
    MessageBody: "Information of 12/11/2016.",QueueUrl: fmReqQ,};

  try {
    data = await sqs.sendMessage(sendParams).promise();
    console.log("Success,request MessageId: ",data.MessageId);
  } catch (err) {
    console.log("Error",err);
  }
}

async function doModelling() {
  console.time("modelling");
  await sendRequest();
  await receiveResponse();
  console.timeEnd("modelling");
}

async function receiveResponse() {
  var receiveParams = {
    AttributeNames: ["SentTimestamp"],MaxNumberOfMessages: 1,MessageAttributeNames: ["All"],QueueUrl: fmRspQ,WaitTimeSeconds: 1,};

  let data = null;
  try {
    data = await sqs.receiveMessage(receiveParams).promise();
    console.log("Success,response MessageId: ",data);
  } catch (err) {
    console.log("Error",err);
  }
}

doModelling();

当我设置“ WaitTimeSeconds:3”时,我得到输出:

Success,request MessageId:  e5079c2a-050f-4681-aa8c-77b05ac7da7f
Success,response MessageId:  {
  ResponseMetadata: { RequestId: '1b4d6a6b-eaa2-59ea-a2c3-3d9b6fadbb3f' }
}
modelling: 3.268s

当我设置“ WaitTimeSeconds:10”时,我得到输出:

Success,request MessageId:  bbf0a429-b2f7-46f2-b9dd-38833b0c462a
Success,response MessageId:  {
  ResponseMetadata: { RequestId: '64bded2d-5398-5ca2-86f8-baddd6d4300a' }
}
modelling: 10.324s

注意经过的持续时间如何与WaitTimeSeconds相匹配。

它从reading about AWS SQS long polling开始说,它长期轮询“尽快返回可用消息”。

我似乎并没有在消息“可用后立即看到”,而是注意到sqs.receiveMessage()调用总是采用WaitTimeSeconds中设置的持续时间。

如您在示例代码中所见,我已将MaxNumberOfMessage设置为1。

解决方法

ReceiveMessage()长轮询一起使用将在队列中至少有一条消息时返回

我不是Node用户,但是这是我测试的方式:

  • 创建了Amazon SQS队列
  • 在一个窗口中,我跑了:
aws sqs receive-message --queue-url https://sqs.ap-southeast-2.amazonaws.com/123/foo --visibility-timeout 1 --wait-time-seconds 10
  • 然后,在另一个窗口中,我跑了:
aws sqs send-message --queue-url https://sqs.ap-southeast-2.amazonaws.com/123/foo --message-body bar

在我使用receive-message命令之后,send-message命令很快返回。

您的测试可能会受到“接收到”但标记为“不可见”的消息的影响,并且对于以后的测试而言,它们仍然不可见,因为您的代码未调用DeleteMessage()。为了避免这种情况,我特别声明了--visibility-timeout 1,它使消息立即重新出现在队列中以进行下一次测试。

请求的消息数(--max-number-of-messages)不会影响此结果。至少有一条消息可用时,它将返回。

,

我将“ WaitTimeSeconds”设置为0,似乎得到了现在的行为:

Success,request MessageId:  9f286e22-1a08-4532-88ba-06c88be3dbc3
Success,response MessageId:  {
  ResponseMetadata: { RequestId: '5c264e27-0788-5772-a990-19d78c8b2565' }
}
modelling: 307.884ms
,

我为“ WaitTimeSeconds”指定的值确定了sqs.receiveMessage()调用的持续时间,因为队列中没有消息,因此sqs.receiveMessage()调用将等待“ WaitTimeSeconds”指定的持续时间。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-