Last active
April 11, 2018 03:01
-
-
Save alifrizkip/458066ee847309d0706a5ea2f7cb6d2e to your computer and use it in GitHub Desktop.
Work Queue workerConsumer.js RabbitMQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const amqp = require('amqplib/callback_api') // Import library amqplib | |
amqp.connect('amqp://localhost', (err, conn) => { | |
conn.createChannel((err, ch) => { | |
const q = 'task_queue' // Nama antrian adalah 'task_queue' | |
ch.assertQueue(q, { durable: true }) // Menyatakan antriannya adalah 'task_queue' | |
ch.prefetch(1) | |
console.log(`[*] Waiting for messages in %s. To exit press CTRL+C`, q) | |
/* Menangkap pesan yang dikirimkan RabbitMQ dari antrian */ | |
ch.consume(q, msg => { | |
const secs = msg.content.toString().split('.').length - 1 | |
console.log(`[x] Received %s`, msg.content.toString()) | |
setTimeout(() => { | |
console.log(`[x] Done`) | |
ch.ack(msg) | |
}, secs * 1000) | |
}, { noAck: false }) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment