home.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. const amqp = require('amqplib');
  3. const msgValue = require('../util/constants');
  4. const Controller = require('egg').Controller;
  5. class HomeController extends Controller {
  6. async index() {
  7. const { ctx } = this;
  8. ctx.body = 'hi, egg';
  9. }
  10. async sendmq() {
  11. const { ctx, app } = this;
  12. console.log(msgValue.MsgValues.EXCHANGE_CROP_REG);
  13. await this.service.rabbitmq.sendQueueMsg(ctx.query.exchange, ctx.query.routekey, new Buffer(JSON.stringify(ctx.query.msg)), data => {
  14. console.log(data);
  15. });
  16. ctx.body = '发送成功';
  17. }
  18. async receivemq() {
  19. const { ctx, app } = this;
  20. await this.service.rabbitmq.receiveQueueMsg(ctx.query.exchange, ctx.query.routekey, msg => {
  21. console.log(msg);
  22. // 插入待办事项到数据库中。
  23. this.service.message.create({ producerid: msg.producerid, consumerid: msg.consumerid, type: msg.type, content: msg.content, remark: msg.remark });
  24. });
  25. ctx.body = '接收成功';
  26. }
  27. }
  28. module.exports = HomeController;