amqplib 0.4.0 → 0.5.1
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 +38 -0
- package/Makefile +1 -1
- package/README.md +27 -17
- package/channel_api.js +6 -6
- package/examples/headers.js +42 -0
- package/examples/send_generators.js +42 -0
- package/examples/tutorials/README.md +1 -1
- package/examples/tutorials/emit_log.js +3 -4
- package/examples/tutorials/emit_log_direct.js +3 -4
- package/examples/tutorials/emit_log_topic.js +3 -4
- package/examples/tutorials/new_task.js +4 -5
- 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 +1 -1
- package/examples/tutorials/send.js +9 -5
- package/examples/tutorials/worker.js +1 -1
- package/examples/waitForConfirms.js +2 -2
- package/lib/callback_model.js +13 -12
- package/lib/channel_model.js +58 -66
- package/lib/connection.js +19 -16
- package/lib/heartbeat.js +1 -1
- package/package.json +3 -3
- package/test/channel.js +37 -32
- package/test/channel_api.js +104 -116
- package/test/util.js +19 -19
package/CHANGELOG.md
CHANGED
|
@@ -1,9 +1,47 @@
|
|
|
1
1
|
# Change log for amqplib
|
|
2
2
|
|
|
3
|
+
## Changes in v0.5.1
|
|
4
|
+
|
|
5
|
+
git log v0.5.0..v0.5.1
|
|
6
|
+
|
|
7
|
+
* Fix mistake in closeBecause
|
|
8
|
+
([PR 298](https://github.com/squaremo/amqp.node/pull/298); thanks
|
|
9
|
+
to @lholznagel and others who reported the issue, and to @nfantone
|
|
10
|
+
for the rapid fix)
|
|
11
|
+
|
|
12
|
+
## Changes in v0.5.0
|
|
13
|
+
|
|
14
|
+
git log v0.4.2..v0.5.0
|
|
15
|
+
|
|
16
|
+
* Port to use bluebird rather than when.js
|
|
17
|
+
([PR 295](https://github.com/squaremo/amqp.node/pull/295); thanks
|
|
18
|
+
to @nfantone, and special mention to @myndzi for #158)
|
|
19
|
+
* Fixed a problem with using `channel.get` in the callback model
|
|
20
|
+
([PR 283](https://github.com/squaremo/amqp.node/pull/283); good
|
|
21
|
+
catch, @shanksauce)
|
|
22
|
+
* Added an example that uses generators (thanks @rudijs)
|
|
23
|
+
* Fixed a link in the comments relating to heartbeats (thanks
|
|
24
|
+
@tapickell)
|
|
25
|
+
|
|
26
|
+
## Changes in v0.4.2
|
|
27
|
+
|
|
28
|
+
git log v0.4.1..v0.4.2
|
|
29
|
+
|
|
30
|
+
* Better documentation and examples
|
|
31
|
+
* Replace uses of ES6 keyword 'await'
|
|
32
|
+
|
|
33
|
+
## Changes in v0.4.1
|
|
34
|
+
|
|
35
|
+
git log v0.4.0..v0.4.1
|
|
36
|
+
|
|
37
|
+
* Tested in Node.JS 0.8 through 4.2 and 5.5
|
|
38
|
+
* Emit an error with the 'close' event if server-initiated
|
|
39
|
+
|
|
3
40
|
## Changes in v0.4.0
|
|
4
41
|
|
|
5
42
|
git log v0.3.2..v0.4.0
|
|
6
43
|
|
|
44
|
+
* Tested on Node.JS 0.8 through 4.0 (and intervening io.js releases)
|
|
7
45
|
* Change meaning of 'b' fields in tables to match RabbitMQ (and AMQP
|
|
8
46
|
specification)
|
|
9
47
|
* Can now pass an object in place of connection URL
|
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.
|
|
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'
|
|
6
6
|
|
|
7
7
|
MOCHA=./node_modules/.bin/mocha
|
|
8
8
|
_MOCHA=./node_modules/.bin/_mocha
|
package/README.md
CHANGED
|
@@ -10,7 +10,8 @@
|
|
|
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,
|
|
13
|
+
client for Node.JS v0.8-12, v4.0, and the intervening io.js
|
|
14
|
+
releases.
|
|
14
15
|
|
|
15
16
|
This library does not implement [AMQP
|
|
16
17
|
1.0](https://github.com/squaremo/amqp.node/issues/63) or [AMQP
|
|
@@ -69,7 +70,7 @@ require('amqplib/callback_api')
|
|
|
69
70
|
.connect('amqp://localhost', function(err, conn) {
|
|
70
71
|
if (err != null) bail(err);
|
|
71
72
|
consumer(conn);
|
|
72
|
-
publisher(conn);
|
|
73
|
+
publisher(conn);
|
|
73
74
|
});
|
|
74
75
|
```
|
|
75
76
|
|
|
@@ -82,36 +83,44 @@ var open = require('amqplib').connect('amqp://localhost');
|
|
|
82
83
|
|
|
83
84
|
// Publisher
|
|
84
85
|
open.then(function(conn) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
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'));
|
|
89
90
|
});
|
|
90
|
-
|
|
91
|
-
}).then(null, console.warn);
|
|
91
|
+
}).catch(console.warn);
|
|
92
92
|
|
|
93
93
|
// Consumer
|
|
94
94
|
open.then(function(conn) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
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) {
|
|
99
99
|
if (msg !== null) {
|
|
100
100
|
console.log(msg.content.toString());
|
|
101
101
|
ch.ack(msg);
|
|
102
102
|
}
|
|
103
103
|
});
|
|
104
104
|
});
|
|
105
|
-
|
|
106
|
-
}).then(null, console.warn);
|
|
105
|
+
}).catch(console.warn);
|
|
107
106
|
```
|
|
108
107
|
|
|
109
108
|
## Running tests
|
|
110
109
|
|
|
111
110
|
npm test
|
|
112
111
|
|
|
113
|
-
|
|
114
|
-
|
|
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:
|
|
115
124
|
|
|
116
125
|
URL=amqp://dev.rabbitmq.com npm test
|
|
117
126
|
|
|
@@ -140,8 +149,9 @@ really only useful for checking the kind and formatting of the errors.
|
|
|
140
149
|
open file://`pwd`/coverage/lcov-report/index.html
|
|
141
150
|
|
|
142
151
|
[gh-pages]: http://squaremo.github.com/amqp.node/
|
|
143
|
-
[gh-pages-apiref]: http://squaremo.github.com/amqp.node/
|
|
152
|
+
[gh-pages-apiref]: http://squaremo.github.com/amqp.node/channel_api.html
|
|
144
153
|
[nave]: https://github.com/isaacs/nave
|
|
145
154
|
[tutes]: https://github.com/squaremo/amqp.node/tree/master/examples/tutorials
|
|
146
155
|
[rabbitmq-tutes]: http://www.rabbitmq.com/getstarted.html
|
|
147
156
|
[changelog]: https://github.com/squaremo/amqp.node/blob/master/CHANGELOG.md
|
|
157
|
+
[docker]: https://www.docker.com/
|
package/channel_api.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
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;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// Example of using a headers exchange
|
|
4
|
+
|
|
5
|
+
var amqp = require('../')
|
|
6
|
+
|
|
7
|
+
amqp.connect().then(function(conn) {
|
|
8
|
+
return conn.createChannel().then(withChannel);
|
|
9
|
+
}, console.error);
|
|
10
|
+
|
|
11
|
+
function withChannel(ch) {
|
|
12
|
+
// NB the type of the exchange is 'headers'
|
|
13
|
+
ch.assertExchange('matching exchange', 'headers').then(function(ex) {
|
|
14
|
+
ch.assertQueue().then(function(q) {
|
|
15
|
+
bindAndConsume(ch, ex, q).then(function() {
|
|
16
|
+
send(ch, ex);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function bindAndConsume(ch, ex, q) {
|
|
23
|
+
// When using a headers exchange, the headers to be matched go in
|
|
24
|
+
// the binding arguments. The routing key is ignore, so best left
|
|
25
|
+
// empty.
|
|
26
|
+
|
|
27
|
+
// 'x-match' is 'all' or 'any', meaning "all fields must match" or
|
|
28
|
+
// "at least one field must match", respectively. The values to be
|
|
29
|
+
// matched go in subsequent fields.
|
|
30
|
+
ch.bindQueue(q.queue, ex.exchange, '', {'x-match': 'any',
|
|
31
|
+
'foo': 'bar',
|
|
32
|
+
'baz': 'boo'});
|
|
33
|
+
return ch.consume(q.queue, function(msg) {
|
|
34
|
+
console.log(msg.content.toString());
|
|
35
|
+
}, {noAck: true});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function send(ch, ex) {
|
|
39
|
+
// The headers for a message are given as an option to `publish`:
|
|
40
|
+
ch.publish(ex.exchange, '', new Buffer('hello'), {headers: {baz: 'boo'}});
|
|
41
|
+
ch.publish(ex.exchange, '', new Buffer('world'), {headers: {foo: 'bar'}});
|
|
42
|
+
}
|
|
@@ -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, new Buffer(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
|
+
});
|
|
@@ -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
|
|
|
@@ -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
|
|
|
@@ -16,5 +15,5 @@ amqp.connect('amqp://localhost').then(function(conn) {
|
|
|
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,14 +1,13 @@
|
|
|
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
|
|
|
@@ -17,5 +16,5 @@ amqp.connect('amqp://localhost').then(function(conn) {
|
|
|
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,14 +1,13 @@
|
|
|
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() {
|
|
@@ -16,5 +15,5 @@ amqp.connect('amqp://localhost').then(function(conn) {
|
|
|
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
13
|
ch.sendToQueue(q, new Buffer(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', new Buffer(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);
|
|
@@ -1,19 +1,23 @@
|
|
|
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 q = 'hello';
|
|
9
8
|
var msg = 'Hello World!';
|
|
10
9
|
|
|
11
10
|
var ok = ch.assertQueue(q, {durable: false});
|
|
12
|
-
|
|
11
|
+
|
|
13
12
|
return ok.then(function(_qok) {
|
|
13
|
+
// NB: `sentToQueue` and `publish` both return a boolean
|
|
14
|
+
// indicating whether it's OK to send again straight away, or
|
|
15
|
+
// (when `false`) that you should wait for the event `'drain'`
|
|
16
|
+
// to fire before writing again. We're just doing the one write,
|
|
17
|
+
// so we'll ignore it.
|
|
14
18
|
ch.sendToQueue(q, new Buffer(msg));
|
|
15
19
|
console.log(" [x] Sent '%s'", msg);
|
|
16
20
|
return ch.close();
|
|
17
21
|
});
|
|
18
|
-
})
|
|
19
|
-
}).
|
|
22
|
+
}).finally(function() { conn.close(); });
|
|
23
|
+
}).catch(console.warn);
|
|
@@ -12,11 +12,11 @@ function mkCallback(i) {
|
|
|
12
12
|
amqp.connect().then(function(c) {
|
|
13
13
|
c.createConfirmChannel().then(function(ch) {
|
|
14
14
|
for (var i=0; i < NUM_MSGS; i++) {
|
|
15
|
-
ch.publish('amq.topic', 'whatever', new Buffer('blah'), {}, mkCallback(i));
|
|
15
|
+
ch.publish('amq.topic', 'whatever', new Buffer('blah'), {}, mkCallback(i));
|
|
16
16
|
}
|
|
17
17
|
ch.waitForConfirms().then(function() {
|
|
18
18
|
console.log('All messages done');
|
|
19
19
|
c.close();
|
|
20
|
-
}
|
|
20
|
+
}).catch(console.error);
|
|
21
21
|
});
|
|
22
22
|
});
|
package/lib/callback_model.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
'use strict';
|
|
6
6
|
|
|
7
7
|
var defs = require('./defs');
|
|
8
|
-
var
|
|
8
|
+
var Promise = require('bluebird');
|
|
9
9
|
var inherits = require('util').inherits;
|
|
10
10
|
var EventEmitter = require('events').EventEmitter;
|
|
11
11
|
var BaseChannel = require('./channel').BaseChannel;
|
|
@@ -215,7 +215,7 @@ Channel.prototype.get = function(queue, options, cb0) {
|
|
|
215
215
|
if (f.id === defs.BasicGetEmpty) {
|
|
216
216
|
cb(null, false);
|
|
217
217
|
}
|
|
218
|
-
else if (f.id
|
|
218
|
+
else if (f.id === defs.BasicGetOk) {
|
|
219
219
|
self.handleMessage = acceptMessage(function(m) {
|
|
220
220
|
m.fields = f.fields;
|
|
221
221
|
cb(null, m);
|
|
@@ -308,20 +308,21 @@ ConfirmChannel.prototype.sendToQueue = function(queue, content,
|
|
|
308
308
|
};
|
|
309
309
|
|
|
310
310
|
ConfirmChannel.prototype.waitForConfirms = function(k) {
|
|
311
|
-
var
|
|
311
|
+
var awaiting = [];
|
|
312
312
|
var unconfirmed = this.unconfirmed;
|
|
313
313
|
unconfirmed.forEach(function(val, index) {
|
|
314
314
|
if (val === null); // already confirmed
|
|
315
315
|
else {
|
|
316
|
-
var confirmed =
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
316
|
+
var confirmed = new Promise(function(resolve, reject) {
|
|
317
|
+
unconfirmed[index] = function(err) {
|
|
318
|
+
if (val) val(err);
|
|
319
|
+
if (err === null) resolve();
|
|
320
|
+
else reject(err);
|
|
321
|
+
};
|
|
322
|
+
});
|
|
323
|
+
awaiting.push(confirmed);
|
|
323
324
|
}
|
|
324
325
|
});
|
|
325
|
-
return
|
|
326
|
-
|
|
326
|
+
return Promise.all(awaiting).then(function() { k(); },
|
|
327
|
+
function(err) { k(err); });
|
|
327
328
|
};
|