簡體   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