无法使用AMQP-CPP在RabbitMQ服务器上成功发布消息

zpf6vheq  于 12个月前  发布在  RabbitMQ
关注(0)|答案(2)|浏览(188)

我正在尝试在windows上做一个简单的控制台应用程序。我已经安装了AMQP-CPP,并在我的Visual Studio项目中包含了这个库。我的任务是创建与rabbitmq服务器的简单通信。
主要功能是:

#include <amqpcpp.h>
#include "rabbitmqModels.h"

int main(){

    string msg = "hello world";
    string queueName = "message_queue";
    string exchangeName = "myexchange";
    string routingKey = "hello";

    Address address("amqp://guest:guest@localhost:15672");
    MyConnectionHandler myHandler;
    Connection connection(&myHandler, address);
    Channel channel(&connection);

    channel.declareQueue(queueName);
    channel.declareExchange(exchangeName, direct).onSuccess([]();
    channel.bindQueue(exchangeName, queueName, routingKey);
    channel.publish(exchangeName, routingKey, msg, msg.size());

    return 0;
}

其中rabbitmqModels.h代码是:

using namespace AMQP;
class MyConnectionHandler : public ConnectionHandler
{
private:
    /**
     *  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(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
    }

    virtual void onReady(Connection* connection) override
    {
        // @todo
        //  add your own implementation, for example by creating a channel
        //  instance, and start publishing or consuming
        std::cout << "Connection is established...\n";

    }

    virtual void onError(Connection* connection, const char* message)
    {
        // report error
        std::cout << "Connection Error: " << message << std::endl;
    }

    virtual void onClosed(Connection* connection)
    {
        std::cout << "closed" << std::endl;

    }

    virtual void onConnected(Connection* connection)
    {
        std::cout << "connected" << std::endl;
    }
};

代码生成时没有错误。请注意,我的rabbitmq服务器运行在localhost:15672上,我已经定义了队列和交换/路由键。
问题是我看不到任何消息到达服务器上我定义的队列。我必须只使用TCPs吗?我找不到任何用于Windows的TCPONT的实现。你能提供任何帮助吗?提前感谢!

7bsow1i6

7bsow1i61#

端口15672是管理Web界面和REST API的默认HTTP端口。您希望使用5672AMQP端口。
将代码更改为:

Address address("amqp://guest:guest@localhost:5672");

请注意,RabbitMQ documentation非常全面。请花时间阅读它并完成特定语言的教程之一。

qvk1mo1f

qvk1mo1f2#

您的应用程序可能在通道准备就绪之前就完成了执行。尝试在执行任何通道操作之前等待channel.onReady()。要做到这一点,你需要一个事件循环库,如libev,libevent或libuv。
主要

#include <amqpcpp.h>
#include "rabbitmqModels.h"
#include <amqpcpp/libev.h>
#include <ev.h>

int main()
{

    string msg = "hello world";
    string queueName = "message_queue";
    string exchangeName = "myexchange";
    string routingKey = "hello";

    // Event loop
    auto *loop = EV_DEFAULT;

    Address address("amqp://guest:guest@localhost:5672");
    MyLibEvHandler myHandler(loop);
    Connection connection(&myHandler, address);
    Channel channel(&connection);

    channel.onReady([&channel]() {
        channel.declareQueue(queueName);
        channel.declareExchange(exchangeName, direct).onSuccess([]();
        channel.bindQueue(exchangeName, queueName, routingKey);
        channel.publish(exchangeName, routingKey, msg, msg.size());
    });

    // Run loop
    ev_run(loop, 0);

    return 0;
}

MyLibEvHandler必须继承LibEvHandler
这里有一些很好的例子(尽管他们使用的是TCP):https://github.com/CopernicaMarketingSoftware/AMQP-CPP/tree/master/examples

相关问题