async function init() { const connection = await amqp.connect(config.rabbitmq.url) const q = config.rabbitmq.channel const channel = await connection.createChannel() await channel.assertQueue(q, { autoDelete: false }) channel.prefetch(1) channel.consume(q, (msg) => { console.log(msg.content.toString()) channel.ack(msg) }) }
async function consMsg() { try{ console.info('consumer listening...!') const chan = await initMQ() await chan.consume(queueCfg.queueId, function(msg){ console.log(msg.content.toString()) chan.ack(msg) }, { noAck: false }) } catch(e) { console.log(String(e)) } }
handleRequest(handler) { return this.channel.consume(this.queue, msg => { const content = JSON.parse(msg.content.toString()); handler(content, reply => { this.channel.sendToQueue( msg.properties.replyTo, new Buffer(JSON.stringify(reply)), {correlationId: msg.properties.correlationId} ); this.channel.ack(msg); }); }); }
async function connect() { const connection = await amqp.connect('amqp://localhost:5672'); const channel = await connection.createChannel(); const queue = channel.assertQueue(QUEUES.JOBS); channel.consume(QUEUES.JOBS, (job) => { let data = JSON.parse(job.content); console.log(data); channel.ack(job); }); console.log('Waiting for the message!'); }
async start (doForEveryItem) { this.channel.consume(this.queueName, (msg) => { doForEveryItem(msg) this.channel.ack(msg) }) }
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);
const consume = async (queue, isNoAck = false, durable = false, prefetch = null) => { const connect = await amqp.connect(config.rabbitMqUrl) const channel = await connect.createChannel() await channel.assertQueue(queue, { durable }) if (prefetch) { channel.prefetch(prefetch) } const consumeEmitter = new EventEmitter() try { channel.consume(queue, message => { if (message !== null) { consumeEmitter.emit('data', message.content.toString(), () => channel.ack(message)) } else { const error = new Error('NullMessageException') consumeEmitter.emit('error', error) } }, {noAck: isNoAck}) } catch (error) { consumeEmitter.emit('error', error) } return consumeEmitter }
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) }) }
amqp.connect('amqp://localhost', (error, connection) => { if (error) throw error; connection.createChannel(function (error, channel) { if (error) throw error; channel.prefetch(1); channel.assertQueue(queue, { durable: true }); channel.consume(queue, (msg) => { console.log(msg.content.toString()); setTimeout(function() { console.log(" [x] Done"); channel.ack(msg); }, 5000); }, { noAck: false }); }); });
// AMQP IP amqp.connect('amqp://10.55.71.212', function(err, conn) { conn.createChannel(function(err, ch) { var q = 'hello'; ch.assertQueue(q, {durable: false}); console.log(`Waiting for messages in ${q}. To exit press CTRL+C`); ch.consume(q, function (msg) { var json = JSON.parse(msg.content.toString()); console.log("Received: ",json.msg); console.log("Value:",json.value); ch.ack(msg); }) }); });
amqp.connect('amqp://localhost', (error, connection) => { if (error) throw error; connection.createChannel(function (error, channel) { if (error) throw error; channel.assertQueue(queue, { durable: true }); channel.prefetch(1); channel.consume(queue, (msg) => { console.log(msg.content.toString()); setTimeout(function() { console.log(" [x] Done"); channel.ack(msg); }, 5000); }, { noAck: false }); }); });
async start (doForEveryItem) { this.channel.consume(this.queueName, (msg) => { doForEveryItem(msg) this.channel.ack(msg) }) }
async start (doForEveryItem) { this.channel.consume(this.queueName, (msg) => { doForEveryItem(msg) this.channel.ack(msg) }) }
async start (doForEveryItem) { this.channel.consume(this.queueName, (msg) => { doForEveryItem(msg) this.channel.ack(msg) }) }