-
Notifications
You must be signed in to change notification settings - Fork 7
/
producer.js
79 lines (53 loc) · 1.41 KB
/
producer.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* @Description: 描述
* @Author: 吴文周
* @Github: http://gitlab.yzf.net/wuwenzhou
* @Date: 2020-07-20 16:14:32
* @LastEditors: 吴文周
* @LastEditTime: 2020-07-20 16:45:47
*/
let amqp = require('amqplib/callback_api');
class Producer {
constructor() {
this.hosts = ["amqp://www.wuwenzhou.com.cn:15672"];
this.index = 0;
this.length = this.hosts.length;
this.open = amqp.connect(this.hosts[this.index]);
}
sendQueueMsg(queueName, msg, errCallBack) {
let self = this;
self.open
.then(function (conn) {
return conn.createChannel();
})
.then(function (channel) {
return channel.assertQueue(queueName).then(function (ok) {
return channel.sendToQueue(queueName, new Buffer(msg), {
persistent: true
});
})
.then(function (data) {
if (data) {
errCallBack && errCallBack("success");
channel.close();
}
})
.catch(function () {
setTimeout(() => {
if (channel) {
channel.close();
}
}, 500)
});
})
.catch(function () {
let num = self.index++;
if (num <= self.length - 1) {
self.open = amqp.connect(self.hosts[num]);
} else {
self.index == 0;
}
});
}
}
module.exports = Producer;