8000 refactored Publish/Subscribe tutorial code · zerolugithub/rabbitmq-tutorials@99a13e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 99a13e7

Browse files
committed
refactored Publish/Subscribe tutorial code
1 parent b818682 commit 99a13e7

File tree

2 files changed

+22
-43
lines changed

2 files changed

+22
-43
lines changed

javascript-nodejs/src/emit_log.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
#!/usr/bin/env node
22

3-
var amqp = require('amqplib');
4-
var when = require('when');
3+
var amqp = require('amqplib/callback_api');
54

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 = 'logs';
15-
var ok = ch.assertExchange(ex, 'fanout', {durable: false})
16-
17-
return ok.then(function() {
5+
amqp.connect('amqp://localhost', function(err, conn) {
6+
conn.createChannel(function(err, ch) {
7+
var ex = 'logs';
188
var msg = process.argv.slice(2).join(' ') || 'Hello World!';
199

10+
ch.assertExchange(ex, 'fanout', {durable: false})
2011
ch.publish(ex, '', new Buffer(msg));
21-
console.log(" [x] Sent '%s'", msg);
22-
return ch.close();
12+
console.log(" [x] Sent %s", msg);
2313
});
24-
}
14+
15+
setTimeout(function() { conn.close(); process.exit(0) }, 500);
16+
});

javascript-nodejs/src/receive_logs.js

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,20 @@
11
#!/usr/bin/env node
22

3-
var amqp = require('amqplib');
4-
var conn = amqp.connect('amqp://localhost')
5-
var ch = conn.then(createChannel).then(null, console.warn);
3+
var amqp = require('amqplib/callback_api');
64

7-
ch.then(function(ch) {
8-
var x = ch.assertExchange('logs', 'fanout', {durable: false});
9-
var q = x.then(function() {
10-
return ch.assertQueue('', {exclusive: true});
11-
});
5+
amqp.connect('amqp://localhost', function(err, conn) {
6+
conn.createChannel(function(err, ch) {
7+
var ex = 'logs';
8+
9+
ch.assertExchange(ex, 'fanout', {durable: false});
1210

13-
ok = q.then(function(qok) {
14-
return ch.bindQueue(qok.queue, 'logs', '').then(function() {
15-
return qok.queue;
11+
ch.assertQueue('', {exclusive: true}, function(err, q) {
12+
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q.queue);
13+
< 8000 span class=pl-s1>ch.bindQueue(q.queue, ex, '');
14+
15+
ch.consume(q.queue, function(msg) {
16+
console.log(" [x] %s", msg.content.toString());
17+
}, {noAck: true});
1618
});
1719
});
18-
ok = ok.then(function(queue) {
19-
return ch.consume(queue, logMessage, {noAck: true});
20-
});
21-
return ok.then(function() {
22-
console.log(' [*] Waiting for logs. To exit press CTRL+C');
23-
});
24-
25-
function logMessage(msg) {
26-
console.log(" [x] '%s'", msg.content.toString());
27-
}
2820
});
29-
30-
function createChannel(conn) {
31-
process.once('SIGINT', function() { conn.close(); });
32-
return conn.createChannel();
33-
}

0 commit comments

Comments
 (0)
0