forked from jwalton/node-amqp-connection-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.js
42 lines (37 loc) · 1.22 KB
/
sender.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
Promise = global.Promise || require('es6-promise').Promise;
var amqp = require('..');
var wait = require('../lib/helpers').wait;
var QUEUE_NAME = 'amqp-connection-manager-sample1'
// Create a connetion manager
var connection = amqp.connect(['amqp://localhost'], {json: true});
connection.on('connect', function() {
console.log('Connected!');
});
connection.on('disconnect', function(params) {
console.log('Disconnected.', params.err.stack);
});
// Create a channel wrapper
var channelWrapper = connection.createChannel({
json: true,
setup: function(channel) {
// `channel` here is a regular amqplib `ConfirmChannel`.
return channel.assertQueue(QUEUE_NAME, {durable: true});
}
});
// Send messages until someone hits CTRL-C or something goes wrong...
var sendMessage = function() {
channelWrapper.sendToQueue(QUEUE_NAME, {time: Date.now()})
.then(function() {
console.log("Message sent");
return wait(1000);
})
.then(function() {
return sendMessage();
}).catch(function(err) {
return console.log("Message was rejected:", err.stack);
channelWrapper.close();
connection.close();
});
};
console.log("Sending messages...");
sendMessage();