企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ### egg-amqplib 基于 rabbitmq 消息队列封装的库 文档:[https://github.com/zubincheung/egg-amqplib](https://github.com/zubincheung/egg-amqplib) 示例代码:[https://github.com/zubincheung/egg-amqplib/blob/master/test/fixtures/apps/amqplib-test/app/controller/home.js](https://github.com/zubincheung/egg-amqplib/blob/master/test/fixtures/apps/amqplib-test/app/controller/home.js) ``` 'use strict'; const Controller = require('egg').Controller; const queueName = 'test'; class HomeController extends Controller { async publish() { const { msg } = this.ctx.query; const ch = await this.app.amqplib.createChannel(); await ch.assertQueue(queueName, { durable: false }); const ok = await ch.sendToQueue(queueName, Buffer.from(msg)); await ch.close(); this.ctx.body = ok; this.ctx.status = 200; } async consume() { const ch = await this.app.amqplib.createChannel(); await ch.assertQueue(queueName, { durable: false }); const msg = await new Promise(resolve => ch.consume(queueName, msg => resolve(msg))); if (msg !== null) { ch.ack(msg); await ch.close(); this.ctx.status = 200; this.ctx.body = { msg: msg.content.toString() }; } else { this.ctx.status = 500; } } } module.exports = HomeController; ```