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 = 'consumer1_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
|
2024-12-10 21:33:59 +04:00
|
|
|
}, (err, q) => {
|
|
|
|
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());
|
2024-12-10 21:33:59 +04:00
|
|
|
setTimeout(() => {
|
2024-12-13 05:48:37 +04:00
|
|
|
console.log(" [x] Done processing %s", msg.content.toString());
|
|
|
|
}, 3000);
|
|
|
|
}
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|