|
@@ -1,5 +1,6 @@
|
|
|
'use strict';
|
|
|
-
|
|
|
+require('../util/constants');
|
|
|
+const sd = require('silly-datetime');
|
|
|
const Service = require('egg').Service;
|
|
|
|
|
|
class RabbitmqService extends Service {
|
|
@@ -11,36 +12,41 @@ class RabbitmqService extends Service {
|
|
|
}
|
|
|
|
|
|
|
|
|
- async sendQueueMsg(ex, routeKey, msg) {
|
|
|
+ async sendQueueMsg(ex, routeKey, msg, parm) {
|
|
|
const self = this;
|
|
|
const { mq } = self.ctx;
|
|
|
if (mq) {
|
|
|
- await mq.topic(ex, msg, routeKey, { durable: self.durable });
|
|
|
+ await mq.topic(ex, routeKey, msg, parm);
|
|
|
} else {
|
|
|
this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
- async receiveQueueMsg(ex, routeKey, receiveCallBack) {
|
|
|
+ async receiveQueueMsg(ex) {
|
|
|
const self = this;
|
|
|
const { mq } = self.ctx;
|
|
|
- console.log(mq);
|
|
|
- console.log(self.exType);
|
|
|
if (mq) {
|
|
|
const ch = await mq.conn.createChannel();
|
|
|
try {
|
|
|
await ch.assertExchange(ex, self.exType, { durable: self.durable });
|
|
|
- const q = await ch.assertQueue('', { exclusive: false });
|
|
|
+ const q = await ch.assertQueue('', { exclusive: true });
|
|
|
console.log('==q=', q);
|
|
|
|
|
|
- await ch.bindQueue(q.queue, ex, routeKey);
|
|
|
+ await ch.bindQueue(q.queue, ex, '*');
|
|
|
ch.consume(q.queue, msg => {
|
|
|
console.log('收到消息: ', msg);
|
|
|
-
|
|
|
- ch.ack(msg);
|
|
|
- receiveCallBack && receiveCallBack(msg);
|
|
|
- }, { noAck: false });
|
|
|
+ const result = msg.content.toString();
|
|
|
+
|
|
|
+
|
|
|
+ const headers = msg.properties.headers;
|
|
|
+
|
|
|
+ if (result != null) {
|
|
|
+
|
|
|
+ const updatetimes = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
|
|
|
+ this.service.message.create({ userid: headers.userId, name: headers.name, createtime: updatetimes, type: headers.type, content: result, remark: '别忘记了' });
|
|
|
+ }
|
|
|
+ }, { noAck: true });
|
|
|
} catch (e) {
|
|
|
console.log('==e==', e);
|
|
|
await ch.close();
|