-
Notifications
You must be signed in to change notification settings - Fork 7
/
c.js
47 lines (46 loc) · 1.29 KB
/
c.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* @Description: 描述
* @Author: 吴文周
* @Github: http://gitlab.yzf.net/wuwenzhou
* @Date: 2020-07-20 16:47:49
* @LastEditors: 吴文周
* @LastEditTime: 2020-07-20 18:16:27
*/
var q = 'tasks';
var open = require('amqplib').connect('amqp://admin:[email protected]:5672');
// Publisher
// for (let index = 0; index < 1000; index++) {
// open.then(function(conn) {
// return conn.createChannel();
// }).then(function(ch) {
// return ch.assertQueue(q).then(function(ok) {
// return ch.sendToQueue(q, Buffer.from(`something ${index} do`));
// });
// }).catch(console.warn);
// }
// open.then(function(conn) {
// return conn.createChannel();
// }).then(function(ch) {
// ch.prefetch(1);
// return ch.assertQueue(q).then(function(ok) {
// return ch.sendToQueue(q, Buffer.from('something toxxx do'));
// });
// }).catch(console.warn);
//Consumer
open.then(function(conn) {
return conn.createChannel();
}).then(function(ch) {
return ch.assertQueue(q).then(function(ok) {
ch.prefetch(1);
ch.consume(q, function(msg) {
if (msg !== null) {
console.log(msg.content.toString());
// ch.ack(msg);
setTimeout(function() {
console.log(" [x] Done");
ch.ack(msg);
}, 5000);
}
});
});
}).catch(console.warn);