1

我正在使用 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)
4

1 回答 1

0

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

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

// declare the queue
channel.declareQueue("domoqueue", AMQP::durable + AMQP::passive, arguments).onSuccess(callback);
于 2017-03-11T12:00:45.360 回答