amqplib 0.4.1 → 0.5.2
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/CHANGELOG.md +51 -0
- package/Makefile +1 -1
- package/README.md +26 -17
- package/callback_api.js +1 -0
- package/channel_api.js +7 -6
- package/examples/headers.js +2 -2
- package/examples/receive_generator.js +39 -0
- package/examples/send_generators.js +42 -0
- package/examples/ssl.js +1 -1
- package/examples/tutorials/README.md +1 -1
- package/examples/tutorials/callback_api/emit_log.js +1 -1
- package/examples/tutorials/callback_api/emit_log_direct.js +1 -1
- package/examples/tutorials/callback_api/emit_log_topic.js +1 -1
- package/examples/tutorials/callback_api/new_task.js +1 -1
- package/examples/tutorials/callback_api/rpc_client.js +1 -1
- package/examples/tutorials/callback_api/rpc_server.js +1 -1
- package/examples/tutorials/callback_api/send.js +1 -1
- package/examples/tutorials/emit_log.js +4 -5
- package/examples/tutorials/emit_log_direct.js +4 -5
- package/examples/tutorials/emit_log_topic.js +4 -5
- package/examples/tutorials/new_task.js +5 -6
- package/examples/tutorials/package.json +2 -3
- package/examples/tutorials/receive.js +4 -4
- package/examples/tutorials/receive_logs.js +1 -1
- package/examples/tutorials/receive_logs_direct.js +2 -2
- package/examples/tutorials/receive_logs_topic.js +6 -6
- package/examples/tutorials/rpc_client.js +25 -26
- package/examples/tutorials/rpc_server.js +2 -2
- package/examples/tutorials/send.js +10 -6
- package/examples/tutorials/worker.js +1 -1
- package/examples/waitForConfirms.js +2 -2
- package/lib/callback_model.js +13 -12
- package/lib/channel.js +2 -2
- package/lib/channel_model.js +58 -66
- package/lib/codec.js +64 -38
- package/lib/connect.js +33 -12
- package/lib/connection.js +7 -6
- package/lib/credentials.js +6 -3
- package/lib/defs.js +75 -75
- package/lib/frame.js +2 -1
- package/lib/heartbeat.js +1 -1
- package/package.json +4 -3
- package/test/callback_api.js +6 -5
- package/test/channel.js +52 -46
- package/test/channel_api.js +125 -136
- package/test/codec.js +48 -9
- package/test/connect.js +63 -3
- package/test/connection.js +10 -9
- package/test/data.js +32 -9
- package/test/frame.js +4 -4
- package/test/util.js +33 -19
- package/.npmignore +0 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,56 @@
|
|
|
1
1
|
# Change log for amqplib
|
|
2
2
|
|
|
3
|
+
## Changes in v0.5.2
|
|
4
|
+
|
|
5
|
+
git log v0.5.1..v0.5.2
|
|
6
|
+
|
|
7
|
+
* Increase encoding buffer to accommodate large header values
|
|
8
|
+
([PR 367](https://github.com/squaremo/amqp.node/pull/367))
|
|
9
|
+
* Bring code up to date with new Buffer interface
|
|
10
|
+
([PR 350](https://github.com/squaremo/amqp.node/pull/350))
|
|
11
|
+
* Fix dangling connection problem
|
|
12
|
+
([PR 340](https://github.com/squaremo/amqp.node/pull/340))
|
|
13
|
+
* Clear up URL credentials parsing
|
|
14
|
+
([PR 330](https://github.com/squaremo/amqp.node/pull/330))
|
|
15
|
+
* Allow connection params to be suppied in object
|
|
16
|
+
([PR 304](https://github.com/squaremo/amqp.node/pull/304))
|
|
17
|
+
* Support explicit numeric types in field tables (e.g., headers)
|
|
18
|
+
([PR 389](https://github.com/squaremo/amqp.node/pull/389), from a
|
|
19
|
+
suggestion in
|
|
20
|
+
[issue 358](https://github.com/squaremo/amqp.node/issues/358))
|
|
21
|
+
|
|
22
|
+
Thank you to all contributors, of PRs, issues and comments.
|
|
23
|
+
|
|
24
|
+
## Changes in v0.5.1
|
|
25
|
+
|
|
26
|
+
git log v0.5.0..v0.5.1
|
|
27
|
+
|
|
28
|
+
* Fix mistake in closeBecause
|
|
29
|
+
([PR 298](https://github.com/squaremo/amqp.node/pull/298); thanks
|
|
30
|
+
to @lholznagel and others who reported the issue, and to @nfantone
|
|
31
|
+
for the rapid fix)
|
|
32
|
+
|
|
33
|
+
## Changes in v0.5.0
|
|
34
|
+
|
|
35
|
+
git log v0.4.2..v0.5.0
|
|
36
|
+
|
|
37
|
+
* Port to use bluebird rather than when.js
|
|
38
|
+
([PR 295](https://github.com/squaremo/amqp.node/pull/295); thanks
|
|
39
|
+
to @nfantone, and special mention to @myndzi for #158)
|
|
40
|
+
* Fixed a problem with using `channel.get` in the callback model
|
|
41
|
+
([PR 283](https://github.com/squaremo/amqp.node/pull/283); good
|
|
42
|
+
catch, @shanksauce)
|
|
43
|
+
* Added an example that uses generators (thanks @rudijs)
|
|
44
|
+
* Fixed a link in the comments relating to heartbeats (thanks
|
|
45
|
+
@tapickell)
|
|
46
|
+
|
|
47
|
+
## Changes in v0.4.2
|
|
48
|
+
|
|
49
|
+
git log v0.4.1..v0.4.2
|
|
50
|
+
|
|
51
|
+
* Better documentation and examples
|
|
52
|
+
* Replace uses of ES6 keyword 'await'
|
|
53
|
+
|
|
3
54
|
## Changes in v0.4.1
|
|
4
55
|
|
|
5
56
|
git log v0.4.0..v0.4.1
|
package/Makefile
CHANGED
|
@@ -2,7 +2,7 @@ RABBITMQ_SRC_VERSION=rabbitmq_v3_2_1
|
|
|
2
2
|
JSON=amqp-rabbitmq-0.9.1.json
|
|
3
3
|
RABBITMQ_CODEGEN=https://raw.githubusercontent.com/rabbitmq/rabbitmq-codegen
|
|
4
4
|
AMQP_JSON=$(RABBITMQ_CODEGEN)/$(RABBITMQ_SRC_VERSION)/$(JSON)
|
|
5
|
-
NODEJS_VERSIONS='0.8' '0.9' '0.10' '0.11' '0.12' '1.6' '2.5' '3.3' '4.2' '5.5'
|
|
5
|
+
NODEJS_VERSIONS='0.8' '0.9' '0.10' '0.11' '0.12' '1.6' '2.5' '3.3' '4.2' '5.5' '6.2' '8.9' '9.1'
|
|
6
6
|
|
|
7
7
|
MOCHA=./node_modules/.bin/mocha
|
|
8
8
|
_MOCHA=./node_modules/.bin/_mocha
|
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* [Examples from RabbitMQ tutorials][tutes]
|
|
11
11
|
|
|
12
12
|
A library for making AMQP 0-9-1 clients for Node.JS, and an AMQP 0-9-1
|
|
13
|
-
client for Node.JS v0.8-12, v4
|
|
13
|
+
client for Node.JS v0.8-0.12, v4-v9, and the intervening io.js
|
|
14
14
|
releases.
|
|
15
15
|
|
|
16
16
|
This library does not implement [AMQP
|
|
@@ -21,6 +21,7 @@ Project status:
|
|
|
21
21
|
|
|
22
22
|
- Expected to work
|
|
23
23
|
- Complete high-level and low-level APIs (i.e., all bits of the protocol)
|
|
24
|
+
- Stable APIs
|
|
24
25
|
- A fair few tests
|
|
25
26
|
- Measured test coverage
|
|
26
27
|
- Ports of the [RabbitMQ tutorials][rabbitmq-tutes] as [examples][tutes]
|
|
@@ -29,7 +30,6 @@ Project status:
|
|
|
29
30
|
Still working on:
|
|
30
31
|
|
|
31
32
|
- Getting to 100% (or very close to 100%) test coverage
|
|
32
|
-
- Settling on completely stable APIs
|
|
33
33
|
|
|
34
34
|
## Callback API example
|
|
35
35
|
|
|
@@ -70,7 +70,7 @@ require('amqplib/callback_api')
|
|
|
70
70
|
.connect('amqp://localhost', function(err, conn) {
|
|
71
71
|
if (err != null) bail(err);
|
|
72
72
|
consumer(conn);
|
|
73
|
-
publisher(conn);
|
|
73
|
+
publisher(conn);
|
|
74
74
|
});
|
|
75
75
|
```
|
|
76
76
|
|
|
@@ -83,36 +83,44 @@ var open = require('amqplib').connect('amqp://localhost');
|
|
|
83
83
|
|
|
84
84
|
// Publisher
|
|
85
85
|
open.then(function(conn) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
ch.sendToQueue(q, new Buffer('something to do'));
|
|
86
|
+
return conn.createChannel();
|
|
87
|
+
}).then(function(ch) {
|
|
88
|
+
return ch.assertQueue(q).then(function(ok) {
|
|
89
|
+
return ch.sendToQueue(q, new Buffer('something to do'));
|
|
90
90
|
});
|
|
91
|
-
|
|
92
|
-
}).then(null, console.warn);
|
|
91
|
+
}).catch(console.warn);
|
|
93
92
|
|
|
94
93
|
// Consumer
|
|
95
94
|
open.then(function(conn) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
ch.consume(q, function(msg) {
|
|
95
|
+
return conn.createChannel();
|
|
96
|
+
}).then(function(ch) {
|
|
97
|
+
return ch.assertQueue(q).then(function(ok) {
|
|
98
|
+
return ch.consume(q, function(msg) {
|
|
100
99
|
if (msg !== null) {
|
|
101
100
|
console.log(msg.content.toString());
|
|
102
101
|
ch.ack(msg);
|
|
103
102
|
}
|
|
104
103
|
});
|
|
105
104
|
});
|
|
106
|
-
|
|
107
|
-
}).then(null, console.warn);
|
|
105
|
+
}).catch(console.warn);
|
|
108
106
|
```
|
|
109
107
|
|
|
110
108
|
## Running tests
|
|
111
109
|
|
|
112
110
|
npm test
|
|
113
111
|
|
|
114
|
-
|
|
115
|
-
|
|
112
|
+
To run the tests RabbitMQ is required. Either install it with your package
|
|
113
|
+
manager, or use [docker][] to run a RabbitMQ instance.
|
|
114
|
+
|
|
115
|
+
docker run -d --name amqp.test -p 5672:5672 rabbitmq
|
|
116
|
+
|
|
117
|
+
If prefer not to run RabbitMQ locally it is also possible to use a
|
|
118
|
+
instance of RabbitMQ hosted elsewhere. Use the `URL` environment
|
|
119
|
+
variable to configure a different amqp host to connect to. You may
|
|
120
|
+
also need to do this if docker is not on localhost; e.g., if it's
|
|
121
|
+
running in docker-machine.
|
|
122
|
+
|
|
123
|
+
One public host is dev.rabbitmq.com:
|
|
116
124
|
|
|
117
125
|
URL=amqp://dev.rabbitmq.com npm test
|
|
118
126
|
|
|
@@ -146,3 +154,4 @@ really only useful for checking the kind and formatting of the errors.
|
|
|
146
154
|
[tutes]: https://github.com/squaremo/amqp.node/tree/master/examples/tutorials
|
|
147
155
|
[rabbitmq-tutes]: http://www.rabbitmq.com/getstarted.html
|
|
148
156
|
[changelog]: https://github.com/squaremo/amqp.node/blob/master/CHANGELOG.md
|
|
157
|
+
[docker]: https://www.docker.com/
|
package/callback_api.js
CHANGED
package/channel_api.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
var raw_connect = require('./lib/connect').connect;
|
|
2
2
|
var ChannelModel = require('./lib/channel_model').ChannelModel;
|
|
3
|
-
var
|
|
3
|
+
var Promise = require('bluebird');
|
|
4
4
|
|
|
5
5
|
function connect(url, connOptions) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
return Promise.fromCallback(function(cb) {
|
|
7
|
+
return raw_connect(url, connOptions, cb);
|
|
8
|
+
})
|
|
9
|
+
.then(function(conn) {
|
|
10
|
+
return new ChannelModel(conn);
|
|
10
11
|
});
|
|
11
|
-
return opened.promise;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
module.exports.connect = connect;
|
|
15
15
|
module.exports.credentials = require('./lib/credentials');
|
|
16
|
+
module.exports.IllegalOperationError = require('./lib/error').IllegalOperationError;
|
package/examples/headers.js
CHANGED
|
@@ -37,6 +37,6 @@ function bindAndConsume(ch, ex, q) {
|
|
|
37
37
|
|
|
38
38
|
function send(ch, ex) {
|
|
39
39
|
// The headers for a message are given as an option to `publish`:
|
|
40
|
-
ch.publish(ex.exchange, '',
|
|
41
|
-
ch.publish(ex.exchange, '',
|
|
40
|
+
ch.publish(ex.exchange, '', Buffer.from('hello'), {headers: {baz: 'boo'}});
|
|
41
|
+
ch.publish(ex.exchange, '', Buffer.from('world'), {headers: {foo: 'bar'}});
|
|
42
42
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
const co = require('co');
|
|
5
|
+
const amqp = require('amqplib');
|
|
6
|
+
const readline = require('readline');
|
|
7
|
+
|
|
8
|
+
co(function* () {
|
|
9
|
+
const myConsumer = (msg) => {
|
|
10
|
+
if (msg !== null) {
|
|
11
|
+
console.log('consuming message %s in generator', JSON.stringify(msg.content.toString()));
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
const conn = yield amqp.connect('amqp://localhost');
|
|
15
|
+
try {
|
|
16
|
+
// create a message to consume
|
|
17
|
+
const q = 'hello';
|
|
18
|
+
const msg = 'Hello World!';
|
|
19
|
+
const channel = yield conn.createChannel();
|
|
20
|
+
yield channel.assertQueue(q);
|
|
21
|
+
channel.sendToQueue(q, Buffer.from(msg));
|
|
22
|
+
console.log(" [x] Sent '%s'", msg);
|
|
23
|
+
// consume the message
|
|
24
|
+
yield channel.consume(q, myConsumer, { noAck: true });
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
throw e;
|
|
28
|
+
}
|
|
29
|
+
}).catch(err => {
|
|
30
|
+
console.warn('Error:', err);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const rl = readline.createInterface({
|
|
34
|
+
input: process.stdin,
|
|
35
|
+
output: process.stdout
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// pend until message is consumed
|
|
39
|
+
rl.question('newline to exit', () => process.exit());
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
// NB this requires the module 'co':
|
|
6
|
+
// npm install co
|
|
7
|
+
const co = require('co');
|
|
8
|
+
const amqp = require('amqplib');
|
|
9
|
+
|
|
10
|
+
co(function* () {
|
|
11
|
+
// connection errors are handled in the co .catch handler
|
|
12
|
+
const conn = yield amqp.connect('amqp://localhost');
|
|
13
|
+
|
|
14
|
+
// try catch will throw any errors from the yielding the following promises to the co .catch handler
|
|
15
|
+
try {
|
|
16
|
+
const q = 'hello';
|
|
17
|
+
const msg = 'Hello World!';
|
|
18
|
+
|
|
19
|
+
// use a confirm channel so we can check the message is sent OK.
|
|
20
|
+
const channel = yield conn.createConfirmChannel();
|
|
21
|
+
|
|
22
|
+
yield channel.assertQueue(q);
|
|
23
|
+
|
|
24
|
+
channel.sendToQueue(q, Buffer.from(msg));
|
|
25
|
+
|
|
26
|
+
// if message has been nacked, this will result in an error (rejected promise);
|
|
27
|
+
yield channel.waitForConfirms();
|
|
28
|
+
|
|
29
|
+
console.log(" [x] Sent '%s'", msg);
|
|
30
|
+
|
|
31
|
+
channel.close();
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
throw e;
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
conn.close();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
}).catch(err => {
|
|
41
|
+
console.warn('Error:', err);
|
|
42
|
+
});
|
package/examples/ssl.js
CHANGED
|
@@ -59,6 +59,6 @@ var open = amqp.connect('amqps://localhost', opts);
|
|
|
59
59
|
open.then(function(conn) {
|
|
60
60
|
process.on('SIGINT', conn.close.bind(conn));
|
|
61
61
|
return conn.createChannel().then(function(ch) {
|
|
62
|
-
ch.sendToQueue('foo',
|
|
62
|
+
ch.sendToQueue('foo', Buffer.from('Hello World!'));
|
|
63
63
|
});
|
|
64
64
|
}).then(null, console.warn);
|
|
@@ -9,7 +9,7 @@ of the tutorial programs to the callback-oriented API.
|
|
|
9
9
|
To run the tutorial code, you need amqplib installed. Assuming you are
|
|
10
10
|
in a clone of the amqplib repository, from the tutorials directory:
|
|
11
11
|
|
|
12
|
-
npm install
|
|
12
|
+
npm install
|
|
13
13
|
|
|
14
14
|
or to use the latest released version,
|
|
15
15
|
|
|
@@ -17,7 +17,7 @@ function on_connect(err, conn) {
|
|
|
17
17
|
ch.assertExchange(ex, 'fanout', {durable: false});
|
|
18
18
|
var msg = process.argv.slice(2).join(' ') ||
|
|
19
19
|
'info: Hello World!';
|
|
20
|
-
ch.publish(ex, '',
|
|
20
|
+
ch.publish(ex, '', Buffer.from(msg));
|
|
21
21
|
console.log(" [x] Sent '%s'", msg);
|
|
22
22
|
ch.close(function() { conn.close(); });
|
|
23
23
|
}
|
|
@@ -20,7 +20,7 @@ function on_connect(err, conn) {
|
|
|
20
20
|
function on_channel_open(err, ch) {
|
|
21
21
|
if (err !== null) return bail(err, conn);
|
|
22
22
|
ch.assertExchange(ex, 'direct', exopts, function(err, ok) {
|
|
23
|
-
ch.publish(ex, severity,
|
|
23
|
+
ch.publish(ex, severity, Buffer.from(message));
|
|
24
24
|
ch.close(function() { conn.close(); });
|
|
25
25
|
});
|
|
26
26
|
}
|
|
@@ -17,7 +17,7 @@ function on_connect(err, conn) {
|
|
|
17
17
|
conn.createChannel(function(err, ch) {
|
|
18
18
|
ch.assertExchange(ex, 'topic', exopts, function(err, ok) {
|
|
19
19
|
if (err !== null) return bail(err, conn);
|
|
20
|
-
ch.publish(ex, key,
|
|
20
|
+
ch.publish(ex, key, Buffer.from(message));
|
|
21
21
|
console.log(" [x] Sent %s:'%s'", key, message);
|
|
22
22
|
ch.close(function() { conn.close(); });
|
|
23
23
|
});
|
|
@@ -17,7 +17,7 @@ function on_connect(err, conn) {
|
|
|
17
17
|
ch.assertQueue(q, {durable: true}, function(err, _ok) {
|
|
18
18
|
if (err !== null) return bail(err, conn);
|
|
19
19
|
var msg = process.argv.slice(2).join(' ') || "Hello World!";
|
|
20
|
-
ch.sendToQueue(q,
|
|
20
|
+
ch.sendToQueue(q, Buffer.from(msg), {persistent: true});
|
|
21
21
|
console.log(" [x] Sent '%s'", msg);
|
|
22
22
|
ch.close(function() { conn.close(); });
|
|
23
23
|
});
|
|
@@ -39,7 +39,7 @@ function on_connect(err, conn) {
|
|
|
39
39
|
var queue = ok.queue;
|
|
40
40
|
ch.consume(queue, maybeAnswer, {noAck:true});
|
|
41
41
|
console.log(' [x] Requesting fib(%d)', n);
|
|
42
|
-
ch.sendToQueue('rpc_queue',
|
|
42
|
+
ch.sendToQueue('rpc_queue', Buffer.from(n.toString()), {
|
|
43
43
|
replyTo: queue, correlationId: correlationId
|
|
44
44
|
});
|
|
45
45
|
});
|
|
@@ -35,7 +35,7 @@ function on_connect(err, conn) {
|
|
|
35
35
|
var n = parseInt(msg.content.toString());
|
|
36
36
|
console.log(' [.] fib(%d)', n);
|
|
37
37
|
ch.sendToQueue(msg.properties.replyTo,
|
|
38
|
-
|
|
38
|
+
Buffer.from(fib(n).toString()),
|
|
39
39
|
{correlationId: msg.properties.correlationId});
|
|
40
40
|
ch.ack(msg);
|
|
41
41
|
}
|
|
@@ -17,7 +17,7 @@ function on_connect(err, conn) {
|
|
|
17
17
|
if (err !== null) return bail(err, conn);
|
|
18
18
|
ch.assertQueue(q, {durable: false}, function(err, ok) {
|
|
19
19
|
if (err !== null) return bail(err, conn);
|
|
20
|
-
ch.sendToQueue(q,
|
|
20
|
+
ch.sendToQueue(q, Buffer.from(msg));
|
|
21
21
|
console.log(" [x] Sent '%s'", msg);
|
|
22
22
|
ch.close(function() { conn.close(); });
|
|
23
23
|
});
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
var amqp = require('amqplib');
|
|
4
|
-
var when = require('when');
|
|
5
4
|
|
|
6
5
|
amqp.connect('amqp://localhost').then(function(conn) {
|
|
7
|
-
return
|
|
6
|
+
return conn.createChannel().then(function(ch) {
|
|
8
7
|
var ex = 'logs';
|
|
9
8
|
var ok = ch.assertExchange(ex, 'fanout', {durable: false})
|
|
10
9
|
|
|
@@ -12,9 +11,9 @@ amqp.connect('amqp://localhost').then(function(conn) {
|
|
|
12
11
|
'info: Hello World!';
|
|
13
12
|
|
|
14
13
|
return ok.then(function() {
|
|
15
|
-
ch.publish(ex, '',
|
|
14
|
+
ch.publish(ex, '', Buffer.from(message));
|
|
16
15
|
console.log(" [x] Sent '%s'", message);
|
|
17
16
|
return ch.close();
|
|
18
17
|
});
|
|
19
|
-
})
|
|
20
|
-
}).
|
|
18
|
+
}).finally(function() { conn.close(); });
|
|
19
|
+
}).catch(console.warn);
|
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
var amqp = require('amqplib');
|
|
4
|
-
var when = require('when');
|
|
5
4
|
|
|
6
5
|
var args = process.argv.slice(2);
|
|
7
6
|
var severity = (args.length > 0) ? args[0] : 'info';
|
|
8
7
|
var message = args.slice(1).join(' ') || 'Hello World!';
|
|
9
8
|
|
|
10
9
|
amqp.connect('amqp://localhost').then(function(conn) {
|
|
11
|
-
return
|
|
10
|
+
return conn.createChannel().then(function(ch) {
|
|
12
11
|
var ex = 'direct_logs';
|
|
13
12
|
var ok = ch.assertExchange(ex, 'direct', {durable: false});
|
|
14
13
|
|
|
15
14
|
return ok.then(function() {
|
|
16
|
-
ch.publish(ex, severity,
|
|
15
|
+
ch.publish(ex, severity, Buffer.from(message));
|
|
17
16
|
console.log(" [x] Sent %s:'%s'", severity, message);
|
|
18
17
|
return ch.close();
|
|
19
18
|
});
|
|
20
|
-
})
|
|
21
|
-
}).
|
|
19
|
+
}).finally(function() { conn.close(); });
|
|
20
|
+
}).catch(console.warn);
|
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
var amqp = require('amqplib');
|
|
4
|
-
var when = require('when');
|
|
5
4
|
|
|
6
5
|
var args = process.argv.slice(2);
|
|
7
6
|
var key = (args.length > 0) ? args[0] : 'info';
|
|
8
7
|
var message = args.slice(1).join(' ') || 'Hello World!';
|
|
9
8
|
|
|
10
9
|
amqp.connect('amqp://localhost').then(function(conn) {
|
|
11
|
-
return
|
|
10
|
+
return conn.createChannel().then(function(ch) {
|
|
12
11
|
var ex = 'topic_logs';
|
|
13
12
|
var ok = ch.assertExchange(ex, 'topic', {durable: false});
|
|
14
13
|
return ok.then(function() {
|
|
15
|
-
ch.publish(ex, key,
|
|
14
|
+
ch.publish(ex, key, Buffer.from(message));
|
|
16
15
|
console.log(" [x] Sent %s:'%s'", key, message);
|
|
17
16
|
return ch.close();
|
|
18
17
|
});
|
|
19
|
-
})
|
|
20
|
-
}).
|
|
18
|
+
}).finally(function() { conn.close(); })
|
|
19
|
+
}).catch(console.log);
|
|
@@ -2,18 +2,17 @@
|
|
|
2
2
|
// Post a new task to the work queue
|
|
3
3
|
|
|
4
4
|
var amqp = require('amqplib');
|
|
5
|
-
var when = require('when');
|
|
6
5
|
|
|
7
6
|
amqp.connect('amqp://localhost').then(function(conn) {
|
|
8
|
-
return
|
|
7
|
+
return conn.createChannel().then(function(ch) {
|
|
9
8
|
var q = 'task_queue';
|
|
10
9
|
var ok = ch.assertQueue(q, {durable: true});
|
|
11
|
-
|
|
10
|
+
|
|
12
11
|
return ok.then(function() {
|
|
13
12
|
var msg = process.argv.slice(2).join(' ') || "Hello World!";
|
|
14
|
-
ch.sendToQueue(q,
|
|
13
|
+
ch.sendToQueue(q, Buffer.from(msg), {deliveryMode: true});
|
|
15
14
|
console.log(" [x] Sent '%s'", msg);
|
|
16
15
|
return ch.close();
|
|
17
16
|
});
|
|
18
|
-
})
|
|
19
|
-
}).
|
|
17
|
+
}).finally(function() { conn.close(); });
|
|
18
|
+
}).catch(console.warn);
|
|
@@ -4,9 +4,8 @@
|
|
|
4
4
|
"description": "The RabbitMQ tutorials, ported to amqplib",
|
|
5
5
|
"main": "send.js",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"amqplib": "",
|
|
8
|
-
"
|
|
9
|
-
"node-uuid": ""
|
|
7
|
+
"amqplib": "../..",
|
|
8
|
+
"node-uuid": "*"
|
|
10
9
|
},
|
|
11
10
|
"scripts": {
|
|
12
11
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
@@ -5,17 +5,17 @@ var amqp = require('amqplib');
|
|
|
5
5
|
amqp.connect('amqp://localhost').then(function(conn) {
|
|
6
6
|
process.once('SIGINT', function() { conn.close(); });
|
|
7
7
|
return conn.createChannel().then(function(ch) {
|
|
8
|
-
|
|
8
|
+
|
|
9
9
|
var ok = ch.assertQueue('hello', {durable: false});
|
|
10
|
-
|
|
10
|
+
|
|
11
11
|
ok = ok.then(function(_qok) {
|
|
12
12
|
return ch.consume('hello', function(msg) {
|
|
13
13
|
console.log(" [x] Received '%s'", msg.content.toString());
|
|
14
14
|
}, {noAck: true});
|
|
15
15
|
});
|
|
16
|
-
|
|
16
|
+
|
|
17
17
|
return ok.then(function(_consumeOk) {
|
|
18
18
|
console.log(' [*] Waiting for messages. To exit press CTRL+C');
|
|
19
19
|
});
|
|
20
20
|
});
|
|
21
|
-
}).
|
|
21
|
+
}).catch(console.warn);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
var amqp = require('amqplib');
|
|
4
|
-
var all = require('
|
|
4
|
+
var all = require('bluebird').all;
|
|
5
5
|
var basename = require('path').basename;
|
|
6
6
|
|
|
7
7
|
var severities = process.argv.slice(2);
|
|
@@ -42,4 +42,4 @@ amqp.connect('amqp://localhost').then(function(conn) {
|
|
|
42
42
|
msg.content.toString());
|
|
43
43
|
}
|
|
44
44
|
});
|
|
45
|
-
}).
|
|
45
|
+
}).catch(console.warn);
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var amqp = require('amqplib');
|
|
4
4
|
var basename = require('path').basename;
|
|
5
|
-
var all = require('
|
|
5
|
+
var all = require('bluebird').all;
|
|
6
6
|
|
|
7
7
|
var keys = process.argv.slice(2);
|
|
8
8
|
if (keys.length < 1) {
|
|
@@ -16,29 +16,29 @@ amqp.connect('amqp://localhost').then(function(conn) {
|
|
|
16
16
|
return conn.createChannel().then(function(ch) {
|
|
17
17
|
var ex = 'topic_logs';
|
|
18
18
|
var ok = ch.assertExchange(ex, 'topic', {durable: false});
|
|
19
|
-
|
|
19
|
+
|
|
20
20
|
ok = ok.then(function() {
|
|
21
21
|
return ch.assertQueue('', {exclusive: true});
|
|
22
22
|
});
|
|
23
|
-
|
|
23
|
+
|
|
24
24
|
ok = ok.then(function(qok) {
|
|
25
25
|
var queue = qok.queue;
|
|
26
26
|
return all(keys.map(function(rk) {
|
|
27
27
|
ch.bindQueue(queue, ex, rk);
|
|
28
28
|
})).then(function() { return queue; });
|
|
29
29
|
});
|
|
30
|
-
|
|
30
|
+
|
|
31
31
|
ok = ok.then(function(queue) {
|
|
32
32
|
return ch.consume(queue, logMessage, {noAck: true});
|
|
33
33
|
});
|
|
34
34
|
return ok.then(function() {
|
|
35
35
|
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
|
|
36
36
|
});
|
|
37
|
-
|
|
37
|
+
|
|
38
38
|
function logMessage(msg) {
|
|
39
39
|
console.log(" [x] %s:'%s'",
|
|
40
40
|
msg.fields.routingKey,
|
|
41
41
|
msg.content.toString());
|
|
42
42
|
}
|
|
43
43
|
});
|
|
44
|
-
}).
|
|
44
|
+
}).catch(console.warn);
|
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var amqp = require('amqplib');
|
|
4
4
|
var basename = require('path').basename;
|
|
5
|
-
var
|
|
6
|
-
var defer = when.defer;
|
|
5
|
+
var Promise = require('bluebird');
|
|
7
6
|
var uuid = require('node-uuid');
|
|
8
7
|
|
|
9
8
|
// I've departed from the form of the original RPC tutorial, which
|
|
@@ -22,33 +21,33 @@ catch (e) {
|
|
|
22
21
|
}
|
|
23
22
|
|
|
24
23
|
amqp.connect('amqp://localhost').then(function(conn) {
|
|
25
|
-
return
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
return conn.createChannel().then(function(ch) {
|
|
25
|
+
return new Promise(function(resolve) {
|
|
26
|
+
var corrId = uuid();
|
|
27
|
+
function maybeAnswer(msg) {
|
|
28
|
+
if (msg.properties.correlationId === corrId) {
|
|
29
|
+
resolve(msg.content.toString());
|
|
30
|
+
}
|
|
31
31
|
}
|
|
32
|
-
}
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
var ok = ch.assertQueue('', {exclusive: true})
|
|
34
|
+
.then(function(qok) { return qok.queue; });
|
|
36
35
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
ok = ok.then(function(queue) {
|
|
43
|
-
console.log(' [x] Requesting fib(%d)', n);
|
|
44
|
-
ch.sendToQueue('rpc_queue', new Buffer(n.toString()), {
|
|
45
|
-
correlationId: corrId, replyTo: queue
|
|
36
|
+
ok = ok.then(function(queue) {
|
|
37
|
+
return ch.consume(queue, maybeAnswer, {noAck: true})
|
|
38
|
+
.then(function() { return queue; });
|
|
46
39
|
});
|
|
47
|
-
return answer.promise;
|
|
48
|
-
});
|
|
49
40
|
|
|
50
|
-
|
|
51
|
-
|
|
41
|
+
ok = ok.then(function(queue) {
|
|
42
|
+
console.log(' [x] Requesting fib(%d)', n);
|
|
43
|
+
ch.sendToQueue('rpc_queue', Buffer.from(n.toString()), {
|
|
44
|
+
correlationId: corrId, replyTo: queue
|
|
45
|
+
});
|
|
46
|
+
});
|
|
52
47
|
});
|
|
53
|
-
})
|
|
54
|
-
|
|
48
|
+
})
|
|
49
|
+
.then(function(fibN) {
|
|
50
|
+
console.log(' [.] Got %d', fibN);
|
|
51
|
+
})
|
|
52
|
+
.finally(function() { conn.close(); });
|
|
53
|
+
}).catch(console.warn);
|