DAS_2024_1/bondarenko_max_lab_4/consumer2.js

39 lines
1.0 KiB
JavaScript
Raw Normal View History

2024-12-10 21:33:59 +04:00
const amqp = require('amqplib/callback_api');
2024-12-13 05:48:37 +04:00
amqp.connect('amqp://127.0.0.1', (err, connection) => {
2024-12-10 21:33:59 +04:00
if (err) {
throw err;
}
connection.createChannel((err, channel) => {
if (err) {
throw err;
}
2024-12-13 05:48:37 +04:00
const exchange = 'logs';
const queue = 'consumer2_queue';
2024-12-10 21:33:59 +04:00
2024-12-13 05:48:37 +04:00
channel.assertExchange(exchange, 'fanout', {
durable: false
2024-12-10 21:33:59 +04:00
});
2024-12-13 05:48:37 +04:00
channel.assertQueue(queue, {
durable: false
}, (err, q) => {
2024-12-10 21:33:59 +04:00
if (err) {
throw err;
}
2024-12-13 05:48:37 +04:00
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q.queue);
2024-12-10 21:33:59 +04:00
2024-12-13 05:48:37 +04:00
channel.bindQueue(q.queue, exchange, '');
2024-12-10 21:33:59 +04:00
channel.consume(q.queue, (msg) => {
2024-12-13 05:48:37 +04:00
if (msg.content) {
console.log(" [x] Received %s", msg.content.toString());
console.log(" [x] Done processing %s", msg.content.toString());
}
2024-12-10 21:33:59 +04:00
}, {
2024-12-13 05:48:37 +04:00
noAck: true
2024-12-10 21:33:59 +04:00
});
});
});
});