当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 6

freehu/RabbitmqConnect
关闭

forked from 寻根/RabbitmqConnect
关闭
 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Example.cpp 1.58 KB
一键复制 编辑 原始数据 按行查看 历史
寻根 提交于 2019-06-11 20:28 . 修改: Example.cpp
#include <unistd.h>
#include <iostream>
#include "RabbitmqConnect.h"
int main(int argc, char** argv)
{
int port = 5672;
string host = "127.0.0.1";
string user = "admin";
string passwd = "admin";
string exchange = "exchange";
string queuename = "queuename";
RabbitmqConnect conn;
if (conn.connect(host, port) && conn.login(user, passwd))
{
cout << "连接消息队列[" << host << ":" << port << "][" << user << "]成功" << endl;
if (conn.send(exchange, queuename, "message") < 0)
{
cout << "发送消息成功" << endl;
}
else
{
cout << "发送消息失败[" << conn.getErrorCode() << "][" << conn.getErrorString() << "]" << endl;
}
}
else
{
cout << "连接消息队列[" << host << ":" << port << "][" << user << "]失败" << endl;
}
while (true)
{
RabbitmqConnect conn;
if (conn.connect(host, port) && conn.login(user, passwd))
{
cout << "开始监听消息队列[" << exchange << "][" << queuename << "]" << endl;
while (true)
{
conn.recv(exchange, queuename, [](const char* data, int len){
string msg(data, data + len);
cout << "收到消息[" << msg << "]" << endl;
});
//连接异常需要重连
if (conn.getErrorCode() == AMQP_STATUS_SOCKET_ERROR) break;
}
cout << "监听消息队列[" << exchange << "][" << queuename << "]失败[" << conn.getErrorCode() << "][" << conn.getErrorString() << "]" << endl;
}
else
{
cout << "连接消息队列[" << host << ":" << port << "][" << user << "]失败" << endl;
}
sleep(5);
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/freehu/rabbitmqconnect.git
git@gitee.com:freehu/rabbitmqconnect.git
freehu
rabbitmqconnect
RabbitmqConnect
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385