]) .then(() => this.broker.Promise.all([ this.channel.bindQueue(...bindingArgs), this.channel.consume( queueName,
async function init() { const connection = await amqp.connect(config.rabbitmq.url) const e = config.rabbitmq.exchange const channel = await connection.createChannel() await channel.assertExchange(e, 'fanout', { durable: false, autoDelete: true }) const q = await channel.assertQueue('', { exclusive: true }) await channel.bindQueue(q.queue, e, '') channel.consume(q.queue, (msg) => { console.log(msg.content.toString()) channel.ack(msg) }) }
const setup = async () => { console.log("Setting up RabbitMQ Exchanges/Queues") // connect to RabbitMQ instance let connection = await amqp.connect(messageQueueConnection) // create a channel let channel = await connection.createChannel() // create exchange await channel.assertExchange("processing", "direct", { durable: true }) // create queues await channel.assertQueue("processing.requests", { durable: true }) await channel.assertQueue("processing.results", { durable: true }) // bind queues await channel.bindQueue("processing.requests", "processing", "request") await channel.bindQueue("processing.results", "processing", "result") console.log("Setup DONE") process.exit() }
amqp.connect('amqp://localhost').then(function(conn) { process.once('SIGINT', function() { conn.close(); }); return conn.createChannel().then(function(ch) { var ok = ch.assertExchange('logs', 'fanout', {durable: true}); ok = ok.then(function() { var queueName = process.argv.slice(2).join(' '); return ch.assertQueue(queueName, {exclusive: false}); }); ok = ok.then(function(qok) { return ch.bindQueue(qok.queue, 'logs', '').then(function() { return qok.queue; }); }); ok = ok.then(function(queue) { return ch.consume(queue, logMessage, {noAck: true}); }); return ok.then(function() { console.log(' [*] Waiting for logs. To exit press CTRL+C'); }); function logMessage(msg) { console.log(" " + msg.fields.deliveryTag + " [x] " + msg.content.toString()); } }); }).then(null, console.warn);
amqp.connect('amqp://localhost', (error, connection) => { if (error) throw error; connection.createChannel((error, channel) => { if (error) throw error; channel.assertExchange('logs', 'fanout', { durable: false }); channel.assertQueue('', { exclusive: true }, (error, queue) => { if (error) throw error; channel.bindQueue(queue.queue, 'logs', ''); channel.consume(queue.queue, (msg) => { if(msg.content) { console.log(" [x] %s", msg.content.toString()); } }, { noAck: true }) }); }) });
amqp.connect('amqp://localhost').then((conn) => { return conn.createChannel(); }) .then((ch) => { const exchangeName = 'poc_topic'; const routingKey = process.argv[2]; const queueName = process.argv[3] || `service:${routingKey}`; ch.assertExchange(exchangeName, 'topic', { durable: true }); return ch.assertQueue(queueName, { exclusive: false }).then(q => { ch.bindQueue(q.queue, exchangeName, routingKey); console.log(`[Consumer] Ready to consume on ${exchangeName}::${routingKey}`); return ch.consume(q.queue, msg => { console.log(`[${routingKey}] got message: ${msg.content}`); ch.ack(msg) }); }); }) .catch(console.warn);
}); await channel.bindQueue(queueName, exchange, queueName);
const subscribe = async (exchangeName, exchangeType) => { const connect = await amqp.connect(config.rabbitMqUrl) const channel = await connect.createChannel() await channel.assertExchange(exchangeName, exchangeType, {durable: false}) const queue = await channel.assertQueue('', {exclusive: true}) channel.bindQueue(queue.queue, exchangeName, '') const consumeEmitter = new EventEmitter() try { channel.consume(queue.queue, message => { if (message !== null) { consumeEmitter.emit('data', message.content.toString()) } else { const error = new Error('NullMessageException') consumeEmitter.emit('error', error) } }, {noAck: true}) } catch (error) { consumeEmitter.emit('error', error) } return consumeEmitter }
log_ch.assertExchange(ex, 'fanout', {durable: false}); log_ch.assertQueue('', {exclusive: true}, function(err, q) { log_ch.bindQueue(q.queue, ex, ''); log_ch.consume(q.queue, function(msg) { var Info = JSON.parse(msg.content.toString());
amqp.connect('amqp://localhost').then(function(conn) { process.once('SIGINT', function() { conn.close(); }); return conn.createChannel().then(function(ch) { return ch.assertExchange('logs', 'fanout', {durable: true}) .then(function() { var queueName = 'history'; return ch.assertQueue(queueName, {exclusive: false}); }) .then(function(qok) { return ch.bindQueue(qok.queue, 'logs', '').then(function() { return qok.queue; }); }) .then(function(queue) { return ch.consume(queue, logMessage, {noAck: false}); }) .then(function() { console.log(' [*] Waiting for logs. To exit press CTRL+C'); }); function logMessage(msg) { console.log(" " + msg.fields.deliveryTag + " [x] " + msg.content.toString()); } }); }).then(null, console.warn);
amqp.connect('amqp://localhost', function(err, conn) { conn.createChannel(function(err, ch) { var ex = 'topic_logs'; ch.assertExchange(ex, 'topic', {durable: false}); ch.assertQueue('', {exclusive: true}, function(err, q) { console.log(' [*] Waiting for logs. To exit press CTRL+C'); args.forEach(function(key) { ch.bindQueue(q.queue, ex, key); }); ch.consume(q.queue, function(msg) { console.log(" [x] %s:'%s'", msg.fields.routingKey, msg.content.toString()); }, {noAck: true}); }); }); });
connectTopicChannel() { amqp.connect(conn, (err, conn) => { if (err) { wl(err, false); } else { conn.createChannel((err, ch) => { ch.assertExchange(exchange, 'topic', { durable: true }); ch.assertQueue(queue, { exclusive: false }, (err, q) => { wl(' [*] Waiting for data. To exit press CTRL+C', true); ch.bindQueue(q.queue, exchange, rabbitMQconfig.keyPattern); // receive data and do whatever ch.consume(q.queue, (msg) => { // write message to consol using our writeLog function wl(`${msg.fields.routingKey}: ${msg.content.toString()}`, true); }, { noAck: true }); }); }); } }); }
}) .then(function(qok) { return ch.bindQueue(qok.queue, 'logs', '').then(function() { return qok.queue; });
amqp.connect('amqp://localhost').then(function(conn) { process.once('SIGINT', function() { conn.close(); }); return conn.createChannel().then(function(ch) { return ch.assertExchange('logs', 'fanout', {durable: true}) .then(function() { var queueName = process.argv.slice(2).join(' '); return ch.assertQueue(queueName, {exclusive: false}); }) .then(function(qok) { return ch.bindQueue(qok.queue, 'logs', '').then(function() { return qok.queue; }); }) .then(function(queue) { return ch.consume(queue, logMessage, {noAck: false}); }) .then(function() { console.log(' [*] Waiting for logs. To exit press CTRL+C'); }); function logMessage(msg) { console.log(" " + msg.fields.deliveryTag + " [x] " + msg.content.toString()); ch.ack(msg); } }); }).then(null, console.warn);