javascript - 如何在一段时间没有消息后停止消费
问题描述
在队列 5 分钟未处于活动状态后,我试图关闭我的连接。我有:
ch.consume(receivingQueue, async function (msg) {
if (msg !== null) {
console.log(msg.content.toString()));
}
});
我阅读了有关Channel.cancel()的信息,但我只是不太确定在哪里将其插入到这里的流程中,因为该过程只是坐下来等待新消息,而且我不确定从哪里得到consumerTag
它是不在msg
变量中。
解决方案
我没有测试过这个,但逻辑如下。setTimeout 设置为在每条消息 5 分钟后关闭连接。如果有新消息到达,旧的计时器会被新消息清除,并重新创建一个新的计时器。否则连接会在 5 分钟后关闭
const closeConnection = function () {
ch.close()
}
let timer = setTimeout(()=>{}, 0); //initial timer for cleartimeout Maybe refactor this?
ch.consume(receivingQueue, async function (msg) {
clearTimeout(timer);
timer = setTimeout(closeConnection, 300000)
if (msg !== null) {
console.log(msg.content.toString()));
}
});
推荐阅读
- unix - UNIX - 跨 ssh 测试文件并在原始主机上返回 True
- javascript - 解析嵌套的 JSON 响应 Javascript
- java - BJP5 练习 16.7:deleteBack — 帮助我理解解决方案
- azure - 如何在嵌套模板资源中使用 copyIndex?
- scala - 无法在 Akka Stream 中使用 GraphStage 类运行 SourceShape
- node.js - sessionStorage 中的 Jwt 验证在后端做出反应
- mysql - 无法进行合适的查询
- sql-server - 即使未执行该过程,列也不存在
- python - 如果用户输入 x 它会停止,我该怎么写
- ios - 转换为 HLS 时,Swift ReplayKit AVAssetWriter 视频音频不同步