javascript - 如何将 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 类就没有意义了。如果你们从制作这个的经验中也有更好的想法,那将很有帮助。
解决方案
推荐阅读
- python - keras 和 sigmoid 不能预测正确的类别
- python - Celery 任务记录多次 - 记录器调用的最佳实践
- reactjs - 未找到公共文件夹中的下一个 js 图像
- node.js - 即使经过身份验证,也无法通过 url 访问 Azure 存储 Blob
- cordova - Cordova:不允许加载本地资源:file:///
- python - 试图为目录中的每个文本文件计算一个值,但它只给我一个结果
- python - 将鼠标悬停在所选单词上时弹出
- python - Beautiful Soup 内容为空,无法抓取此网页,是否被屏蔽?
- javascript - 更改下拉菜单位置时清除谷歌地图上的标记
- windows - 如何 Cygwin 编译目录中的所有 .c 文件(在 Windows 中通配)