Nodejs:在主题交换中使用 rabbitmq 消息 [英] Nodejs: Consume rabbitmq messages in topic exchange

查看:62
本文介绍了Nodejs:在主题交换中使用 rabbitmq 消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 rabbitmq topic exchange.在 rabbitmq 网络管理中,我创建了一个名为 queue1 的队列,其中包含以下详细信息:

创建队列后,我使用了默认交换 amq.topic 并使用 anonymous.info 路由键将 queue1 绑定到此交换:

将一些消息推送到此队列 1 后:

现在我想使用这些消息,以便我编写此脚本:

var amqp = require('amqplib/callback_api');amqp.connect(uri, (error0, connection) => {如果(错误0){抛出错误0;}connection.createChannel((error1, channel) => {如果(错误1){抛出错误1;}var exchange = 'amq.topic';channel.assertExchange(交换,'主题',{耐用:真实});channel.assertQueue('queue1', { 独占:真,持久:真},(error2, q) => {如果(错误2){抛出错误2;}console.log(' [*] 等待日志.退出按 CTRL+C');var key = 'anonymous.info';channel.bindQueue(q.queue, exchange, key);channel.consume(q.queue, function (msg) {console.log(" [x] %s:'%s'", msg.fields.routingKey, msg.content.toString());}, {noAck: 真});});});});

但是我遇到了这个错误:

错误:操作失败:QueueDeclare;405 (RESOURCE-LOCKED) 带有消息RESOURCE_LOCKED - 无法获得对 vhost '/' 中锁定队列 'queue1' 的独占访问权限.它最初可能是在另一个连接上声明的,或者独占属性值与原始声明的值不匹配."

所以我将 channel.assertQueue() 方法更改为此,意味着我删除了队列名称:

channel.assertQueue('', { exclusive: true, Durable: true }, (error2, q) => {如果(错误2){抛出错误2;}

现在我没有收到这些错误,但我没有任何结果.我在 queue1 中有 101 条消息?

channel.assertQueue回调中q的结果是:

对象{队列:amq.gen-Z7PhA8xKdA7v0H_33alxDA",messageCount:0,consumerCount:0}

但我没有这个队列名称amq.gen-Z7PhA8xKdA7v0H_33alxDA.

它是 Temporary queues 但我有一个队列,我想从我的队列中读取.

解决方案

从快照来看,您的queue1"是持久的、非独占的;并且您的代码尝试使用持久、独占的队列

I am trying to use rabbitmq topic exchange. In rabbitmq web management i created a queue called queue1 with this details :

After creating queue i used default exchange amq.topic and i binded queue1 to this exchange with anonymous.info routing key:

After push some message to this queue1 :

Now i want to consume these messages so in order to i wrote this script:

var amqp = require('amqplib/callback_api');
amqp.connect(uri, (error0, connection) => {
    if (error0) {
        throw error0;
    }
    connection.createChannel((error1, channel) => {
        if (error1) {
            throw error1;
        }
        var exchange = 'amq.topic';

        channel.assertExchange(exchange, 'topic', {
            durable: true
        });

        channel.assertQueue('queue1', { exclusive: true, durable: true }, (error2, q) => {
            if (error2) {
                throw error2;
            }
            console.log(' [*] Waiting for logs. To exit press CTRL+C');
            var key = 'anonymous.info';

            channel.bindQueue(q.queue, exchange, key);
            channel.consume(q.queue, function (msg) {
                console.log(" [x] %s:'%s'", msg.fields.routingKey, msg.content.toString());
            }, {
                noAck: true
            });
        });
    });
});

But i got this error :

Error: Operation failed: QueueDeclare; 405 (RESOURCE-LOCKED) with message "RESOURCE_LOCKED - cannot obtain exclusive access to locked queue 'queue1' in vhost '/'. It could be originally declared on another connection or the exclusive property value does not match that of the original declaration."

So i change channel.assertQueue() method to this, means i removed queue name:

channel.assertQueue('', { exclusive: true, durable: true }, (error2, q) => {
    if (error2) {
        throw error2;
    }

Now i did not get those error but i did not any result. I have 101 messages in queue1?

In channel.assertQueue callback the result of q is :

Object {queue: "amq.gen-Z7PhA8xKdA7v0H_33alxDA", messageCount: 0, consumerCount: 0}

but i do not have this queue name amq.gen-Z7PhA8xKdA7v0H_33alxDA.

It is Temporary queues but i have a queue and i want to read from my queue.

解决方案

from the snapshot your "queue1" is durable, non-exclusvie; and your code try with a queue durable, exclusive

这篇关于Nodejs:在主题交换中使用 rabbitmq 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆