【问题标题】:node.js subscriber client for rabbitmq (implements topics (ExcahangeName))rabbitmq 的 node.js 订阅客户端(实现主题(ExcahangeName))
【发布时间】:2013-07-31 10:14:29
【问题描述】:

我正在尝试编写一个 node.js 客户端(订阅者模块)来使用来自 rabbitmq(AMQP) 的消息。 我正在尝试在 rabbitmq 中实现主题(exchangeName)。

我正在尝试使用 (easy-amqp) 或 postwait 来完成此任务。

我在 java 中写了一个发布者方法,想在 javascript(node.js) 中写一个订阅者方法。

我的 java 程序运行良好,我可以向 rabbitmq 发送消息。

我认为我搞砸了订阅者方法。当我运行订阅者方法时,它不会给我任何错误,也不会向控制台打印任何消息。

我的java方法有点像

//Publisher (written in java)

Connection connection = null;
Channel channel = null;
String routingKey =null;

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");

connection = factory.newConnection();
channel = connection.createChannel();

//publishing to a exchange_name topic

channel.exchangeDeclare(EXCHANGE_NAME, "topic");

//set the routing key

routingKey = "anonymous.info" ;

channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
System.out.println("Sent '" + routingKey + "':'" + message + "'");

//js中的订阅者方法(node.js) //使用(postwait-node-amqp)

var amqp = require('amqp');

var connection = amqp.createConnection({defaultExchangeName: "topic"});

// Wait for connection to become established.
   connection.on('ready', function () {

  connection.queue('anonymous.info', function(q){
      // Catch all messages
      q.bind('#');

      // Receive messages
      q.subscribe(function (message) {
        // Print messages to stdout
        console.log(message);
      });
  });
});

这不会给我任何错误,但它甚至不会将任何消息打印到控制台。


然后我遇到了另一个名为 easy-amqp 的库。我试了一下

// 订阅者使用easy-amqp。

var easyamqp = require('easy-amqp');

var connection = easyamqp.createConnection("amqp://localhost:5672");



// setting the exchange
connection.exchange('topic')

connection.on('ready', function () {

  connection.queue('anonymous.info', function(q){

      q.bind('#');

      // Receive messages
      q.subscribe(function (message) {
        // Print messages to stdout
        console.log(message);
      });
  });
});

这也没有给我想要的结果。

【问题讨论】:

    标签: node.js rabbitmq node-amqp


    【解决方案1】:

    您检查控制台日志了吗?先检查一下。

    connection.on('ready', function () {
      **console.log('ready function called!');**
    
      connection.queue('anonymous.info', function(q){
          // Catch all messages
          q.bind('#');
    
          // Receive messages
          q.subscribe(function (message) {
            // Print messages to stdout
            console.log(message);
          });
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2020-04-26
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 2015-04-04
      • 1970-01-01
      相关资源
      最近更新 更多