amqplib 0.10.2 → 0.10.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/ISSUE_TEMPLATE/bug_report.md +41 -0
- package/.github/workflows/test.yml +15 -2
- package/CHANGELOG.md +12 -1
- package/Makefile +3 -4
- package/README.md +1 -1
- package/bin/generate-defs.js +5 -5
- package/examples/direct_reply_to_client.js +22 -0
- package/examples/direct_reply_to_server.js +25 -0
- package/examples/headers.js +29 -31
- package/examples/ssl.js +16 -10
- package/examples/stream_queues/README.md +40 -0
- package/examples/stream_queues/package.json +14 -0
- package/examples/stream_queues/receive_stream.js +55 -0
- package/examples/stream_queues/send_stream.js +38 -0
- package/examples/tutorials/callback_api/emit_log.js +22 -22
- package/examples/tutorials/callback_api/emit_log_direct.js +24 -24
- package/examples/tutorials/callback_api/emit_log_topic.js +23 -20
- package/examples/tutorials/callback_api/new_task.js +21 -20
- package/examples/tutorials/callback_api/receive.js +27 -21
- package/examples/tutorials/callback_api/receive_logs.js +32 -26
- package/examples/tutorials/callback_api/receive_logs_direct.js +42 -41
- package/examples/tutorials/callback_api/receive_logs_topic.js +43 -40
- package/examples/tutorials/callback_api/rpc_client.js +39 -37
- package/examples/tutorials/callback_api/rpc_server.js +42 -32
- package/examples/tutorials/callback_api/send.js +21 -22
- package/examples/tutorials/callback_api/worker.js +29 -25
- package/examples/tutorials/emit_log.js +20 -15
- package/examples/tutorials/emit_log_direct.js +22 -16
- package/examples/tutorials/emit_log_topic.js +22 -15
- package/examples/tutorials/new_task.js +20 -13
- package/examples/tutorials/receive.js +19 -14
- package/examples/tutorials/receive_logs.js +25 -23
- package/examples/tutorials/receive_logs_direct.js +29 -35
- package/examples/tutorials/receive_logs_topic.js +28 -32
- package/examples/tutorials/rpc_client.js +37 -40
- package/examples/tutorials/rpc_server.js +36 -29
- package/examples/tutorials/send.js +26 -18
- package/examples/tutorials/worker.js +27 -21
- package/examples/waitForConfirms.js +18 -19
- package/lib/callback_model.js +295 -290
- package/lib/channel.js +353 -346
- package/lib/channel_model.js +5 -1
- package/lib/codec.js +2 -2
- package/lib/connection.js +542 -527
- package/lib/credentials.js +1 -1
- package/lib/defs.js +2568 -2457
- package/lib/format.js +0 -1
- package/lib/frame.js +1 -1
- package/lib/heartbeat.js +32 -29
- package/lib/mux.js +96 -99
- package/package.json +2 -2
- package/test/callback_api.js +182 -174
- package/test/channel_api.js +455 -445
- package/test/codec.js +4 -4
- package/test/frame.js +5 -5
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const amqp = require('amqplib/callback_api');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
if (conn) conn.close(function() { process.exit(1); });
|
|
8
|
-
}
|
|
5
|
+
const queue = 'task_queue';
|
|
6
|
+
const text = process.argv.slice(2).join(' ') || "Hello World!";
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
if (err
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
console.log(" [x] Sent '%s'", msg);
|
|
22
|
-
ch.close(function() { conn.close(); });
|
|
8
|
+
amqp.connect((err, connection) => {
|
|
9
|
+
if (err) return bail(err);
|
|
10
|
+
connection.createChannel((err, channel) => {
|
|
11
|
+
if (err) return bail(err, connection);
|
|
12
|
+
channel.assertQueue(queue, { durable: true }, (err) => {
|
|
13
|
+
if (err) return bails(err, connection);
|
|
14
|
+
channel.sendToQueue(queue, Buffer.from(text), { persistent: true });
|
|
15
|
+
console.log(" [x] Sent '%s'", text);
|
|
16
|
+
channel.close(() => {
|
|
17
|
+
connection.close();
|
|
18
|
+
});
|
|
23
19
|
});
|
|
24
20
|
});
|
|
25
|
-
}
|
|
21
|
+
});
|
|
26
22
|
|
|
27
|
-
|
|
23
|
+
function bail(err, connection) {
|
|
24
|
+
console.error(err);
|
|
25
|
+
if (connection) connection.close(() => {
|
|
26
|
+
process.exit(1);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
@@ -1,30 +1,36 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const amqp = require('amqplib/callback_api');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
console.error(err);
|
|
7
|
-
if (conn) conn.close(function() { process.exit(1); });
|
|
8
|
-
}
|
|
5
|
+
const queue = 'hello';
|
|
9
6
|
|
|
10
|
-
|
|
11
|
-
if (err
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
var q = 'hello';
|
|
7
|
+
amqp.connect((err, connection) => {
|
|
8
|
+
if (err) return bail(err);
|
|
9
|
+
connection.createChannel((err, channel) => {
|
|
10
|
+
if (err) return bail(err, connection);
|
|
15
11
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
ch.consume(q, function(msg) { // message callback
|
|
20
|
-
console.log(" [x] Received '%s'", msg.content.toString());
|
|
21
|
-
}, {noAck: true}, function(_consumeOk) { // consume callback
|
|
22
|
-
console.log(' [*] Waiting for messages. To exit press CTRL+C');
|
|
12
|
+
process.once('SIGINT', () => {
|
|
13
|
+
channel.close(() => {
|
|
14
|
+
connection.close();
|
|
23
15
|
});
|
|
24
16
|
});
|
|
25
|
-
}
|
|
26
17
|
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
channel.assertQueue(queue, { durable: false }, (err) => {
|
|
19
|
+
if (err) return bail(err, connection);
|
|
20
|
+
channel.consume(queue, (message) => {
|
|
21
|
+
if (message) console.log(" [x] Received '%s'", message.content.toString());
|
|
22
|
+
else console.warn(' [x] Consumer cancelled');
|
|
23
|
+
}, { noAck: true }, (err) => {
|
|
24
|
+
if (err) return bail(err, connection);
|
|
25
|
+
console.log(" [*] Waiting for logs. To exit press CTRL+C.");
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
});
|
|
29
30
|
|
|
30
|
-
|
|
31
|
+
function bail(err, connection) {
|
|
32
|
+
console.error(err);
|
|
33
|
+
if (connection) connection.close(() => {
|
|
34
|
+
process.exit(1);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -1,36 +1,42 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const amqp = require('amqplib/callback_api');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
console.error(err);
|
|
7
|
-
if (conn) conn.close(function() { process.exit(1); });
|
|
8
|
-
}
|
|
5
|
+
const exchange = 'logs';
|
|
9
6
|
|
|
10
|
-
|
|
11
|
-
if (err
|
|
12
|
-
|
|
7
|
+
amqp.connect((err, connection) => {
|
|
8
|
+
if (err) return bail(err);
|
|
9
|
+
connection.createChannel((err, channel) => {
|
|
10
|
+
if (err) return bail(err, connection);
|
|
13
11
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (err !== null) return bail(err, conn);
|
|
18
|
-
ch.assertQueue('', {exclusive: true}, function(err, ok) {
|
|
19
|
-
var q = ok.queue;
|
|
20
|
-
ch.bindQueue(q, ex, '');
|
|
21
|
-
ch.consume(q, logMessage, {noAck: true}, function(err, ok) {
|
|
22
|
-
if (err !== null) return bail(err, conn);
|
|
23
|
-
console.log(" [*] Waiting for logs. To exit press CTRL+C.");
|
|
12
|
+
process.once('SIGINT', () => {
|
|
13
|
+
channel.close(() => {
|
|
14
|
+
connection.close();
|
|
24
15
|
});
|
|
25
16
|
});
|
|
26
|
-
}
|
|
27
17
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
18
|
+
channel.assertExchange(exchange, 'fanout', { durable: false }, (err, { queue }) => {
|
|
19
|
+
if (err) return bail(err, connection);
|
|
20
|
+
channel.assertQueue('', { exclusive: true }, (err, { queue }) => {
|
|
21
|
+
if (err) return bail(err, connection);
|
|
22
|
+
channel.bindQueue(queue, exchange, '', {}, (err) => {
|
|
23
|
+
if (err) return bail(err, connection);
|
|
24
|
+
channel.consume(queue, (message) => {
|
|
25
|
+
if (message) console.log(" [x] '%s'", message.content.toString());
|
|
26
|
+
else console.warn(' [x] Consumer cancelled');
|
|
27
|
+
}, { noAck: true }, (err) => {
|
|
28
|
+
if (err) return bail(err, connection);
|
|
29
|
+
console.log(" [*] Waiting for logs. To exit press CTRL+C.");
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
32
36
|
|
|
33
|
-
|
|
37
|
+
function bail(err, connection) {
|
|
38
|
+
console.error(err);
|
|
39
|
+
if (connection) connection.close(() => {
|
|
40
|
+
process.exit(1);
|
|
41
|
+
});
|
|
34
42
|
}
|
|
35
|
-
|
|
36
|
-
amqp.connect(on_connect);
|
|
@@ -1,56 +1,57 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const amqp = require('amqplib/callback_api');
|
|
4
|
+
const { basename } = require('path');
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var severities = process.argv.slice(2);
|
|
6
|
+
const exchange = 'direct_logs';
|
|
7
|
+
const severities = process.argv.slice(2);
|
|
8
8
|
if (severities.length < 1) {
|
|
9
|
-
console.log('Usage %s [info] [warning] [error]',
|
|
10
|
-
basename(process.argv[1]));
|
|
9
|
+
console.log('Usage %s [info] [warning] [error]', basename(process.argv[1]));
|
|
11
10
|
process.exit(1);
|
|
12
11
|
}
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
function on_connect(err, conn) {
|
|
20
|
-
if (err !== null) return bail(err);
|
|
21
|
-
process.once('SIGINT', function() { conn.close(); });
|
|
22
|
-
|
|
23
|
-
conn.createChannel(function(err, ch) {
|
|
24
|
-
if (err !== null) return bail(err, conn);
|
|
25
|
-
var ex = 'direct_logs', exopts = {durable: false};
|
|
26
|
-
|
|
27
|
-
ch.assertExchange(ex, 'direct', exopts);
|
|
28
|
-
ch.assertQueue('', {exclusive: true}, function(err, ok) {
|
|
29
|
-
if (err !== null) return bail(err, conn);
|
|
30
|
-
|
|
31
|
-
var queue = ok.queue, i = 0;
|
|
13
|
+
amqp.connect((err, connection) => {
|
|
14
|
+
if (err) return bail(err);
|
|
15
|
+
connection.createChannel((err, channel) => {
|
|
16
|
+
if (err) return bail(err, connection);
|
|
32
17
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
}
|
|
18
|
+
process.once('SIGINT', () => {
|
|
19
|
+
channel.close(() => {
|
|
20
|
+
connection.close();
|
|
21
|
+
});
|
|
22
|
+
});
|
|
40
23
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
24
|
+
channel.assertExchange(exchange, 'direct', { durable: false }, (err) => {
|
|
25
|
+
if (err) return bail(err, connection);
|
|
26
|
+
channel.assertQueue('', { exclusive: true }, (err, { queue }) => {
|
|
27
|
+
if (err) return bail(err, connection);
|
|
28
|
+
channel.consume(queue, (message) => {
|
|
29
|
+
if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
|
|
30
|
+
else console.warn(' [x] Consumer cancelled');
|
|
31
|
+
}, {noAck: true}, function(err) {
|
|
32
|
+
if (err) return bail(err, connection);
|
|
33
|
+
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
|
|
34
|
+
subscribeAll(channel, queue, severities, (err) => {
|
|
35
|
+
if (err) return bail(err, connection);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
45
38
|
});
|
|
46
39
|
});
|
|
47
40
|
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
function subscribeAll(channel, queue, bindingKeys, cb) {
|
|
44
|
+
if (bindingKeys.length === 0) return cb();
|
|
45
|
+
const bindingKey = bindingKeys.shift();
|
|
46
|
+
channel.bindQueue(queue, exchange, bindingKey, {}, (err) => {
|
|
47
|
+
if (err) return cb(err);
|
|
48
|
+
subscribeAll(channel, queue, bindingKeys, cb);
|
|
49
|
+
});
|
|
48
50
|
}
|
|
49
51
|
|
|
50
|
-
function
|
|
51
|
-
console.
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
function bail(err, connection) {
|
|
53
|
+
console.error(err);
|
|
54
|
+
if (connection) connection.close(() => {
|
|
55
|
+
process.exit(1);
|
|
56
|
+
});
|
|
54
57
|
}
|
|
55
|
-
|
|
56
|
-
amqp.connect(on_connect);
|
|
@@ -1,55 +1,58 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const amqp = require('amqplib/callback_api');
|
|
4
|
+
const { basename } = require('path');
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
const exchange = 'topic_logs';
|
|
7
|
+
const severities = process.argv.slice(2);
|
|
8
|
+
if (severities.length < 1) {
|
|
9
|
+
console.log('Usage %s [info] [warning] [error]', basename(process.argv[1]));
|
|
10
10
|
process.exit(1);
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function on_connect(err, conn) {
|
|
19
|
-
if (err !== null) return bail(err);
|
|
20
|
-
process.once('SIGINT', function() { conn.close(); });
|
|
21
|
-
|
|
22
|
-
conn.createChannel(function(err, ch) {
|
|
23
|
-
if (err !== null) return bail(err, conn);
|
|
24
|
-
var ex = 'topic_logs', exopts = {durable: false};
|
|
25
|
-
|
|
26
|
-
ch.assertExchange(ex, 'topic', exopts);
|
|
27
|
-
ch.assertQueue('', {exclusive: true}, function(err, ok) {
|
|
28
|
-
if (err !== null) return bail(err, conn);
|
|
29
|
-
|
|
30
|
-
var queue = ok.queue, i = 0;
|
|
13
|
+
amqp.connect((err, connection) => {
|
|
14
|
+
if (err) return bail(err);
|
|
15
|
+
connection.createChannel((err, channel) => {
|
|
16
|
+
if (err) return bail(err, connection);
|
|
31
17
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
}
|
|
18
|
+
process.once('SIGINT', () => {
|
|
19
|
+
channel.close(() => {
|
|
20
|
+
connection.close();
|
|
21
|
+
});
|
|
22
|
+
});
|
|
39
23
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
24
|
+
channel.assertExchange(exchange, 'topic', { durable: false }, (err) => {
|
|
25
|
+
if (err) return bail(err, connection);
|
|
26
|
+
channel.assertQueue('', { exclusive: true }, (err, { queue }) => {
|
|
27
|
+
if (err) return bail(err, connection);
|
|
28
|
+
channel.consume(queue, (message) => {
|
|
29
|
+
if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
|
|
30
|
+
else console.warn(' [x] Consumer cancelled');
|
|
31
|
+
}, {noAck: true}, function(err) {
|
|
32
|
+
if (err) return bail(err, connection);
|
|
33
|
+
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
|
|
34
|
+
subscribeAll(channel, queue, severities, (err) => {
|
|
35
|
+
if (err) return bail(err, connection);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
44
38
|
});
|
|
45
39
|
});
|
|
46
40
|
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
function subscribeAll(channel, queue, bindingKeys, cb) {
|
|
44
|
+
if (bindingKeys.length === 0) return cb();
|
|
45
|
+
const bindingKey = bindingKeys.shift();
|
|
46
|
+
channel.bindQueue(queue, exchange, bindingKey, {}, (err) => {
|
|
47
|
+
if (err) return cb(err);
|
|
48
|
+
subscribeAll(channel, queue, bindingKeys, cb);
|
|
49
|
+
});
|
|
47
50
|
}
|
|
48
51
|
|
|
49
|
-
function
|
|
50
|
-
console.
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
function bail(err, connection) {
|
|
53
|
+
console.error(err);
|
|
54
|
+
if (connection) connection.close(() => {
|
|
55
|
+
process.exit(1);
|
|
56
|
+
});
|
|
53
57
|
}
|
|
54
58
|
|
|
55
|
-
amqp.connect(on_connect);
|
|
@@ -1,49 +1,51 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
catch (e) {
|
|
13
|
-
console.error(e);
|
|
3
|
+
const amqp = require('amqplib/callback_api');
|
|
4
|
+
const { basename } = require('path');
|
|
5
|
+
const { v4: uuid } = require('uuid');
|
|
6
|
+
|
|
7
|
+
const queue = 'rpc_queue';
|
|
8
|
+
|
|
9
|
+
const n = parseInt(process.argv[2], 10);
|
|
10
|
+
if (isNaN(n)) {
|
|
14
11
|
console.warn('Usage: %s number', basename(process.argv[1]));
|
|
15
12
|
process.exit(1);
|
|
16
13
|
}
|
|
17
14
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
amqp.connect((err, connection) => {
|
|
16
|
+
if (err) return bail(err);
|
|
17
|
+
connection.createChannel((err, channel) => {
|
|
18
|
+
if (err) return bail(err, connection);
|
|
19
|
+
channel.assertQueue('', { exclusive: true }, (err, { queue: replyTo }) => {
|
|
20
|
+
if (err) return bail(err, connection);
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
console.log(' [x] Requesting fib(%d)', n);
|
|
42
|
-
ch.sendToQueue('rpc_queue', Buffer.from(n.toString()), {
|
|
43
|
-
replyTo: queue, correlationId: correlationId
|
|
22
|
+
const correlationId = uuid();
|
|
23
|
+
channel.consume(replyTo, (message) => {
|
|
24
|
+
if (!message) console.warn(' [x] Consumer cancelled');
|
|
25
|
+
else if (message.properties.correlationId === correlationId) {
|
|
26
|
+
console.log(' [.] Got %d', message.content.toString());
|
|
27
|
+
channel.close(() => {
|
|
28
|
+
connection.close();
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
}, { noAck: true });
|
|
32
|
+
|
|
33
|
+
channel.assertQueue(queue, { durable: false }, (err) => {
|
|
34
|
+
if (err) return bail(err, connection);
|
|
35
|
+
console.log(' [x] Requesting fib(%d)', n);
|
|
36
|
+
channel.sendToQueue(queue, Buffer.from(n.toString()), {
|
|
37
|
+
correlationId,
|
|
38
|
+
replyTo
|
|
39
|
+
});
|
|
44
40
|
});
|
|
45
41
|
});
|
|
46
42
|
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
function bail(err, connection) {
|
|
46
|
+
console.error(err);
|
|
47
|
+
if (connection) connection.close(() => {
|
|
48
|
+
process.exit(1);
|
|
49
|
+
});
|
|
47
50
|
}
|
|
48
51
|
|
|
49
|
-
amqp.connect(on_connect);
|
|
@@ -1,45 +1,55 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const amqp = require('amqplib/callback_api');
|
|
4
|
+
|
|
5
|
+
const queue = 'rpc_queue';
|
|
6
|
+
|
|
7
|
+
amqp.connect((err, connection) => {
|
|
8
|
+
if (err) return bail(err);
|
|
9
|
+
connection.createChannel((err, channel) => {
|
|
10
|
+
if (err) return bail(err, connection);
|
|
11
|
+
|
|
12
|
+
process.once('SIGINT', () => {
|
|
13
|
+
channel.close(() => {
|
|
14
|
+
connection.close();
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
channel.assertQueue(queue, { durable: false }, (err) => {
|
|
19
|
+
if (err) return bail(err, connection);
|
|
20
|
+
channel.prefetch(1);
|
|
21
|
+
channel.consume(queue, (message) => {
|
|
22
|
+
const n = parseInt(message.content.toString(), 10);
|
|
23
|
+
console.log(' [.] fib(%d)', n);
|
|
24
|
+
const response = fib(n);
|
|
25
|
+
channel.sendToQueue(message.properties.replyTo, Buffer.from(response.toString()), {
|
|
26
|
+
correlationId: message.properties.correlationId
|
|
27
|
+
});
|
|
28
|
+
channel.ack(message);
|
|
29
|
+
}, { noAck: false }, function(err) {
|
|
30
|
+
if (err) return bail(err, conn);
|
|
31
|
+
console.log(' [x] Awaiting RPC requests. To exit press CTRL+C.');
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
4
36
|
|
|
5
37
|
function fib(n) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
38
|
+
// Do it the ridiculous, but not most ridiculous, way. For better,
|
|
39
|
+
// see http://nayuki.eigenstate.org/page/fast-fibonacci-algorithms
|
|
40
|
+
let a = 0, b = 1;
|
|
41
|
+
for (let i=0; i < n; i++) {
|
|
42
|
+
let c = a + b;
|
|
9
43
|
a = b; b = c;
|
|
10
44
|
}
|
|
11
45
|
return a;
|
|
12
46
|
}
|
|
13
47
|
|
|
14
|
-
function bail(err, conn) {
|
|
15
|
-
console.error(err);
|
|
16
|
-
if (conn) conn.close(function() { process.exit(1); });
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function on_connect(err, conn) {
|
|
20
|
-
if (err !== null) return bail(err);
|
|
21
|
-
|
|
22
|
-
process.once('SIGINT', function() { conn.close(); });
|
|
23
|
-
|
|
24
|
-
var q = 'rpc_queue';
|
|
25
48
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (err !== null) return bail(err, conn);
|
|
31
|
-
console.log(' [x] Awaiting RPC requests');
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
function reply(msg) {
|
|
35
|
-
var n = parseInt(msg.content.toString());
|
|
36
|
-
console.log(' [.] fib(%d)', n);
|
|
37
|
-
ch.sendToQueue(msg.properties.replyTo,
|
|
38
|
-
Buffer.from(fib(n).toString()),
|
|
39
|
-
{correlationId: msg.properties.correlationId});
|
|
40
|
-
ch.ack(msg);
|
|
41
|
-
}
|
|
49
|
+
function bail(err, connection) {
|
|
50
|
+
console.error(err);
|
|
51
|
+
if (connection) connection.close(() => {
|
|
52
|
+
process.exit(1);
|
|
42
53
|
});
|
|
43
54
|
}
|
|
44
55
|
|
|
45
|
-
amqp.connect(on_connect);
|
|
@@ -1,29 +1,28 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const amqp = require('amqplib/callback_api');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
if (conn) conn.close(function() { process.exit(1); });
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function on_connect(err, conn) {
|
|
11
|
-
if (err !== null) return bail(err);
|
|
5
|
+
const queue = 'hello';
|
|
6
|
+
const text = 'Hello World!';
|
|
12
7
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
8
|
+
amqp.connect((err, connection) => {
|
|
9
|
+
if (err) return bail(err);
|
|
10
|
+
connection.createChannel((err, channel) => {
|
|
11
|
+
if (err) return bail(err, connection);
|
|
12
|
+
channel.assertQueue(queue, { durable: false }, (err) => {
|
|
13
|
+
if (err) return bail(err, connection);
|
|
14
|
+
channel.sendToQueue(queue, Buffer.from(text));
|
|
15
|
+
console.log(" [x] Sent '%s'", text);
|
|
16
|
+
channel.close(() => {
|
|
17
|
+
connection.close();
|
|
18
|
+
});
|
|
23
19
|
});
|
|
24
|
-
}
|
|
20
|
+
});
|
|
21
|
+
});
|
|
25
22
|
|
|
26
|
-
|
|
23
|
+
function bail(err, connection) {
|
|
24
|
+
console.error(err);
|
|
25
|
+
if (connection) connection.close(() => {
|
|
26
|
+
process.exit(1);
|
|
27
|
+
});
|
|
27
28
|
}
|
|
28
|
-
|
|
29
|
-
amqp.connect(on_connect);
|