AMQP-CPP RABBITMQ HYNC事件的消费者不消耗任何东西

AMQP-CPP RabbitMQ async event based consumer not consuming anything

本文关键字:任何东 消费者 RABBITMQ HYNC 事件 AMQP-CPP      更新时间:2023-10-16

我正在使用amq-cpp库(https://github.com/copernicamarketingsoftware/amqp-cpp)来连接到我创建的现有队列,但我无法阅读任何东西。我已经测试了队列使用另一个库(https://github.com/alanxz/simpleamqpclient,它起作用,我会消耗消息),但它使用了一种投票方法,我需要一个基于事件的方法。

我的代码看起来像(基于提供的示例):

int main()
{
    auto *poll = EV_DEFAULT;
    // handler for libev (so we don't have to implement AMQP::TcpHandler!)
    AMQP::LibEvHandler handler(poll);
    // make a connection
    AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));
    // we need a channel too
    AMQP::TcpChannel channel(&connection);
    // Define callbacks and start
    auto messageCb = [&channel](
            const AMQP::Message &message, uint64_t deliveryTag, 
            bool redelivered)
    {
        std::cout << "message received" << std::endl;
        // acknowledge the message
        channel.ack(deliveryTag);
        processMessage(message.routingKey(), message.body());
    };
    // callback function that is called when the consume operation starts
    auto startCb = [](const std::string &consumertag) {
        std::cout << "consume operation started: " << consumertag << std::endl;
    };
    // callback function that is called when the consume operation failed
    auto errorCb = [](const char *message) {
        std::cout << "consume operation failed" << std::endl;
    };
    channel.consume("domoqueue")
        .onReceived(messageCb)
        .onSuccess(startCb)
        .onError(errorCb);
    // run the poll
    ev_run(poll, 0);
    // done
    return 0;
}

我正在用覆盆子pi运行代码:

Linux raspberrypi 4.4.26-v7+ #915 SMP Thu Oct 20 17:08:44 BST 2016 armv7l GNU/Linux

有什么问题?可能我缺少一些队列的配置参数...我放置了一些调试轨迹,并且没有发生频道创建。它在连接语句中阻止:

AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));
cout << "I never show up" << endl;
// we need a channel too
AMQP::TcpChannel channel(&connection)

我发现了我的问题:我没有使用exclarequeue()方法!实际上,我必须使用它,但要指定以下参数(与我手动创建队列时相同):

AMQP::Table arguments;
arguments["x-message-ttl"] = 120 * 1000;
// declare the queue
channel.declareQueue("domoqueue", AMQP::durable + AMQP::passive, arguments).onSuccess(callback);