简体   繁体   English

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

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

I'm trying to make a simple console application on windows.我正在尝试在 windows 上制作一个简单的控制台应用程序。 I have installed AMQP-CPP and included the library on my Visual Studio project.我已经安装了 AMQP-CPP 并将该库包含在我的 Visual Studio 项目中。 My task is to create the simple communication with the rabbitmq server.我的任务是创建与 rabbitmq 服务器的简单通信。

The main function is: 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;
}

where rabbitmqModels.h code is:其中 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;
    }
};

The code builds without errors.代码构建没有错误。 Note that my rabbitmq server runs on localhost:15672 and I have defined both the queue and the exchange/routing key.请注意,我的 rabbitmq 服务器在 localhost:15672 上运行,并且我已经定义了队列和交换/路由键。

The thing is that I cannot see any message coming to my defined queue on the server.问题是我看不到任何消息进入我在服务器上定义的队列。 Do I have to use only TCPHandler?我必须只使用 TCPHandler 吗? I can't find any implementation of TCPHandler for windows.我找不到 windows 的任何 TCPHandler 实现。 Can you please provide any help?你能提供任何帮助吗? Thanks in advance!提前致谢!

Port 15672 is the default HTTP port for the management web interface and REST API.端口15672是默认的HTTP端口,用于管理 web 接口和 REST ZDB974278714CA8ACEDFZACE1604。 You want to use the AMQP port which is 5672 .您想使用AMQP端口,即5672

Change your code to this:将您的代码更改为:

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

Please note that the RabbitMQ documentation is very thorough.请注意, RabbitMQ 文档非常详尽。 Please take the time to read it and complete one of the language-specific tutorials.请花时间阅读并完成其中一个特定于语言的教程。


NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.注意: RabbitMQ 团队监控rabbitmq-users邮件列表,并且有时只回答 StackOverflow 上的问题。

It's possible that your application completes execution before the channel is even ready.您的应用程序可能在通道准备好之前就完成了执行。 Try waiting for channel.onReady() before performing any channel operations.在执行任何通道操作之前尝试等待channel.onReady() To do this you will need an event loop library like libev, libevent or libuv.为此,您需要一个事件循环库,如 libev、libevent 或 libuv。

main主要的

#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 must inherit LibEvHandler MyLibEvHandler必须继承LibEvHandler

There are good examples here (although they are using TCP): https://github.com/CopernicaMarketingSoftware/AMQP-CPP/tree/master/examples这里有很好的例子(虽然他们使用 TCP): https://github.com/CopernicaMarketingSoftware/AMQP-CPP/tree/master/examples

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

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