AMQP使用Node.js,我如何发布/订阅?

时间:2011-08-29 11:44:08

标签: node.js coffeescript rabbitmq amqp

我正在制作一个操作Node-AMQP模块的类:https://github.com/postwait/node-amqp

但我无法使用此发布/订阅:

  var Queue = require('./Queue.js');
  var queue = new Queue();
  queue.addTaskToQueue('salut', 5);
  queue.subscribeTaskQueue('salut');

这是我正在使用的类(我在CoffeeScript中提供代码,在Node.js中为那些不了解CoffeeScript的人提供代码):

感谢您的帮助。

在CoffeeScript中:

amqp = require('amqp')

class Queue

    constructor: (ip = 'localhost') ->
            @ip = ip
            @receivedObject
            @connection = amqp.createConnection({ host: @ip })
            @queue

    subscribeTaskQueue: (queueToSubscribe) ->
            self = @
            self.connection.on('ready', ->
                    q = self.connection.queue(queueToSubscribe)
                    q.bind('#')

                    q.subscribe({ ack: true }, (message) ->
                            self.receivedObject = message
                            console.log(self.receivedObject)
                    )
            )

    addTaskToQueue: (queue, objectToSend) ->
            @queue = @connection.queue("salut", { durable: true })
            @connection.publish(queue, objectToSend)

module.exports = Queue

在Node.js

(function() {
  var Queue, amqp;
  amqp = require('amqp');
  Queue = (function() {
    function Queue(ip) {
      if (ip == null) {
        ip = 'localhost';
      }
      this.ip = ip;
      this.receivedObject;
      this.connection = amqp.createConnection({
        host: this.ip
      });
      this.queue;
    }
    Queue.prototype.subscribeTaskQueue = function(queueToSubscribe) {
      var self;
      self = this;
      return self.connection.on('ready', function() {
        var q;
        q = self.connection.queue(queueToSubscribe);
        q.bind('#');
        return q.subscribe({
          ack: true
        }, function(message) {
          self.receivedObject = message;
          return console.log(self.receivedObject);
        });
      });
    };
    Queue.prototype.addTaskToQueue = function(queue, objectToSend) {
      this.queue = this.connection.queue("salut", {
        durable: true
      });
      return this.connection.publish(queue, objectToSend);
    };
    return Queue;
  })();
  module.exports = Queue;
}).call(this);

我做了你说的话+我做了一些修改。

这是我的班级Queue.coffee:

amqp = require('amqp')

class Queue

    constructor: (ip = "localhost", queueName = "salut") ->
            @ip = ip
            @receivedObject = "test"
            @connection = amqp.createConnection({ host: 'localhost' })
            @queueName = queueName

    subscribeTaskQueue: () ->
            @connection.on('ready', ->
                    q = @connection.queue(@queueName)
                    q.bind('#')

                    q.subscribe({ ack: true }, (message) ->
                            @receivedObject = message
                            console.log(@receivedObject)
                    )
            )

    addTaskToQueue: (objectToSend = "hello") ->
            @connection.publish(@queueName, objectToSend)


queue = new Queue("localhost", "salut")

queue.connection.on 'ready', ->
  queue.addTaskToQueue 'salut', 5
  queue.subscribeTaskQueue 'salut'

这是rabbituser.coffee:

Queue = require('./Queue.js')
queue = new Queue("localhost", "salut")

queue.addTaskToQueue("hello")
queue.subscribeTaskQueue()

当我执行命令时:node rabbituser.js

我明白了:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
TypeError: Cannot read property '' of undefined
    at Connection.exchange (/home/armand/node_modules/amqp/amqp.js:1242:21)
    at Connection.publish (/home/armand/node_modules/amqp/amqp.js:1258:60)
    at Queue.addTaskToQueue (/home/armand/Desktop/RockSolidProject/coucheAMQP/Queue.js:36:30)
    at Object.<anonymous> (/home/armand/Desktop/RockSolidProject/coucheAMQP/rabbituser.js:5:9)
    at Object.<anonymous> (/home/armand/Desktop/RockSolidProject/coucheAMQP/rabbituser.js:7:4)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)
    at Array.<anonymous> (module.js:421:10)

2 个答案:

答案 0 :(得分:1)

我不确定你遇到的错误是什么,但我会指出一些事情:

  1. 在您的构造函数中,当您编写@receivedObject@queue时 - 它不会执行任何操作。在JavaScript中,每个对象都是一个哈希,你可以随时附加属性;如果在@receivedObject实例中最初未定义@queueQueue,则您不必在构造函数中定义它们。

  2. 问题可能是您在连接存在之前(即@connection.queue回调之前)在addTaskToQueue中调用self.connection.on 'ready'吗?

  3. 也许您将代码更改为

    queue = new Queue()
    queue.connection.on 'ready', ->
      queue.addTaskToQueue 'salut', 5
      queue.subscribeTaskQueue 'salut'
    

    如果这不能解决您的问题,请您描述您遇到的确切错误以及您遇到的错误?

答案 1 :(得分:0)

能够将amqp用作发布/订阅者,现在可以使用rabbitmq-nodejs-client