Node.js AMQP+;节点等待通道
我在FeathersJS中有一个启动到RabbitMQ连接的服务,问题是如何在接收请求之前等待通道准备就绪:Node.js AMQP+;节点等待通道,node.js,async-await,amqp,channel,feathersjs,Node.js,Async Await,Amqp,Channel,Feathersjs,我在FeathersJS中有一个启动到RabbitMQ连接的服务,问题是如何在接收请求之前等待通道准备就绪: class Service { constructor({ amqpConnection, queueName }) { this.amqpConnection = amqpConnection; this.queueName = queueName; this.replyQueueName = queueName + "Reply"
class Service {
constructor({ amqpConnection, queueName }) {
this.amqpConnection = amqpConnection;
this.queueName = queueName;
this.replyQueueName = queueName + "Reply"
}
async create(data, params) {
new Promise(resolve => {
if (!this.channel) await this.createChannel();
channel.responseEmitter.once(correlationId, resolve);
channel.sendToQueue(this.queueName, Buffer.from(data), {
correlationId: asyncLocalStorage.getStore(),
replyTo: this.replyQueueName,
});
});
}
async createChannel() {
let connection = this.amqpConnection();
let channel = await connection.createChannel();
await channel.assertQueue(this.queueName, {
durable: false,
});
this.channel = channel;
channel.responseEmitter = new EventEmitter();
channel.responseEmitter.setMaxListeners(0);
channel.consume(
this.replyQueueName,
(msg) => {
channel.responseEmitter.emit(
msg.properties.correlationId,
msg.content.toString("utf8")
);
},
{ noAck: true }
);
}
....
}
在请求期间等待创建通道似乎是一种浪费。如何“正确地”执行此操作?Feathers服务可以实现一个在服务器启动时调用(或者您自己调用
app.setup()
的服务):
是否有更高级别的异步
设置
选项-例如,在应用程序级别创建连接,然后在服务级别创建通道?
class Service {
async setup () {
await this.createChannel();
}
}