繁体   English   中英

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

[英]Can I use AMQP-CPP without implementing network layer in C++ on Windows?

我正在尝试使用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 的工作示例。

你应该能够在 Windows 上使用libuv ,因此这个处理程序:

https://github.com/CopernicaMarketingSoftware/AMQP-CPP/blob/master/include/amqpcpp/libuv.h


注意: RabbitMQ 团队监控rabbitmq-users邮件列表,并且有时只回答 StackOverflow 上的问题。

暂无
暂无

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

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