rabbitmq.js 897 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class RabbitmqService extends Service {
  4. constructor(ctx) {
  5. super(ctx);
  6. this.exType = 'topic';
  7. this.durable = true;
  8. }
  9. // 接收消息
  10. async receiveQueueMsg(ex) {
  11. this.ctx.logger.info('调用mq的' + ex);
  12. const self = this;
  13. const { mq } = self.ctx;
  14. if (mq) {
  15. const ch = await mq.conn.createChannel();
  16. await ch.assertExchange(ex, 'topic', { durable: true });
  17. const q = await ch.assertQueue('', { exclusive: true });
  18. await ch.bindQueue(q.queue, ex, '*');
  19. await ch.consume(q.queue, msg => this.logMessage(msg, this), { noAck: true });
  20. } else {
  21. this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
  22. }
  23. }
  24. async logMessage(msg) {
  25. const result = msg.content.toString();
  26. const headers = msg.properties.headers;
  27. }
  28. }
  29. module.exports = RabbitmqService;