8000 Routing tutorial · bsumgit/rabbitmq-tutorials@a6f70b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit a6f70b3

Browse files
committed
Routing tutorial
1 parent 6f6be60 commit a6f70b3

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env node
2+
3+
var amqp = require('amqplib');
4+
var when = require('when');
5+
6+
var conn = amqp.connect('amqp://localhost')
7+
conn.then(createChannel).then(null, console.warn);
8+
9+
function createChannel(conn) {
10+
return when(conn.createChannel().then(logMessage)).ensure(function() { conn.close(); });
11+
}
12+
13+
function logMessage(ch) {
14+
var ex = 'direct_logs';
15+
var ok = ch.assertExchange(ex, 'direct', {durable: false})
16+
17+
return ok.then(function() {
18+
var args = process.argv.slice(2);
19+
var msg = args.slice(1).join(' ') || 'Hello World!';
20+
var severity = (args.length > 0) ? args[0] : 'info';
21+
22+
ch.publish(ex, severity, new Buffer(msg));
23+
console.log(" [x] Sent %s:'%s'", severity, msg);
24+
return ch.close();
25+
});
26+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env node
2+
3+
usage();
4+
5+
var amqp = require('amqplib');
6+
var all = require('when').all;
7+
var severities = process.argv.slice(2);
8+
var conn = amqp.connect('amqp://localhost')
9+
var ch = conn.then(createChannel).then(null, console.warn);
10+
11+
ch.then(function(ch) {
12+
var x = ch.assertExchange('direct_logs', 'direct', {durable: false});
13+
var q = x.then(function() {
14+
return ch.assertQueue('', {exclusive: true});
15+
});
16+
17+
var ok = q.then(function(qok) {
18+
var queue = qok.queue;
19+
return all(severities.map(function(sev) {
20+
ch.bindQueue(queue, 'direct_logs', sev);
21+
})).then(function() { return queue; });
22+
});
23+
24+
ok = ok.then(function(queue) {
25+
return ch.consume(queue, logMessage, {noAck: true});
26+
});
27+
return ok.then(function() {
28+
console.log(' [*] Waiting for logs. To exit press CTRL+C');
29+
});
30+
31+
function logMessage(msg) {
32+
console.log(" [x] %s:'%s'", msg.fields.routingKey, msg.content.toString());
33+
}
34+
});
35+
36+
function createChannel(conn) {
37+
process.once('SIGINT', function() { conn.close(); });
38+
return conn.createChannel();
39+
}
40+
41+
function usage() {
42+
if (process.argv.length < 3) {
43+
console.log("Usage: receive_logs_direct.js [info] [warning] [error]");
44+
process.exit(1);
45+
}
46+
}

0 commit comments

Comments
 (0)
0