Last active
July 11, 2018 11:01
-
-
Save spddl/3984107a09dc5f35dc39208e2ba4a61c to your computer and use it in GitHub Desktop.
amqp
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
#!/usr/bin/env node | |
// https://www.rabbitmq.com/tutorials/tutorial-two-javascript.html | |
var amqp = require('amqplib/callback_api') | |
const arr = ['1', '2', '3', '4', '5', '6', '7', '{a, "b"}'] // {a: 'b'}, ['a', 'b'] | |
amqp.connect('amqp://localhost', function (err, conn) { | |
if (err) console.warn(err) | |
conn.createChannel(function (err, ch) { | |
if (err) console.warn(err) | |
var q = 'task_queue' | |
for (let i = 0; i < arr.length; i++) { | |
const msg = arr[i] | |
ch.assertQueue(q, {durable: true}) // https://www.squaremobius.net/amqp.node/channel_api.html#channel_assertQueue | |
ch.sendToQueue(q, Buffer.from(msg), {persistent: true}) // https://www.squaremobius.net/amqp.node/channel_api.html#channel_sendToQueue | |
console.log(new Date(), ' [x] Sent "%s"', msg) | |
} | |
}) | |
setTimeout(function () { conn.close(); process.exit(0) }, 500) | |
}) |
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
#!/usr/bin/env node | |
var amqp = require('amqplib/callback_api') | |
amqp.connect('amqp://localhost', function (err, conn) { | |
if (err) console.warn(err) | |
conn.createChannel(function (err, ch) { | |
if (err) console.warn(err) | |
var q = 'task_queue' | |
ch.assertQueue(q, {durable: true}) // https://www.squaremobius.net/amqp.node/channel_api.html#channel_assertQueue | |
ch.prefetch(1) | |
console.log(' [*] Waiting for messages in %s. To exit press CTRL+C', q) | |
ch.consume(q, function (msg) { | |
var secs = msg.content.toString().split('.').length - 1 | |
console.log(new Date(), ' [x] Received %j', msg.content.toString()) | |
// if (JSON.parse(msg.content.toString()).a) { | |
// console.log('json gefunden', JSON.parse(msg.content.toString()).a) | |
// } | |
setTimeout(function () { | |
console.log(new Date(), ' [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