简体   繁体   English

AMQP-CPP 初学者问题

[英]AMQP-CPP beginner problems

I'm new to rabbitmq, and try to have a Listener to read from a messagequeue.我是rabbitmq 的新手,并尝试让一个监听器从消息队列中读取。 The server should be fine (not implemented by me, I guess implemented in Java).服务器应该没问题(不是我实现的,我猜是用 Java 实现的)。 I use C++ for the consumer, and I use the amqpcpp library.我为消费者使用 C++,我使用 amqpcpp 库。

This is what I tried until now:这是我迄今为止所尝试的:

    int main(int argc, char* const argv[])
{
  // access to the boost asio handler
    // note: we suggest use of 2 threads - normally one is fin (we are simply demonstrating thread safety).
    boost::asio::io_service service(2);

    // handler for libev
    AMQP::LibBoostAsioHandler handler(service);
    
    // make a connection
    AMQP::Address address("amqp://10.40.216.87");
    AMQP::TcpConnection connection(&handler, address);

    // we need a channel too
    AMQP::TcpChannel channel(&connection);


    //channel.onError(errorTCP);

    channel.declareQueue("logmessages");

   // 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:" << *message << std::endl;
    };

    channel.consume("logmessages")
        .onReceived(messageCb)
        .onSuccess(startCb)
        .onError(errorCb);

    
    // create a temporary queue
    /*channel.declareQueue(AMQP::exclusive).onSuccess([&connection](const std::string &name, uint32_t messagecount, uint32_t consumercount) {
        
        // report the name of the temporary queue
        std::cout << "declared queue " << name << std::endl;
        
        // now we can close the connection
        connection.close();
    });*/

    channel.consume("devices.state.*");
    
    // run the handler
    // a t the moment, one will need SIGINT to stop.  In time, should add signal handling through boost API.
    return service.run();
}

I always get a "consume operation failed".我总是收到“消费操作失败”。 The question is now why...Is there any way to get more error messages?现在的问题是为什么...有没有办法获得更多的错误信息? (eg could not connect with TCP socket, could not find queue with correct name, etc.). (例如,无法与 TCP 套接字连接,无法找到具有正确名称的队列等)。

Thanks for your tips!感谢您的提示!

In your code, you're trying to cout the *message pointer instead of the message itself.在您的代码中,您试图计算 *message 指针而不是消息本身。

Instead of,代替,

auto errorCb = [](const char* message) {
    std::cout << "consume operation failed:" << *message << std::endl;
};

It should be,它应该是,

auto errorCb = [](const char* message) {
    std::cout << "consume operation failed:" << message << std::endl;
};

That should give you proper error message.那应该给你正确的错误信息。 Please checkout the docs/examples in the official repo .请查看官方仓库中的文档/示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM