home.js 1008 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. {
  15. console.log(data);
  16. });
  17. ctx.body = '发送成功';
  18. }
  19. async receivemq() {
  20. const { ctx, app } = this;
  21. await this.service.rabbitmq.receiveQueueMsg(ctx.query.exchange,ctx.query.routekey,(msg) =>
  22. {
  23. console.log(msg);
  24. // 插入待办事项到数据库中。
  25. await this.service.message.create({producerid: '1', consumerid: '2',type: '1', content: '2222'});
  26. });
  27. ctx.body = "接收成功";
  28. }
  29. }
  30. module.exports = HomeController;