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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
| const amqplib = require('amqplib');
class RabbitMQChannel { constructor (options) { const { type, connect, channel, queue = '', exchange = '', routingKey = '', queueOptions = {}, exchangeOptions = {} } = options;
this.type = type; this.connect = connect; this.channel = channel; this.queue = queue; this.options = options; this.exchange = exchange; this.queueOptions = queueOptions; this.exchangeOptions = exchangeOptions; this.routingKey = routingKey;
if (!this.type || !this.connect || !this.channel || !this.queue) { throw new Error('illegal params'); }
if (this.channelOptions.prefetch) { this.prefetch(this.channelOptions.prefetch); } }
_handleMsgToBuffer (buffer) { if (typeof buffer === 'object' && !Buffer.isBuffer(buffer)) { const hasToString = buffer.toString && buffer.toString !== Object.prototype.toString && !Array.isArray(buffer); if (hasToString) { buffer = buffer.toString(); } else { buffer = JSON.stringify(buffer); } } if (typeof buffer === 'string') { buffer = Buffer.from(buffer); } return buffer; }
sendToQueue (buffer, sendOptions = {}) { const { queue } = sendOptions; this.channel.sendToQueue(queue || this.queue, this._handleMsgToBuffer(buffer), sendOptions); }
publish (buffer, publishOptions = {}) { let { routingKey, exchange } = publishOptions;
if (Array.isArray(this.routingKey) && this.routingKey.length !== 1 && !routingKey) { throw new Error('需要额外指定 options.routingKey'); } else { routingKey = routingKey || this.routingKey[0] || this.routingKey; }
exchange = exchange || this.exchange;
this.channel.publish(exchange || this.exchange, routingKey, this._handleMsgToBuffer(buffer), publishOptions); }
async consume (consumer, consumeOptions = { }) { if (!this.assertStatus) { await this.channel.assertQueue(this.queue, this.type, this.assertOptions); this.assertStatus = true; }
this.channel.consume(this.queue, async (msg) => { if (!msg) { return; }
const msgContent = msg.content;
await consumer(msgContent, msg);
if (!consumeOptions.noAck) { this.channel.ack(msg); } }, consumeOptions); }
async ackAll () { await this.channel.ackAll(); }
async nack (message, allUpto, requeue) { await this.channel.nack(message, allUpto, requeue); }
async nackAll (requeue) { await this.channel.nackAll(requeue); }
prefetch (number) { if (typeof number === 'number' && number >= 1) { this.channel.prefetch(parseInt(number)); } } }
class RabbitMQ { constructor (link = 'amqp://localhost') { const defaultLinkOptions = { protocol: 'amqp', hostname: 'localhost', port: 5672, username: 'guest', password: 'guest', locale: 'en_US', frameMax: 0, heartbeat: 0, vhost: '/' };
this.link = typeof link === 'string' ? link : Object.assign(defaultLinkOptions, link || {}); }
async connet (link) { const _link = link || this._link; this.connet = await amqplib.connect(_link); return this.connect(); }
async createChannel ( queue = '', exchange, type = 'direct', options = { queueOptions: {}, exchangeOptions: {} } ) { if (typeof exchange === 'string') { if (['direct', 'topic', 'headers', 'fanout', 'match'].indexOf(exchange) > -1) { if (typeof type === 'object' && !options) { options = type; }
type = exchange; exchange = null; } }
const { queueOptions, exchangeOptions, routingKey = '' } = options;
const channel = await this.connet.createChannel();
if (exchange) { if (queue === '' && queueOptions.exclusive === undefined) { queueOptions.exclusive = true; }
await channel.assertExchange(exchange, type, exchangeOptions);
const q = await channel.assertQueue(queue, type, queueOptions);
let routingKeys = routingKey;
if (!Array.isArray(routingKeys)) { routingKeys = [routingKeys || '']; }
routingKeys.forEach(routingKey => { channel.bindQueue(q.queue, exchange, routingKey); }); } else { await channel.assertQueue(queue, type, queueOptions); }
return new RabbitMQChannel(type, this.connet, channel, queue, exchange, options); }
async close () { if (this.connet) { this.connet.close(); } } }
|