因为需要,不得不用c++搞这个,还是比较恶心的,不像java成熟,资料也多,没办法也得整。
步骤:
一、安装rabbit mq,之前装了个centos的,这次搞了个windows的,网上资料多,安装也没有什么坑,顺利
二、基于rabbitmq-c去弄吧,要不真的是闲得蛋腾。https://www.rabbitmq.com/devtools.html
这个rabbitmq-c要自己编译,网上资料也算可以,因为坑比较少,自己选择编译成32位或64位都可以。
在这里吐槽一下某个SB,技术界越来越向着开源方向,它下载个JB资料还要积分,去TMD
我把生成的项目和编译好的32/64位都传上来(vs2015)
链接: https://pan.baidu.com/s/1mkNmgJVLA9CBdZvJPTNzMA 提取码: nhgb
三、关于对rabbitmq-c进行再次封装,我没有用官网上的那个SimpleAmqpClient,太难了。编译成功了最后也连不上rabbit mq server,不知道是什么原因。
自己在网上找了个封装的,感觉还不错,
https://plain.blog.csdn.net/article/details/88875446
四、三天时间,终于可以了
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include "RabbitmqClient.h" int Rec() { CRabbitMqClient objRabbitmq; std::string ErrorReturn; int iRet = objRabbitmq.Connect(ErrorReturn); printf("Rabbitmq Connect Ret: %d ", iRet); CExchange my_exchange(exchange_name); iRet = objRabbitmq.DeclareExchange(my_exchange, ErrorReturn); printf("Rabbitmq ExchangeDeclare Ret: %d ", iRet); CQueue my_Queue(queue_name); iRet = objRabbitmq.DeclareQueue(my_Queue, ErrorReturn); printf("Rabbitmq QueueDeclare Ret: %d ", iRet); iRet = objRabbitmq.BindQueueToExchange(my_Queue, my_exchange, routing_keys_name, ErrorReturn); printf("Rabbitmq QueueBind Ret: %d ", iRet); while (true) { std::string msg; iRet = objRabbitmq.ConsumerMessage(msg, "my_Queue", { 1, 0 }, ErrorReturn); printf("Rabbitmq QueueBind Ret: %d ", iRet); printf("Consumer: %s ", msg.c_str()); Sleep(0.5); } return 0; } int Send() { CRabbitMqClient objRabbitmq; std::string ErrorReturn; int iRet = objRabbitmq.Connect(ErrorReturn); printf("Rabbitmq Connect Ret: %d " , iRet); CExchange my_exchange(exchange_name); iRet = objRabbitmq.DeclareExchange(my_exchange,ErrorReturn); printf("Rabbitmq ExchangeDeclare Ret: %d ", iRet); CQueue my_Queue(queue_name); iRet = objRabbitmq.DeclareQueue(my_Queue, ErrorReturn); printf("Rabbitmq QueueDeclare Ret: %d ", iRet); iRet = objRabbitmq.BindQueueToExchange(my_Queue, my_exchange, routing_keys_name, ErrorReturn); printf("Rabbitmq QueueBind Ret: %d ", iRet); std::string strSendMsg1 = "rabbitmq send test msg1"; amqp_basic_properties_t properties = {}; CMessage cmsg(strSendMsg1, properties, routing_keys_name); iRet = objRabbitmq.PublishMessage(cmsg, ErrorReturn); printf("Rabbitmq Publish 1 Ret: %d ", iRet); iRet = objRabbitmq.PublishMessage(cmsg, ErrorReturn); printf("Rabbitmq Publish 1 Ret: %d ", iRet); iRet = objRabbitmq.PublishMessage(cmsg, ErrorReturn); printf("Rabbitmq Publish 1 Ret: %d ", iRet); iRet = objRabbitmq.PublishMessage(cmsg, ErrorReturn); printf("Rabbitmq Publish 1 Ret: %d ", iRet); //objRabbitmq.DisConnect(ErrorReturn); return 0; } int test() { CRabbitMqClient objRabbitmq; std::string ErrorReturn; int iRet = objRabbitmq.Connect(ErrorReturn); printf("Rabbitmq Connect Ret: %d ", iRet); while (true) { CQueue my_Queue(queue_name, true, true); amqp_queue_declare_ok_t r = objRabbitmq.DeclareQueue_Query(my_Queue, ErrorReturn); printf("Rabbitmq QueueDeclare Ret: %d ", r.message_count); Sleep(2); } return 0; } int main() { /*Send(); std::cin.get(); Rec(); std::cin.get();*/ test(); system("pause"); return 0; }
test方法是用来检测队列里有多少消息,有多少消费者