gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:07:44 36 4
gpt4 key购买 nike

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

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

最佳答案

端口 15672 是管理 Web 界面和 REST API 的默认 HTTP 端口。您想要使用 AMQP 端口,即 5672

将您的代码更改为:

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

请注意 RabbitMQ documentation非常彻底。请花时间阅读它并完成其中一个特定于语言的教程。


注意:RabbitMQ 团队监控rabbitmq-users mailing list并且只是偶尔在 StackOverflow 上回答问题。

关于c++ - 无法使用 AMQP-CPP 在 RabbitMQ 服务器上成功发布消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58412292/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com