javascript - 如何使用 amqplib 库中的 channel.assertQueue 函数用于 node.JS?

标签 javascript node.js rabbitmq amqp php-amqplib

我正在使用 RabbitMQ 和 Node.JS 开发消息传递应用程序。为此,我正在使用 amqplib。我是 Node.JS 的新手,在理解 amqplib 的语法方面发现了一些困难。 例如有一个声明队列的函数, 也就是

channel.assertQueue([queue, [options, [function(err, ok) {...}]]]);

我一直在引用This从过去 2-3 天开始,但我仍然不清楚这些 -> errok。如何使用这些参数?

一个例子将不胜感激。

最佳答案

ampqlib github page有一些关于如何使用该库的示例,使用回调或 promise 。

我复制了他们的第一个示例并添加了一些注释来解释发生了什么。

可能值得检查他们的 tutorial examples同样,这遵循官方 RabbitMQ tutorials .

var amqp = require('amqplib/callback_api');
var q = 'tasks';

// connects to rabbitmq
amqp.connect('amqp://localhost', function(err, conn) {
    // this function will be called when the connection is created
    // `err` will contain the error object, if any errors occurred
    // `conn` will contain the connection object

    if (err != null) bail(err); // calls `bail` function if an error occurred when connecting
    consumer(conn); // creates a consumer
    publisher(conn); // creates a publisher
});

function bail(err) {
    console.error(err);
    process.exit(1);
}

// Publisher
function publisher(conn) {
    conn.createChannel(on_open); // creates a channel and call `on_open` when done
    function on_open(err, ch) {
        // this function will be called when the channel is created
        // `err` will contain the error object, if any errors occurred
        // `ch` will contain the channel object

        if (err != null) bail(err); // calls `bail` function if an error occurred when creating the channel
        ch.assertQueue(q); // asserts the queue exists
        ch.sendToQueue(q, new Buffer('something to do')); // sends a message to the queue
    }
}

// Consumer
function consumer(conn) {
    var ok = conn.createChannel(on_open); // creates a channel and call `on_open` when done
    function on_open(err, ch) {
        // this function will be called when the channel is created
        // `err` will contain the error object, if any errors occurred
        // `ch` will contain the channel object

        if (err != null) bail(err); // calls `bail` function if an error occurred when creating the channel
        ch.assertQueue(q); // asserts the queue exists
        ch.consume(q, function(msg) { //consumes the queue
            if (msg !== null) {
                console.log(msg.content.toString()); // writes the received message to the console
                ch.ack(msg); // acknowledge that the message was received
            }
        });
    }
}

关于javascript - 如何使用 amqplib 库中的 channel.assertQueue 函数用于 node.JS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30396350/

相关文章:

javascript - 如何使用 Busboy 检测上传流意外停止

javascript - Laravel Mix 自定义生成大 JS 文件

mysql - 使用 Node 将 JSON 对象导入 mysql 的最快方法

node.js - RabbitMQ/AMQP : single queue, 同一消息的多个消费者?

javascript - 服务器端渲染构建错误期间 Gatsby 窗口不可用

javascript - Highcharts 气泡图悬停颜色

javascript - 使用扫描的 DynamoDB 读取峰值

node.js - 如何使用对话流从 RESTful API 获取数据

rabbitmq - 如何通过 RabbitMQ 发送类似 word 的文件

php - 同一台服务器上的多个 gearman worker (PHP)