12345678910111213141516171819202122232425262728293031323334353637 |
- '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), { noAck: true });
- } else {
- this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
- }
- }
- async logMessage(msg) {
- const result = msg.content.toString();
- const headers = msg.properties.headers;
- console.log(headers);
- }
- }
- module.exports = RabbitmqService;
|