繁体   English   中英

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

[英]Can't publish a message successfully on RabbitMQ server with AMQP-CPP

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

function主要是:

#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 上运行,并且我已经定义了队列和交换/路由键。

问题是我看不到任何消息进入我在服务器上定义的队列。 我必须只使用 TCPHandler 吗? 我找不到 windows 的任何 TCPHandler 实现。 你能提供任何帮助吗? 提前致谢!

端口15672是默认的HTTP端口,用于管理 web 接口和 REST ZDB974278714CA8ACEDFZACE1604。 您想使用AMQP端口,即5672

将您的代码更改为:

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

请注意, RabbitMQ 文档非常详尽。 请花时间阅读并完成其中一个特定于语言的教程。


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

您的应用程序可能在通道准备好之前就完成了执行。 在执行任何通道操作之前尝试等待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

暂无
暂无

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

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