簡體   English   中英

AMQP-CPP 初學者問題

[英]AMQP-CPP beginner problems

我是rabbitmq 的新手,並嘗試讓一個監聽器從消息隊列中讀取。 服務器應該沒問題(不是我實現的,我猜是用 Java 實現的)。 我為消費者使用 C++,我使用 amqpcpp 庫。

這是我迄今為止所嘗試的:

    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();
}

我總是收到“消費操作失敗”。 現在的問題是為什么...有沒有辦法獲得更多的錯誤信息? (例如,無法與 TCP 套接字連接,無法找到具有正確名稱的隊列等)。

感謝您的提示!

在您的代碼中,您試圖計算 *message 指針而不是消息本身。

代替,

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

它應該是,

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

那應該給你正確的錯誤信息。 請查看官方倉庫中的文檔/示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM