赞
踩
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
生产者示例代码(SimpleProducer.cpp)的内容如下:
/* * Description: Simple Producer demo */ #include <unistd.h> #include <stdlib.h> #include <iostream> #include <string> #include "CProducer.h" #include "CMessage.h" #include "CSendResult.h" using namespace std; // send message void StartSendMessage(CProducer *producer) { CSendResult result; // create message and set some values for it CMessage *msg = CreateMessage("Test_Topic"); SetMessageTags(msg, "Test_Tag"); SetMessageKeys(msg, "Test_Keys"); for (int i = 0; i < 10; i++) { // construct different body string strMessageBody = "this is body number-" + to_string(i); SetMessageBody(msg, strMessageBody.c_str()); // send message SendMessageSync(producer, msg, &result); cout << "send message[" << i << "], result status:" << (int)result.sendStatus << ", msgBody:" << strMessageBody << endl; usleep(1000000); } // destroy message DestroyMessage(msg); } int main(int argc, char *argv[]) { cout << "Producer Initializing....." << endl; // create producer and set some values for it CProducer *producer = CreateProducer("Group_producer"); SetProducerNameServerAddress(producer, "192.168.213.128:9876;192.168.213.129:9876"); // start producer StartProducer(producer); cout << "Producer start....." << endl; // send message StartSendMessage(producer); // shutdown producer ShutdownProducer(producer); // destroy producer DestroyProducer(producer); cout << "Producer Shutdown!" << endl; return 0; }
编译并执行上述代码,过程信息结果如下:
[root@node3 /opt/liitdar/rocketmq]# g++ -o SimpleProducer SimpleProducer.cpp -I ./include/ -L/opt/rocketmq-client-cpp-1.2.1/bin/ -lrocketmq -lpthread -lz -ldl -lrt -std=c++11 [root@node3 /opt/liitdar/rocketmq]# ./SimpleProducer Producer Initializing..... Producer start..... send message[0], result status:0, msgBody:this is body number-0 send message[1], result status:0, msgBody:this is body number-1 send message[2], result status:0, msgBody:this is body number-2 send message[3], result status:0, msgBody:this is body number-3 send message[4], result status:0, msgBody:this is body number-4 send message[5], result status:0, msgBody:this is body number-5 send message[6], result status:0, msgBody:this is body number-6 send message[7], result status:0, msgBody:this is body number-7 send message[8], result status:0, msgBody:this is body number-8 send message[9], result status:0, msgBody:this is body number-9 Producer Shutdown! [root@node3 /opt/liitdar/rocketmq]#
从上述结果能够看到,生产者程序成功地生产(向消息队列中发送)了 10 条消息。
Push 模式的消费者示例代码(SimplePushConsumer.cpp),内容如下:
/* * Description: Simple push consumer demo */ #include <unistd.h> #include <stdlib.h> #include <iostream> #include <string> #include "CPushConsumer.h" #include "CMessageExt.h" using namespace std; // consume message int doConsumeMessage(struct CPushConsumer *consumer, CMessageExt *msgExt) { cout << "[Consume Message] " << "MsgTopic:" << GetMessageTopic(msgExt) << ", MsgTags:" << GetMessageTags(msgExt) << ", MsgKeys:" << GetMessageKeys(msgExt) << ", MsgBody:" << GetMessageBody(msgExt) << endl; return E_CONSUME_SUCCESS; } int main(int argc, char *argv[]) { cout << "Push consumer Initializing...." << endl; // create push consumer and set some values for it CPushConsumer *consumer = CreatePushConsumer("Group_Consumer_Test"); SetPushConsumerNameServerAddress(consumer, "192.168.213.128:9876;192.168.213.129:9876"); Subscribe(consumer, "Test_Topic", "*"); // register message callback RegisterMessageCallback(consumer, doConsumeMessage); // start push consumer StartPushConsumer(consumer); cout << "Push consumer start, and listening message within 1min..." << endl; for (int i = 0; i < 6; i++) { cout << "Already Running: " << (i * 10) << "S" << endl; usleep(10000000); } // shutdown push consumer ShutdownPushConsumer(consumer); // destroy push consumer DestroyPushConsumer(consumer); cout << "PushConsumer Shutdown!" << endl; return 0; }
编译上述代码,得到可执行程序,命令如下:
[root@node3 /opt/liitdar/rocketmq]# g++ -o SimplePushConsumer SimplePushConsumer.cpp -I ./include/ -L/opt/rocketmq-client-cpp-1.2.1/bin/ -lrocketmq -lpthread -lz -ldl -lrt
执行上面生成的消费者程序,待消费者进入消息监听状态时,执行前面编译好的生产者程序(以产生消息),运行结果信息如下:
[root@node3 /opt/liitdar/rocketmq]# ./SimplePushConsumer Push consumer Initializing.... Push consumer start, and listening message within 1min... Already Running: 0S Already Running: 10S Already Running: 20S Already Running: 30S Already Running: 40S [Consume Message] MsgTopic:Test_Topic, MsgTags:Test_Tag, MsgKeys:Test_Keys, MsgBody:this is body number-0 [Consume Message] MsgTopic:Test_Topic, MsgTags:Test_Tag, MsgKeys:Test_Keys, MsgBody:this is body number-1 [Consume Message] MsgTopic:Test_Topic, MsgTags:Test_Tag, MsgKeys:Test_Keys, MsgBody:this is body number-2 [Consume Message] MsgTopic:Test_Topic, MsgTags:Test_Tag, MsgKeys:Test_Keys, MsgBody:this is body number-3 [Consume Message] MsgTopic:Test_Topic, MsgTags:Test_Tag, MsgKeys:Test_Keys, MsgBody:this is body number-4 [Consume Message] MsgTopic:Test_Topic, MsgTags:Test_Tag, MsgKeys:Test_Keys, MsgBody:this is body number-5 ![img](https://img-blog.csdnimg.cn/img_convert/66fb6e94c4b4ed5ac937650509a09c08.png) ![img](https://img-blog.csdnimg.cn/img_convert/9a5482e9d4b1098d1944e3e2eb5e5ff7.png) **既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!** **由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新** **[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)** 有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!** **由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新** **[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。