简体   繁体   English

AMQP-CPP > 处理程序中的错误文件描述符

[英]AMQP-CPP > Bad file descriptor in handler

I am trying to use AMQP-CPP library for messaging, however I can not make it work.我正在尝试使用AMQP-CPP库进行消息传递,但是我无法使其工作。 I want to use already built classes from the library for channel, connection, handler.我想将库中已经构建的类用于通道、连接、处理程序。 I started with their examples but every time running the code I get Bad file descriptor error and process ends.我从他们的例子开始,但每次运行代码时,我都会收到Bad file descriptor错误并且进程结束。 My code looks like this我的代码看起来像这样

#include <amqpcpp.h>
#include <amqpcpp/libboostasio.h>
#include <boost/asio/io_service.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/deadline_timer.hpp>

class MyHandler : public AMQP::LibBoostAsioHandler
{
public:
    MyHandler(boost::asio::io_service& service)
        : AMQP::LibBoostAsioHandler(service)
    {
    }

    virtual void onError(AMQP::TcpConnection *connection, const char *message) override
    {
        std::cout << "MyHandler::onError " << message << std::endl;
    }
};

int main()
{
    // access to the event loop
    boost::asio::io_service service(2);

    // handler for libevent
    MyHandler handler(service);

    // make a connection
    AMQP::TcpConnection connection(&handler, AMQP::Address("amqp://localhost/"));

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

    channel.onError([](const char *message) {
        // report error
        std::cout << "channel error: " << message << std::endl;
    });
    channel.onReady([]() {
        // send the first instructions (like publishing messages)
        std::cout << "channel onReady: " << std::endl;
    });

    // create a temporary queue
    channel.declareQueue("aaa").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();
    });  

    auto startCb = [](const std::string &consumertag) {
        std::cout << "consume operation started" << std::endl;
    };

    // callback function that is called when the consume operation failed
    auto errorCb = [](const char *message) {
        std::cout << "consume operation failed" << std::endl;
    };

    // callback operation when a message was received
    auto messageCb = [&channel](const AMQP::Message &message, uint64_t deliveryTag, bool redelivered) {
        std::cout << "message received" << std::endl;
        // acknowledge the message
        channel.ack(deliveryTag);
    };

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

    // run the loop
    service.run();

    return 0;
}

Output:输出:

MyHandler::onError Bad file descriptor MyHandler::onError 错误的文件描述符

Also, error occurs on the line service.run();此外,错误发生在service.run();行上service.run();

I have also tried a similar thing with libevent .我也用libevent尝试过类似的事情。

What's wrong here and how to fix?这里有什么问题以及如何解决? Any ideas?有任何想法吗?

Actually it turned out that there are two issues.事实证明,有两个问题。

Firstly, libev should be used, because that's the oficial supported one.首先,应该使用libev ,因为这是官方支持的。 Secondly, make sure that your RabbitMQ server is running.其次,确保您的 RabbitMQ 服务器正在运行。 More detailed information you can find here .您可以在此处找到更多详细信息。

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

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