首页 > 解决方案 > 如何将 rabbitMQ 连接分配给类的属性以在另一个函数中使用它?

问题描述

所以主要的想法是做一个 Wrapper Class 来保存一些函数,以便更容易使用 rabbitmq 的发布、订阅和 RPC 特性。例如,目标是,如果我想发布一些东西,我会很容易地使用 Rabbit Wrapper 类中的发布函数,对于 exp,它看起来像这样:var rabbit = new IRabbitMQ();然后发布我将使用发布者函数rabbit.publisher(exchangeName, queueName, etc..),如果我是有兴趣接收然后我会很容易打电话rabbit.subscriber(exchangeName, queueName, etc..)

我尝试了我脑海中的所有想法,它不适用于嵌套类,因为在 js 中我没有找到方便的方法来做到这一点,我还尝试了工厂函数或简单对象并避免使用类,但它没有工作。问题始终是,当我在我和 rabbitMQ 之间建立连接并创建通道时,如果我想的话,我无法将该连接和通道存储在 Wrapper 类的属性中以便在另一个函数中使用它

 class IRabbitMQ {

constructor() {
   this.init(rabbitMQServer); // this should initialize my this.connection and this.channel property to the connection and channel object but instead receive an error because there are undefined
 }

async init(host) {
    try {
        const connection = await amqplib.connect(host);
        const channel = await connection.createChannel();
        channel.prefetch(1);
        console.log(' [x] Awaiting RPC requests');
        this.connection = connection; // this should make a property connection with the connection object, when i console.log here it shows me the connection object but if i want to use this in another function it gaves me undefined value!
        this.channel = channel;
    }catch(err) {
        console.error(err);
    }
}

async publisher(queue, body) {

    let q = await this.channel.assertQueue(queue, {durable: false});

    this.channel.sendToQueue(q, Buffer.from(JSON.stringify(body)));

    console.log("msg sent ", body);
}

async subsriber(queue) {

    let q = await this.channel.assertQueue(queue, {durable: false});
    console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);

    await  this.channel.consume(q, function(msg) {
        console.log(" [x] Received %s", msg.content.toString());
      }, {noAck: true});
}


}

   // another example using a simple object instead of a class
      /* var IRabbitMQ = {

channel : undefined, 

init : async (host) => {

    try {
        const connection = await amqplib.connect(host);
        const channel = await connection.createChannel();
        channel.prefetch(1);
        console.log(' [x] Awaiting RPC requests');
        this.channel = channel;

    }catch(err) {
        console.error(err);
    }
},

publisher : async (queue, body) =>{
    this.init(rabbitMQServer);
    let q = await this.channel.assertQueue(queue, {durable: false});

    this.channel.sendToQueue(q, Buffer.from(JSON.stringify(body)));

    console.log("msg sent ", body);
},

subscriber : async(queue) => {
    this.init(rabbitMQServer);
    let q = await this.channel.assertQueue(queue, {durable: false});
    console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);

    await  this.channel.consume(q, function(msg) {
        console.log(" [x] Received %s", msg.content.toString());
      }, {noAck: true});
}

   } */

预期的输出是我可以访问连接和通道的值,以便稍后在另一个函数中使用它,因为如果不是这样,创建一个 Wrapper 类就没有意义了。如果你们从制作这个的经验中也有更好的想法,那将很有帮助。

标签: javascriptnode.jsrabbitmqamqp

解决方案


推荐阅读