liuyu há 5 anos atrás
pai
commit
2d7124308f
2 ficheiros alterados com 12 adições e 2 exclusões
  1. 9 1
      app/controller/home.js
  2. 3 1
      app/service/rabbitmq.js

+ 9 - 1
app/controller/home.js

@@ -13,7 +13,11 @@ class HomeController extends Controller {
   async sendmq() {
   async sendmq() {
     const { ctx, app } = this;
     const { ctx, app } = this;
     console.log(msgValue.MsgValues.EXCHANGE_CROP_REG);
     console.log(msgValue.MsgValues.EXCHANGE_CROP_REG);
-    await this.service.rabbitmq.sendQueueMsg(ctx.query.exchange,ctx.query.routekey, new Buffer(JSON.stringify(ctx.query.msg)));
+    await this.service.rabbitmq.sendQueueMsg(ctx.query.exchange,ctx.query.routekey, new Buffer(JSON.stringify(ctx.query.msg)), (data) => 
+    {    
+       console.log(data);
+       
+    });
     ctx.body = '发送成功';
     ctx.body = '发送成功';
   }
   }
 
 
@@ -22,6 +26,10 @@ class HomeController extends Controller {
     await this.service.rabbitmq.receiveQueueMsg(ctx.query.exchange,ctx.query.routekey,(msg) => 
     await this.service.rabbitmq.receiveQueueMsg(ctx.query.exchange,ctx.query.routekey,(msg) => 
     {    
     {    
        console.log(msg);
        console.log(msg);
+ 
+      // 插入待办事项到数据库中。
+        
+
     });
     });
     ctx.body = "接收成功";
     ctx.body = "接收成功";
   }
   }

+ 3 - 1
app/service/rabbitmq.js

@@ -27,7 +27,7 @@ class RabbitmqnewService extends Service {
   }
   }
 
 
   // 发送消息
   // 发送消息
-  async sendQueueMsg(queueName, routeKey, msg) {
+  async sendQueueMsg(queueName, routeKey, msg, sendCallBack) {
     const self = this;
     const self = this;
     const conn = await amqp.connect(self.hosts[self.index]);
     const conn = await amqp.connect(self.hosts[self.index]);
     const ch = await conn.createConfirmChannel();
     const ch = await conn.createConfirmChannel();
@@ -40,8 +40,10 @@ class RabbitmqnewService extends Service {
       console.log('==result==', result);
       console.log('==result==', result);
       if (result) {
       if (result) {
         console.log('发送成功');
         console.log('发送成功');
+        sendCallBack && sendCallBack(true);
       } else {
       } else {
         console.log('发送失败');
         console.log('发送失败');
+        sendCallBack && sendCallBack(false);
       }
       }
       await ch.close();
       await ch.close();
     } catch (e) {
     } catch (e) {