基于AMQP-CPP RabbitMQ异步事件的消费者不消耗任何东西

时间:2017-03-10 19:41:13

标签: c++ raspberry-pi amqp libev

我使用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;
}

我在Raspberry 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)

1 个答案:

答案 0 :(得分:0)

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

AMQP::Table arguments;
arguments["x-message-ttl"] = 120 * 1000;

// declare the queue
channel.declareQueue("domoqueue", AMQP::durable + AMQP::passive, arguments).onSuccess(callback);