rabbitmq 我可以在Windows上使用AMQP-CPP而不使用C++实现网络层吗?

aydmsdu9  于 2022-11-08  发布在  RabbitMQ
关注(0)|答案(1)|浏览(364)

我正试图使用AMQP-CPP库与RabbitMQ通信,阅读他们的自述文件并不是那么简单,如何在Windows上躲避实现网络层。
正如我们前面提到的,库可以以网络不可知的方式使用。它本身不执行任何IO,您需要向库传递一个对象,以便库可以使用该对象执行IO。因此,在您开始使用库之前,您首先需要创建一个从ConnectionHandler基类扩展的类。这是一个具有许多方法的类,每次库要发送数据或需要通知您发生错误时,库都会调用这些方法。


# include <amqpcpp.h>

class MyConnectionHandler : public AMQP::ConnectionHandler
{
    /**
     *  Method that is called by the AMQP library every time it has data
     *  available that should be sent to RabbitMQ.
     *  @param  connection  pointer to the main connection object
     *  @param  data        memory buffer with the data that should be sent to RabbitMQ
     *  @param  size        size of the buffer
     */
    virtual void onData(AMQP::Connection *connection, const char *data, size_t size)
    {
        // @todo
        //  Add your own implementation, for example by doing a call to the
        //  send() system call. But be aware that the send() call may not
        //  send all data at once, so you also need to take care of buffering
        //  the bytes that could not immediately be sent, and try to send
        //  them again when the socket becomes writable again
    }

    /**
     *  Method that is called by the AMQP library when the login attempt
     *  succeeded. After this method has been called, the connection is ready
     *  to use.
     *  @param  connection      The connection that can now be used
     */
    virtual void onReady(AMQP::Connection *connection)
    {
        // @todo
        //  add your own implementation, for example by creating a channel
        //  instance, and start publishing or consuming
    }

    /**
     *  Method that is called by the AMQP library when a fatal error occurs
     *  on the connection, for example because data received from RabbitMQ
     *  could not be recognized.
     *  @param  connection      The connection on which the error occurred
     *  @param  message         A human readable error message
     */
    virtual void onError(AMQP::Connection *connection, const char *message)
    {
        // @todo
        //  add your own implementation, for example by reporting the error
        //  to the user of your program, log the error, and destruct the
        //  connection object because it is no longer in a usable state
    }

    /**
     *  Method that is called when the connection was closed. This is the
     *  counter part of a call to Connection::close() and it confirms that the
     *  AMQP connection was correctly closed.
     *
     *  @param  connection      The connection that was closed and that is now unusable
     */
    virtual void onClosed(AMQP::Connection *connection) 
    {
        // @todo
        //  add your own implementation, for example by closing down the
        //  underlying TCP connection too
    }
};

实现ConnectionHandler类(完全取决于您)后,可以通过创建Connection对象以及一个或多个Channel对象来开始使用该库:

// create an instance of your own connection handler
MyConnectionHandler myHandler;

// create a AMQP connection object
AMQP::Connection connection(&myHandler, AMQP::Login("guest","guest"), "/");

// and create a channel
AMQP::Channel channel(&connection);

// use the channel object to call the AMQP method you like
channel.declareExchange("my-exchange", AMQP::fanout);
channel.declareQueue("my-queue");
channel.bindQueue("my-exchange", "my-queue", "my-routing-key");

这些都是他们的自述。
有人能告诉我,有没有办法订阅RabbitMQ并使用通道,而不需要在Windows上实现IO?
我知道他们已经为linux实现了,但是找不到任何为Windows工作的例子。

41zrol4v

41zrol4v1#

您应该可以在Windows上使用libuv,因此这个行程常式:
https://github.com/CopernicaMarketingSoftware/AMQP-CPP/blob/master/include/amqpcpp/libuv.h

**注意:**RabbitMQ团队监控rabbitmq-users邮件列表,仅在某些时候回答StackOverflow上的问题。

相关问题