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

寻根 / RabbitmqConnect
关闭

加入 Gitee
与超过 600 万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未指定开源许可证,未经作者的许可,此代码仅用于学习,不能用于其他用途。
克隆/下载
Example.cpp 1.58 KB
一键复制 编辑 Web IDE 原始数据 按行查看 历史
寻根 提交于 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;
}

评论 ( 0 )

你可以在登录后,发表评论

C++
1
https://gitee.com/xungen/rabbitmqconnect.git
git@gitee.com:xungen/rabbitmqconnect.git
xungen
rabbitmqconnect
RabbitmqConnect
master

搜索帮助

182229 41614e54 1850385 182230 7885ed45 1850385