我正在尝试在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非常详尽。请花点时间阅读它,并完成其中一种特定于语言的教程。
[NOTE: RabbitMQ团队监视rabbitmq-users
mailing list,并且有时仅在StackOverflow上回答问题。