|
@@ -0,0 +1,39 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+const Service = require('egg').Service;
|
|
|
+
|
|
|
+class RabbitmqService extends Service {
|
|
|
+
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx);
|
|
|
+ this.exType = 'topic';
|
|
|
+ this.durable = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 接收消息
|
|
|
+ async receiveQueueMsg(ex) {
|
|
|
+ console.log(ex);
|
|
|
+ this.ctx.logger.info('调用mq的' + ex);
|
|
|
+ const self = this;
|
|
|
+ const { mq } = self.ctx;
|
|
|
+ if (mq) {
|
|
|
+ const ch = await mq.conn.createChannel();
|
|
|
+ await ch.assertExchange(ex, 'topic', { durable: true });
|
|
|
+ const q = await ch.assertQueue('', { exclusive: true });
|
|
|
+ await ch.bindQueue(q.queue, ex, '*');
|
|
|
+ await ch.consume(q.queue, msg => this.logMessage(msg, this), { autoAck: true });
|
|
|
+ } else {
|
|
|
+ this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async logMessage(msg) {
|
|
|
+
|
|
|
+ const result = msg.content.toString();
|
|
|
+ console.log(result);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = RabbitmqService;
|