amqplib 0.10.3 → 0.10.5
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 +7 -4
- package/.github/workflows/publish.yml +59 -0
- package/.github/workflows/test.yml +15 -2
- package/CHANGELOG.md +14 -1
- package/Makefile +22 -20
- package/README.md +1 -2
- package/bin/amqp-rabbitmq-0.9.1.json +483 -0
- 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 +26 -2
- package/lib/connection.js +544 -532
- package/lib/credentials.js +1 -1
- package/lib/defs.js +2568 -2457
- package/lib/format.js +0 -1
- package/lib/heartbeat.js +32 -29
- package/lib/mux.js +96 -99
- package/package.json +3 -5
- package/test/callback_api.js +182 -174
- package/test/channel_api.js +455 -445
- package/test/codec.js +15 -15
- package/test/data.js +15 -0
- package/test/frame.js +6 -7
- package/test/mux.js +1 -2
- package/test/util.js +1 -3
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// Post a new task to the work queue
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const amqp = require('amqplib');
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var q = 'task_queue';
|
|
9
|
-
var ok = ch.assertQueue(q, {durable: true});
|
|
6
|
+
const queue = 'task_queue';
|
|
7
|
+
const text = process.argv.slice(2).join(' ') || "Hello World!";
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
|
|
9
|
+
(async () => {
|
|
10
|
+
let connection;
|
|
11
|
+
try {
|
|
12
|
+
connection = await amqp.connect('amqp://localhost');
|
|
13
|
+
const channel = await connection.createChannel();
|
|
14
|
+
await channel.assertQueue(queue, { durable: true });
|
|
15
|
+
channel.sendToQueue(queue, Buffer.from(text), { persistent: true });
|
|
16
|
+
console.log(" [x] Sent '%s'", text);
|
|
17
|
+
await channel.close();
|
|
18
|
+
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
console.warn(err);
|
|
21
|
+
}
|
|
22
|
+
finally {
|
|
23
|
+
await connection.close();
|
|
24
|
+
};
|
|
25
|
+
})();
|
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const amqp = require('amqplib');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
process.once('SIGINT', function() { conn.close(); });
|
|
7
|
-
return conn.createChannel().then(function(ch) {
|
|
5
|
+
const queue = 'hello';
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
(async () => {
|
|
8
|
+
try {
|
|
9
|
+
const connection = await amqp.connect('amqp://localhost');
|
|
10
|
+
const channel = await connection.createChannel();
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}, {noAck: true});
|
|
12
|
+
process.once('SIGINT', async () => {
|
|
13
|
+
await channel.close();
|
|
14
|
+
await connection.close();
|
|
15
15
|
});
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
await channel.assertQueue(queue, { durable: false });
|
|
18
|
+
await channel.consume(queue, (message) => {
|
|
19
|
+
console.log(" [x] Received '%s'", message.content.toString());
|
|
20
|
+
}, { noAck: true });
|
|
21
|
+
|
|
22
|
+
console.log(' [*] Waiting for messages. To exit press CTRL+C');
|
|
23
|
+
} catch (err) {
|
|
24
|
+
console.warn(err);
|
|
25
|
+
}
|
|
26
|
+
})();
|
|
@@ -1,28 +1,30 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const amqp = require('amqplib');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
ok = ok.then(function(queue) {
|
|
18
|
-
return ch.consume(queue, logMessage, {noAck: true});
|
|
19
|
-
});
|
|
20
|
-
return ok.then(function() {
|
|
21
|
-
console.log(' [*] Waiting for logs. To exit press CTRL+C');
|
|
5
|
+
const exchange = 'logs';
|
|
6
|
+
|
|
7
|
+
(async () => {
|
|
8
|
+
try {
|
|
9
|
+
const connection = await amqp.connect('amqp://localhost');
|
|
10
|
+
const channel = await connection.createChannel();
|
|
11
|
+
|
|
12
|
+
process.once('SIGINT', async () => {
|
|
13
|
+
await channel.close();
|
|
14
|
+
await connection.close();
|
|
22
15
|
});
|
|
23
16
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
17
|
+
await channel.assertExchange(exchange, 'fanout', { durable: false });
|
|
18
|
+
const { queue } = await channel.assertQueue('', { exclusive: true });
|
|
19
|
+
await channel.bindQueue(queue, exchange, '')
|
|
20
|
+
|
|
21
|
+
await channel.consume(queue, (message) => {
|
|
22
|
+
if (message) console.log(" [x] '%s'", message.content.toString());
|
|
23
|
+
else console.warn(' [x] Consumer cancelled');
|
|
24
|
+
}, { noAck: true });
|
|
25
|
+
|
|
26
|
+
console.log(' [*] Waiting for logs. To exit press CTRL+C');
|
|
27
|
+
} catch (err) {
|
|
28
|
+
console.warn(err);
|
|
29
|
+
}
|
|
30
|
+
})();
|
|
@@ -1,44 +1,38 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const amqp = require('../..');
|
|
4
|
+
const { basename } = require('path');
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
const exchange = 'direct_logs';
|
|
7
|
+
const bindingKeys = process.argv.slice(2);
|
|
8
|
+
if (bindingKeys.length < 1) {
|
|
9
|
+
console.warn('Usage: %s [info] [warning] [error]', basename(process.argv[1]));
|
|
10
10
|
process.exit(1);
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
(async () => {
|
|
14
|
+
try {
|
|
15
|
+
const connection = await amqp.connect('amqp://localhost');
|
|
16
|
+
const channel = await connection.createChannel();
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return ch.assertQueue('', {exclusive: true});
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
ok = ok.then(function(qok) {
|
|
25
|
-
var queue = qok.queue;
|
|
26
|
-
return Promise.all(severities.map(function(sev) {
|
|
27
|
-
ch.bindQueue(queue, ex, sev);
|
|
28
|
-
})).then(function() { return queue; });
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
ok = ok.then(function(queue) {
|
|
32
|
-
return ch.consume(queue, logMessage, {noAck: true});
|
|
33
|
-
});
|
|
34
|
-
return ok.then(function() {
|
|
35
|
-
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
|
|
18
|
+
process.once('SIGINT', async () => {
|
|
19
|
+
await channel.close();
|
|
20
|
+
await connection.close();
|
|
36
21
|
});
|
|
37
22
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
23
|
+
await channel.assertExchange(exchange, 'direct', { durable: false });
|
|
24
|
+
const { queue } = await channel.assertQueue('', { exclusive: true });
|
|
25
|
+
await Promise.all(bindingKeys.map(async (bindingKey) => {
|
|
26
|
+
await channel.bindQueue(queue, exchange, bindingKey);
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
await channel.consume(queue, (message) => {
|
|
30
|
+
if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
|
|
31
|
+
else console.warn(' [x] Consumer cancelled');
|
|
32
|
+
}, { noAck: true });
|
|
33
|
+
|
|
34
|
+
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
|
|
35
|
+
} catch(err) {
|
|
36
|
+
console.warn(err);
|
|
37
|
+
}
|
|
38
|
+
})();
|
|
@@ -1,43 +1,39 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const amqp = require('../..');
|
|
4
|
+
const { basename } = require('path');
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
const exchange = 'topic_logs';
|
|
7
|
+
const bindingKeys = process.argv.slice(2);
|
|
8
|
+
if (bindingKeys.length < 1) {
|
|
9
|
+
console.log('Usage: %s pattern [pattern...]', basename(process.argv[1]));
|
|
10
10
|
process.exit(1);
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
var ok = ch.assertExchange(ex, 'topic', {durable: false});
|
|
13
|
+
(async () => {
|
|
14
|
+
try {
|
|
15
|
+
const connection = await amqp.connect('amqp://localhost');
|
|
16
|
+
const channel = await connection.createChannel();
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
process.once('SIGINT', async () => {
|
|
19
|
+
await channel.close();
|
|
20
|
+
await connection.close();
|
|
21
21
|
});
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
});
|
|
23
|
+
await channel.assertExchange(exchange, 'topic', { durable: false });
|
|
24
|
+
const { queue } = await channel.assertQueue('', { exclusive: true });
|
|
25
|
+
await Promise.all(bindingKeys.map(async (bindingKey) => {
|
|
26
|
+
await channel.bindQueue(queue, exchange, bindingKey);
|
|
27
|
+
}));
|
|
29
28
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
|
|
35
|
-
});
|
|
29
|
+
await channel.consume(queue, (message) => {
|
|
30
|
+
if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
|
|
31
|
+
else console.warn(' [x] Consumer cancelled');
|
|
32
|
+
}, { noAck: true });
|
|
36
33
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}).catch(console.warn);
|
|
34
|
+
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
console.warn(err);
|
|
38
|
+
}
|
|
39
|
+
})();
|
|
@@ -1,52 +1,49 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var n;
|
|
12
|
-
try {
|
|
13
|
-
if (process.argv.length < 3) throw Error('Too few args');
|
|
14
|
-
n = parseInt(process.argv[2]);
|
|
15
|
-
}
|
|
16
|
-
catch (e) {
|
|
17
|
-
console.error(e);
|
|
3
|
+
const amqp = require('amqplib');
|
|
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)) {
|
|
18
11
|
console.warn('Usage: %s number', basename(process.argv[1]));
|
|
19
12
|
process.exit(1);
|
|
20
13
|
}
|
|
21
14
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
resolve(msg.content.toString());
|
|
29
|
-
}
|
|
30
|
-
}
|
|
15
|
+
(async () => {
|
|
16
|
+
let connection;
|
|
17
|
+
try {
|
|
18
|
+
connection = await amqp.connect('amqp://localhost');
|
|
19
|
+
const channel = await connection.createChannel();
|
|
20
|
+
const correlationId = uuid();
|
|
31
21
|
|
|
32
|
-
|
|
33
|
-
|
|
22
|
+
const requestFib = new Promise(async (resolve) => {
|
|
23
|
+
const { queue: replyTo } = await channel.assertQueue('', { exclusive: true });
|
|
34
24
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
25
|
+
await channel.consume(replyTo, (message) => {
|
|
26
|
+
if (!message) console.warn(' [x] Consumer cancelled');
|
|
27
|
+
else if (message.properties.correlationId === correlationId) {
|
|
28
|
+
resolve(message.content.toString());
|
|
29
|
+
}
|
|
30
|
+
}, { noAck: true });
|
|
39
31
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
32
|
+
await channel.assertQueue(queue, { durable: false });
|
|
33
|
+
console.log(' [x] Requesting fib(%d)', n);
|
|
34
|
+
channel.sendToQueue(queue, Buffer.from(n.toString()), {
|
|
35
|
+
correlationId,
|
|
36
|
+
replyTo,
|
|
45
37
|
});
|
|
46
38
|
});
|
|
47
|
-
|
|
48
|
-
|
|
39
|
+
|
|
40
|
+
const fibN = await requestFib;
|
|
49
41
|
console.log(' [.] Got %d', fibN);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
42
|
+
}
|
|
43
|
+
catch (err) {
|
|
44
|
+
console.warn(err);
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
if (connection) await connection.close();
|
|
48
|
+
};
|
|
49
|
+
})();
|
|
@@ -1,39 +1,46 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const amqp = require('amqplib');
|
|
4
|
+
|
|
5
|
+
const queue = 'rpc_queue';
|
|
6
|
+
|
|
7
|
+
(async () => {
|
|
8
|
+
try {
|
|
9
|
+
const connection = await amqp.connect('amqp://localhost');
|
|
10
|
+
const channel = await connection.createChannel();
|
|
11
|
+
|
|
12
|
+
process.once('SIGINT', async () => {
|
|
13
|
+
await channel.close();
|
|
14
|
+
await connection.close();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
await channel.assertQueue(queue, { durable: false });
|
|
18
|
+
|
|
19
|
+
channel.prefetch(1);
|
|
20
|
+
await channel.consume(queue, (message) => {
|
|
21
|
+
const n = parseInt(message.content.toString(), 10);
|
|
22
|
+
console.log(' [.] fib(%d)', n);
|
|
23
|
+
const response = fib(n);
|
|
24
|
+
channel.sendToQueue(message.properties.replyTo, Buffer.from(response.toString()), {
|
|
25
|
+
correlationId: message.properties.correlationId
|
|
26
|
+
});
|
|
27
|
+
channel.ack(message);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
console.log(' [x] Awaiting RPC requests. To exit press CTRL+C.');
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
console.warn(err);
|
|
34
|
+
}
|
|
35
|
+
})();
|
|
4
36
|
|
|
5
37
|
function fib(n) {
|
|
6
38
|
// Do it the ridiculous, but not most ridiculous, way. For better,
|
|
7
39
|
// see http://nayuki.eigenstate.org/page/fast-fibonacci-algorithms
|
|
8
|
-
|
|
9
|
-
for (
|
|
10
|
-
|
|
40
|
+
let a = 0, b = 1;
|
|
41
|
+
for (let i=0; i < n; i++) {
|
|
42
|
+
let c = a + b;
|
|
11
43
|
a = b; b = c;
|
|
12
44
|
}
|
|
13
45
|
return a;
|
|
14
46
|
}
|
|
15
|
-
|
|
16
|
-
amqp.connect('amqp://localhost').then(function(conn) {
|
|
17
|
-
process.once('SIGINT', function() { conn.close(); });
|
|
18
|
-
return conn.createChannel().then(function(ch) {
|
|
19
|
-
var q = 'rpc_queue';
|
|
20
|
-
var ok = ch.assertQueue(q, {durable: false});
|
|
21
|
-
var ok = ok.then(function() {
|
|
22
|
-
ch.prefetch(1);
|
|
23
|
-
return ch.consume(q, reply);
|
|
24
|
-
});
|
|
25
|
-
return ok.then(function() {
|
|
26
|
-
console.log(' [x] Awaiting RPC requests');
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
function reply(msg) {
|
|
30
|
-
var n = parseInt(msg.content.toString());
|
|
31
|
-
console.log(' [.] fib(%d)', n);
|
|
32
|
-
var response = fib(n);
|
|
33
|
-
ch.sendToQueue(msg.properties.replyTo,
|
|
34
|
-
Buffer.from(response.toString()),
|
|
35
|
-
{correlationId: msg.properties.correlationId});
|
|
36
|
-
ch.ack(msg);
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
}).catch(console.warn);
|
|
@@ -1,23 +1,31 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const amqp = require('amqplib');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var q = 'hello';
|
|
8
|
-
var msg = 'Hello World!';
|
|
5
|
+
const queue = 'hello';
|
|
6
|
+
const text = 'Hello World!';
|
|
9
7
|
|
|
10
|
-
|
|
8
|
+
(async () => {
|
|
9
|
+
let connection;
|
|
10
|
+
try {
|
|
11
|
+
connection = await amqp.connect('amqp://localhost');
|
|
12
|
+
const channel = await connection.createChannel();
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
14
|
+
await channel.assertQueue(queue, { durable: false });
|
|
15
|
+
|
|
16
|
+
// NB: `sentToQueue` and `publish` both return a boolean
|
|
17
|
+
// indicating whether it's OK to send again straight away, or
|
|
18
|
+
// (when `false`) that you should wait for the event `'drain'`
|
|
19
|
+
// to fire before writing again. We're just doing the one write,
|
|
20
|
+
// so we'll ignore it.
|
|
21
|
+
channel.sendToQueue(queue, Buffer.from(text));
|
|
22
|
+
console.log(" [x] Sent '%s'", text);
|
|
23
|
+
await channel.close();
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
console.warn(err);
|
|
27
|
+
}
|
|
28
|
+
finally {
|
|
29
|
+
if (connection) await connection.close();
|
|
30
|
+
};
|
|
31
|
+
})();
|
|
@@ -1,28 +1,34 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// Process tasks from the work queue
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const amqp = require('amqplib');
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
console.log(" [*] Waiting for messages. To exit press CTRL+C");
|
|
6
|
+
const queue = 'task_queue';
|
|
7
|
+
|
|
8
|
+
(async () => {
|
|
9
|
+
try {
|
|
10
|
+
const connection = await amqp.connect('amqp://localhost');
|
|
11
|
+
process.once('SIGINT', async () => {
|
|
12
|
+
await connection.close();
|
|
14
13
|
});
|
|
15
|
-
return ok;
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
const channel = await connection.createChannel();
|
|
16
|
+
await channel.assertQueue(queue, { durable: true });
|
|
17
|
+
|
|
18
|
+
channel.prefetch(1);
|
|
19
|
+
await channel.consume(queue, (message) => {
|
|
20
|
+
const text = message.content.toString();
|
|
21
|
+
console.log(" [x] Received '%s'", text);
|
|
22
|
+
const seconds = text.split('.').length - 1;
|
|
23
|
+
setTimeout(() => {
|
|
23
24
|
console.log(" [x] Done");
|
|
24
|
-
|
|
25
|
-
},
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
channel.ack(message);
|
|
26
|
+
}, seconds * 1000);
|
|
27
|
+
}, { noAck: false });
|
|
28
|
+
|
|
29
|
+
console.log(" [*] Waiting for messages. To exit press CTRL+C");
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
console.warn(err);
|
|
33
|
+
}
|
|
34
|
+
})();
|
|
@@ -1,22 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
const amqp = require('../');
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
(async () => {
|
|
4
|
+
let connection;
|
|
5
|
+
try {
|
|
6
|
+
connection = await amqp.connect();
|
|
7
|
+
const channel = await connection.createConfirmChannel();
|
|
4
8
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
else { console.log('Message %d confirmed', i); }
|
|
9
|
-
} : null;
|
|
10
|
-
}
|
|
9
|
+
for (var i=0; i < 20; i++) {
|
|
10
|
+
channel.publish('amq.topic', 'whatever', Buffer.from('blah'));
|
|
11
|
+
};
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
});
|
|
22
|
-
});
|
|
13
|
+
await channel.waitForConfirms();
|
|
14
|
+
console.log('All messages done');
|
|
15
|
+
await channel.close();
|
|
16
|
+
} catch (err) {
|
|
17
|
+
console.warn(err);
|
|
18
|
+
} finally {
|
|
19
|
+
if (connection) await connection.close();
|
|
20
|
+
}
|
|
21
|
+
})();
|