`
sillycat
  • 浏览: 2564526 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Connection and Timeout in RabbitMQ NodeJS

 
阅读更多
Connection and Timeout in RabbitMQ NodeJS

In our simple script, we are write a sendToQueue.js in NodeJS to send out JSON file content to RabbitMQ. I think we just follow the example in RabbitMQ and do it like this
// Usage: node sendToQueue.js meeting-to-update-stage.json
{
const amqp = require('amqplib/callback_api');
const fs = require('fs');
const durable = process.env.MQ_DURABLE || true;
const exchangeType = 'topic';
const exchange = ‘xxxxxx_events';
const amqpHost = 'amqp://username:password@domainurl.rmq.cloudamqp.com/name?heartbeat=30';
const objectFile = process.argv[2];
let objects = fs.readFileSync(objectFile).toString().split("\n");
objects.forEach(msg => {
    console.log(`parsed from file - ${JSON.stringify(msg)}\n`);
});
try {
    console.log(`sending messages to rabbit... `);
    amqp.connect(amqpHost, function (err, conn) {
        conn.createChannel(function (err, ch) {
            ch.assertExchange(exchange, exchangeType, { durable: durable });
            objects.forEach(msg => {
                if(msg !== null && msg !== '') {
                    ch.publish(exchange, '', Buffer.from(msg));
                }
            });
    });
    setTimeout(function () { conn.close(); process.exit(0) }, 500);});
    } catch (error) {
        console.log(`error: ${error}`);
    }
}
The issue is setTimeout(xxx, 500); in the example, it only send out 1 message, wait 500 ms is good enough.
But in my case, it is a loop with thousands, hundreds messages. It is working when the number is 100, but not working when the number is 1000.
Here is the promise example I think, it fixed the timeout connection issue.
{
const amqp = require('amqplib');
const fs = require('fs');
const durable = process.env.MQ_DURABLE || true;
const exchangeType = 'topic';
const exchange = ‘xxxxxx_events';
const amqpHost = 'amqp://username:password@host.rmq.cloudamqp.com/name?heartbeat=30';
const objectFile = process.argv[2];
let objects = fs.readFileSync(objectFile).toString().split("\n");
(async () => {
    try {
        const conn = await amqp.connect(amqpHost);
        const ch = await conn.createChannel();
        await ch.assertExchange(exchange, exchangeType, { durable: durable });
        console.log("Loading csv file contents, number of records = " + objects.length);
        await objects.map(async (msg) => {
            if(msg !== null && msg !== '') {
                try {
                    console.log(`msg = ${JSON.stringify(msg)}\n`)
                    await ch.publish(exchange, '', Buffer.from(msg));
                    return msg;
                } catch (error) {
                    console.log("**** ERROR processing msg: " + error);
                }
            }
        });
        await ch.close();
        await conn.close();
    } catch (error) {
        console.log(error);
    }
})()
}
Then it works perfectly.
References:
https://github.com/squaremo/amqp.node/issues/404
https://zhuanlan.zhihu.com/p/28276010
https://www.cnblogs.com/duhuo/p/6306535.html
https://stackoverflow.com/questions/41212249/node-wait-for-loop-to-finish
https://stackoverflow.com/questions/18574298/wait-until-node-amqp-has-sent-a-message
分享到:
评论

相关推荐

    rabbitmq-nodejs-demos:带有AMQP协议的RabbitMQ DemosTutorials。 [NodeJS]

    使用NodeJS的RabbitMQ演示/教程该存储库包含带有amqplib node-js库的RabbitMQ演示/教程。 RabbitMQ使用多种协议。 但是在本演示/教程中,我使用了AMQP 0-9-1 ,这是一种开放的通用消息传递协议。要求入门想运行我的...

    node-js-rabbitmq:NodeJS中的RabbitMQ实现

    NodeJS中的RabbitMQ实现 安装 使用docker安装 ,然后安装 : docker run -d -p 15672:15672 -p 5672:5672 --name rabbitmq rabbitmq:3-management 使用,创建一个交换和一个队列,并绑定它们。 设置NodeJS项目 初始...

    rabbitmq-nodejs-client:node.js的Rabbitmq客户端

    安装$ npm install rabbitmq-nodejs-client发布/订阅使用var rabbitHub = require('rabbitmq-nodejs-client');var subHub = rabbitHub.create( { task: 'sub', channel: 'myChannel' } );subHub.on('connection', ...

    nodejs-rabbitmq:简单的RabbitMQ发布者消费者应用程序,通过快速路由发送消息

    综上所述,`nodejs-rabbitmq`项目演示了如何利用Node.js、AMQP和RabbitMQ实现发布者-消费者模式,这对于构建异步、解耦的分布式系统非常有用。通过使用`amqplib`库,开发者可以轻松地创建连接、交换机和队列,进而...

    RabbitMQ in action

    Offered under an open source license and language neutral, RabbitMQ integrates seamlessly into applications written in written in C++, Java, Python, Erlang, and other standard languages. RabbitMQ in ...

    RabbitMQ in Depth.pdf

    RabbitMQ is an open source message broker written in Erlang, currently under the wing of Pivotal Software. It’s based around the AMQP open protocol, with official cli- ent libraries in Java, .NET, ...

    RabbitMQ-in-Action-Distributed-Messaging-for-Everyone.pdf

    around the world, both of us discovered RabbitMQ in the same way, and in response to trying to solve almost exactly the same problem! In fact, since you’re reading this book about RabbitMQ, it’s ...

    RabbitMQ In Action

    Offered under an open source license and language neutral, RabbitMQ integrates seamlessly into applications written in written in C++, Java, Python, Erlang, and other standard languages. RabbitMQ in ...

    RabbitMQ消息模式之Confirm确认消息

    理解Confirm消息确认机制 消息的确认,是指生产者投递消息后,如果Broker收到消息,则会给我们生产者一个应答。生产者进行接收应答,用来确定这条消息是否正常的发送到Broker,...import com.rabbitmq.client.Queuein

    Mastering RabbitMQ(PACKT,2016)

    RabbitMQ is one of the most powerful Open Source message broker software, which is widely used in tech companies such as Mozilla, VMware, Google, AT&T, and so on. RabbitMQ gives you lots of fantastic ...

    RabbitMQ in Action

    Offered under an open source license and language neutral, RabbitMQ integrates seamlessly into applications written in written in C++, Java, Python, Erlang, and other standard languages. RabbitMQ in ...

    RabbitMQ in depth 样章(chapter 1)

    RabbitMQ in depth的内容覆盖了从RabbitMQ的基础知识到深层次的应用,适合作为中级水平的读者使用,特别是那些正在编写使用RabbitMQ的应用程序,或者是在生产环境中负责管理RabbitMQ的开发者或系统管理员。...

    example-rabbitMQ-nodejs

    本教程以"example-rabbitMQ-nodejs"为例,深入讲解如何使用Node.js与RabbitMQ进行集成,实现高效的消息通信。 首先,我们要了解RabbitMQ的基本概念。RabbitMQ是一个基于AMQP(Advanced Message Queuing Protocol)...

    RabbitMQ in Action高清版

    ### RabbitMQ in Action 高清版 - 分布式消息传递为所有人 #### 一、书籍简介与背景 《RabbitMQ in Action》是一本全面介绍RabbitMQ分布式消息队列系统的书籍,由Alvaro Videla和Jason J. W. Williams共同撰写,...

    Java使用RabbitMq的一个简单demo

    RabbitMQ是当前广泛使用的开源消息队列系统,它基于AMQP(Advanced Message Queuing Protocol)协议,适用于多种编程语言,包括Java。本篇文章将深入探讨如何在Java环境下使用RabbitMQ实现一个简单的示例。 首先,...

    android上RabbitMQ发送简单例子

    在Android平台上,使用RabbitMQ进行网络通信是一个高效且可靠的选择。RabbitMQ是一个开源的消息代理和队列服务器,它允许应用程序之间通过消息传递进行异步通信。在本例中,我们将探讨如何在Android上设置和使用...

    windows RabbitMQ安装总结.docx

    Windows RabbitMQ 安装总结 本文总结了 Windows 平台下 RabbitMQ 的安装过程,从下载 Erlang 到安装 RabbitMQ 服务器,并提供了 Java 代码示例,展示了生产者和消费者端的实现。 一、Erlang 的安装 Erlang 是 ...

    rabbitmq-server-windows-3.6.12.zip

    Passwords in source and destination URIs are now redacted out. GitHub issue: rabbitmq-federation-management#15 Federation Management Plugin Bug Fixes Passwords in upstream URIs are now redacted ...

Global site tag (gtag.js) - Google Analytics