async emit(data) {
const exchange = 'jiomedia';
let connection = await amqp.connect(rabbitmq.url);
try {
const {products, categories} = data;
const productInfo = await this.getProductInfo(products);
const metadtaInfo = await this.getMetadataInfo(categories);
for await (const iterator of productInfo) {
let {name} = iterator;
let routingKey = name.toLowerCase();
delete data.products;
const channel = await connection.createChannel();
await channel.assertExchange(exchange, 'topic', { durable: true });
channel.publish(exchange, routingKey, Buffer.from(JSON.stringify(data)));
if(metadtaInfo){
channel.publish(exchange, `${routingKey}.content`, Buffer.from(JSON.stringify(metadtaInfo)));
}
console.log(" [x] Sent %s:'%s'", routingKey, data);
await channel.close();
}
}
catch (err) {
console.warn(err);
}
finally {
if (connection) await connection.close();
};
}
0 Comments