Add prxrmq

This commit is contained in:
李科 2019-08-28 15:44:52 +08:00
parent 308c0f8af1
commit 0ddd6dd8c6
4 changed files with 184 additions and 7 deletions

67
utils/impl/prxrmq.js Normal file
View File

@ -0,0 +1,67 @@
'use strict';
const amqplib = require('amqplib');
function RabbitMQPrx(conf) {
this.param = {
protocol: 'amqp',
hostname: conf.host,
port: conf.port,
username: conf.user,
password: conf.passwd,
frameMax: 0,
heartbeat: 0,
vhost: conf.vhost
};
this.amqp = amqplib.connect(this.param);
this.produces = {};
this.consumes = {};
}
RabbitMQPrx.prototype.initProduce = async function(excName, excType, excArgs = {}) {
let produce = {};
produce.channel = await this.amqp.then(function (conn) {
return conn.createChannel().then( function(channel) {
return channel.assertExchange(excName, excType, excArgs).then(function(){
return channel;
});
});
});
this.produces[excName] = produce;
};
RabbitMQPrx.prototype.initConsume = async function(excName, excType, excArgs, queueName, queueArgs, routeKey, cb) {
let consumer = {};
consumer.channel = await this.amqp.then(function (conn) {
return conn.createChannel().then( function(channel) {
let ret = channel.assertExchange(excName, excType, excArgs);
ret = ret.then(function () {
return channel.assertQueue(queueName, queueArgs);
});
ret = ret.then(function(qok) {
for (let n in routeKey) {
if (routeKey.hasOwnProperty(n))
channel.bindQueue(qok.queue, excName, routeKey[n]);
}
return qok.queue;
});
ret = ret.then(function(queue) {
return channel.consume(queue, cb, {noAck: true});
});
return ret.then(function() {
return channel;
});
});
});
this.consumes[excName] = consumer;
};
RabbitMQPrx.prototype.publish = function(excName, routeKey, msg) {
this.produces[excName].channel.publish(excName, routeKey, Buffer.from(msg));
};
module.exports = RabbitMQPrx;

View File

@ -1,6 +1,4 @@
exports.yo = function() {
alert('Yo Coder!')
}
exports.hello = function() {
alert('Hello Coder!')
}
module.exports = {
prxrmq: require('./impl/prxrmq')
};

109
utils/package-lock.json generated Normal file
View File

@ -0,0 +1,109 @@
{
"name": "@ehason/utils",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"amqplib": {
"version": "0.5.5",
"resolved": "https://registry.npmjs.org/amqplib/-/amqplib-0.5.5.tgz",
"integrity": "sha512-sWx1hbfHbyKMw6bXOK2k6+lHL8TESWxjAx5hG8fBtT7wcxoXNIsFxZMnFyBjxt3yL14vn7WqBDe5U6BGOadtLg==",
"requires": {
"bitsyntax": "~0.1.0",
"bluebird": "^3.5.2",
"buffer-more-ints": "~1.0.0",
"readable-stream": "1.x >=1.1.9",
"safe-buffer": "~5.1.2",
"url-parse": "~1.4.3"
}
},
"bitsyntax": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/bitsyntax/-/bitsyntax-0.1.0.tgz",
"integrity": "sha512-ikAdCnrloKmFOugAfxWws89/fPc+nw0OOG1IzIE72uSOg/A3cYptKCjSUhDTuj7fhsJtzkzlv7l3b8PzRHLN0Q==",
"requires": {
"buffer-more-ints": "~1.0.0",
"debug": "~2.6.9",
"safe-buffer": "~5.1.2"
}
},
"bluebird": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.5.tgz",
"integrity": "sha512-5am6HnnfN+urzt4yfg7IgTbotDjIT/u8AJpEt0sIU9FtXfVeezXAPKswrG+xKUCOYAINpSdgZVDU6QFh+cuH3w=="
},
"buffer-more-ints": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/buffer-more-ints/-/buffer-more-ints-1.0.0.tgz",
"integrity": "sha512-EMetuGFz5SLsT0QTnXzINh4Ksr+oo4i+UGTXEshiGCQWnsgSs7ZhJ8fzlwQ+OzEMs0MpDAMr1hxnblp5a4vcHg=="
},
"core-util-is": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
},
"debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"requires": {
"ms": "2.0.0"
}
},
"inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
},
"isarray": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz",
"integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8="
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
},
"querystringify": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.1.1.tgz",
"integrity": "sha512-w7fLxIRCRT7U8Qu53jQnJyPkYZIaR4n5151KMfcJlO/A9397Wxb1amJvROTK6TOnp7PfoAmg/qXiNHI+08jRfA=="
},
"readable-stream": {
"version": "1.1.14",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz",
"integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=",
"requires": {
"core-util-is": "~1.0.0",
"inherits": "~2.0.1",
"isarray": "0.0.1",
"string_decoder": "~0.10.x"
}
},
"requires-port": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
"integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8="
},
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
},
"string_decoder": {
"version": "0.10.31",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz",
"integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ="
},
"url-parse": {
"version": "1.4.7",
"resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.4.7.tgz",
"integrity": "sha512-d3uaVyzDB9tQoSXFvuSUNFibTd9zxd2bkVrDRvF5TmvWWQwqE4lgYJ5m+x1DbecWkw+LK4RNl2CU1hHuOKPVlg==",
"requires": {
"querystringify": "^2.1.1",
"requires-port": "^1.0.0"
}
}
}
}

View File

@ -7,5 +7,8 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "likeagle <likeagle@163.com>",
"license": "MIT"
"license": "MIT",
"dependencies": {
"amqplib": "^0.5.5"
}
}