微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

RabbitMQ消耗的消息值未返回到调用方法

如何解决RabbitMQ消耗的消息值未返回到调用方法

我有一项服务可以使用RabbitMQ队列中的消息:

INDEX.JS

const cote = require('cote');
const consumer = require('./utils/consumer');

const messageConsumerResponder = new cote.Responder({
    name: 'message consumer service responder',key: 'message-consumer'
})
messageConsumerResponder.on('*',req => req.type && console.log(req))


messageConsumerResponder.on('consume-message',async (req) => {

    // Consume the message
    const list = await consumer.consumeMessages();
    console.log('message-consumer-service :: This is the list of the Consumed messages ...',list);
});

...它使用了Consumer,如下所示

CONSUMER.JS

const CONN_URL = 'amqp://YYYYYYYYYYYYYYYYYYYYY';
const amqp = require("amqplib").connect(CONN_URL);
const queueName = "XXXXXXXXXXXXXXX";
let ch = null;

/**
 * Consume messages and send them to the "path-finder-service" to find shortest path 
 */
const consumeMessages = async () => {
    amqp
        .then(function (conn) {
            return conn.createChannel();
        })
        .then(function (ch) {
            return ch.assertQueue(queueName).then(function (ok) {
                console.log('message-consumer-service :: Asserted...');
                return ch.consume(queueName,function (msg) {
                    console.log('message-consumer-service :: Consuming...');
                    // msg = JSON.parse(msg);
                    if (msg !== null) {
                        console.log('message-consumer-service :: ',msg.content.toString());
                        ch.ack(msg);
                    }
                });
            });
        })
        .catch(console.warn);
}

process.on('exit',(code) => {
    ch.close();
    console.log(`Closing rabbitmq channel`);
});


module.exports.consumeMessages = consumeMessages;

当我从使用者返回值时,它总是返回null,在这里

const list = await consumer.consumeMessages();

即使消息被弹出并成功使用,也是如此。

我们如何将已使用的消息返回给

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。