first commit
This commit is contained in:
135
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/AsyncProducer.cpp
vendored
Normal file
135
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/AsyncProducer.cpp
vendored
Normal file
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
using namespace rocketmq;
|
||||
|
||||
std::atomic<bool> g_quit;
|
||||
std::mutex g_mtx;
|
||||
std::condition_variable g_finished;
|
||||
SendCallback* g_callback = NULL;
|
||||
TpsReportService g_tps;
|
||||
|
||||
class MySendCallback : public SendCallback {
|
||||
virtual void onSuccess(SendResult& sendResult) {
|
||||
g_msgCount--;
|
||||
g_tps.Increment();
|
||||
if (g_msgCount.load() <= 0) {
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.notify_one();
|
||||
}
|
||||
}
|
||||
virtual void onException(MQException& e) { cout << "send Exception\n"; }
|
||||
};
|
||||
|
||||
class MyAutoDeleteSendCallback : public AutoDeleteSendCallBack {
|
||||
public:
|
||||
virtual ~MyAutoDeleteSendCallback() {}
|
||||
virtual void onSuccess(SendResult& sendResult) {
|
||||
g_msgCount--;
|
||||
if (g_msgCount.load() <= 0) {
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.notify_one();
|
||||
}
|
||||
}
|
||||
virtual void onException(MQException& e) { std::cout << "send Exception" << e << "\n"; }
|
||||
};
|
||||
|
||||
void AsyncProducerWorker(RocketmqSendAndConsumerArgs* info, DefaultMQProducer* producer) {
|
||||
while (!g_quit.load()) {
|
||||
if (g_msgCount.load() <= 0) {
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.notify_one();
|
||||
}
|
||||
MQMessage msg(info->topic, // topic
|
||||
"*", // tag
|
||||
info->body); // body
|
||||
|
||||
if (info->IsAutoDeleteSendCallback) {
|
||||
g_callback = new MyAutoDeleteSendCallback(); // auto delete
|
||||
}
|
||||
|
||||
try {
|
||||
producer->send(msg, g_callback);
|
||||
} catch (MQException& e) {
|
||||
std::cout << e << endl; // if catch excepiton , need re-send this msg by
|
||||
// service
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
RocketmqSendAndConsumerArgs info;
|
||||
if (!ParseArgs(argc, argv, &info)) {
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
DefaultMQProducer producer("please_rename_unique_group_name");
|
||||
if (!info.IsAutoDeleteSendCallback) {
|
||||
g_callback = new MySendCallback();
|
||||
}
|
||||
|
||||
PrintRocketmqSendAndConsumerArgs(info);
|
||||
|
||||
if (!info.namesrv.empty())
|
||||
producer.setNamesrvAddr(info.namesrv);
|
||||
|
||||
producer.setGroupName(info.groupname);
|
||||
producer.setInstanceName(info.groupname);
|
||||
producer.setNamesrvDomain(info.namesrv_domain);
|
||||
producer.start();
|
||||
g_tps.start();
|
||||
std::vector<std::shared_ptr<std::thread>> work_pool;
|
||||
auto start = std::chrono::system_clock::now();
|
||||
int msgcount = g_msgCount.load();
|
||||
for (int j = 0; j < info.thread_count; j++) {
|
||||
std::shared_ptr<std::thread> th = std::make_shared<std::thread>(AsyncProducerWorker, &info, &producer);
|
||||
work_pool.push_back(th);
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.wait(lck);
|
||||
g_quit.store(true);
|
||||
}
|
||||
|
||||
auto end = std::chrono::system_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
|
||||
std::cout << "per msg time: " << duration.count() / (double)msgcount << "ms \n"
|
||||
<< "========================finished==============================\n";
|
||||
|
||||
producer.shutdown();
|
||||
for (size_t th = 0; th != work_pool.size(); ++th) {
|
||||
work_pool[th]->join();
|
||||
}
|
||||
if (!info.IsAutoDeleteSendCallback) {
|
||||
delete g_callback;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
108
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/AsyncPushConsumer.cpp
vendored
Normal file
108
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/AsyncPushConsumer.cpp
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <condition_variable>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
std::mutex g_mtx;
|
||||
std::condition_variable g_finished;
|
||||
TpsReportService g_tps;
|
||||
using namespace rocketmq;
|
||||
|
||||
class MyMsgListener : public MessageListenerConcurrently {
|
||||
public:
|
||||
MyMsgListener() {}
|
||||
virtual ~MyMsgListener() {}
|
||||
|
||||
virtual ConsumeStatus consumeMessage(const std::vector<MQMessageExt>& msgs) {
|
||||
g_msgCount.store(g_msgCount.load() - msgs.size());
|
||||
for (size_t i = 0; i < msgs.size(); ++i) {
|
||||
g_tps.Increment();
|
||||
}
|
||||
|
||||
if (g_msgCount.load() <= 0) {
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.notify_one();
|
||||
}
|
||||
return CONSUME_SUCCESS;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
RocketmqSendAndConsumerArgs info;
|
||||
if (!ParseArgs(argc, argv, &info)) {
|
||||
exit(-1);
|
||||
}
|
||||
PrintRocketmqSendAndConsumerArgs(info);
|
||||
DefaultMQPushConsumer consumer("please_rename_unique_group_name");
|
||||
DefaultMQProducer producer("please_rename_unique_group_name");
|
||||
|
||||
producer.setNamesrvAddr(info.namesrv);
|
||||
producer.setGroupName("msg-persist-group_producer_sandbox");
|
||||
producer.setNamesrvDomain(info.namesrv_domain);
|
||||
producer.start();
|
||||
|
||||
consumer.setNamesrvAddr(info.namesrv);
|
||||
consumer.setGroupName(info.groupname);
|
||||
consumer.setNamesrvDomain(info.namesrv_domain);
|
||||
consumer.setConsumeFromWhere(CONSUME_FROM_LAST_OFFSET);
|
||||
|
||||
consumer.setInstanceName(info.groupname);
|
||||
|
||||
consumer.subscribe(info.topic, "*");
|
||||
consumer.setConsumeThreadCount(15);
|
||||
consumer.setTcpTransportTryLockTimeout(1000);
|
||||
consumer.setTcpTransportConnectTimeout(400);
|
||||
|
||||
MyMsgListener msglistener;
|
||||
consumer.registerMessageListener(&msglistener);
|
||||
|
||||
try {
|
||||
consumer.start();
|
||||
} catch (MQClientException& e) {
|
||||
cout << e << endl;
|
||||
}
|
||||
g_tps.start();
|
||||
|
||||
int msgcount = g_msgCount.load();
|
||||
for (int i = 0; i < msgcount; ++i) {
|
||||
MQMessage msg(info.topic, // topic
|
||||
"*", // tag
|
||||
info.body); // body
|
||||
|
||||
try {
|
||||
producer.send(msg);
|
||||
} catch (MQException& e) {
|
||||
std::cout << e << endl; // if catch excepiton , need re-send this msg by
|
||||
// service
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.wait(lck);
|
||||
}
|
||||
producer.shutdown();
|
||||
consumer.shutdown();
|
||||
return 0;
|
||||
}
|
||||
122
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/BatchProducer.cpp
vendored
Normal file
122
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/BatchProducer.cpp
vendored
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <condition_variable>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include "common.h"
|
||||
|
||||
using namespace rocketmq;
|
||||
using namespace std;
|
||||
std::atomic<bool> g_quit;
|
||||
std::mutex g_mtx;
|
||||
std::condition_variable g_finished;
|
||||
TpsReportService g_tps;
|
||||
|
||||
void SyncProducerWorker(RocketmqSendAndConsumerArgs* info, DefaultMQProducer* producer) {
|
||||
while (!g_quit.load()) {
|
||||
if (g_msgCount.load() <= 0) {
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.notify_one();
|
||||
break;
|
||||
}
|
||||
|
||||
vector<MQMessage> msgs;
|
||||
MQMessage msg1(info->topic, "*", info->body);
|
||||
msg1.setProperty("property1", "value1");
|
||||
MQMessage msg2(info->topic, "*", info->body);
|
||||
msg2.setProperty("property1", "value1");
|
||||
msg2.setProperty("property2", "value2");
|
||||
MQMessage msg3(info->topic, "*", info->body);
|
||||
msg3.setProperty("property1", "value1");
|
||||
msg3.setProperty("property2", "value2");
|
||||
msg3.setProperty("property3", "value3");
|
||||
msgs.push_back(msg1);
|
||||
msgs.push_back(msg2);
|
||||
msgs.push_back(msg3);
|
||||
try {
|
||||
auto start = std::chrono::system_clock::now();
|
||||
SendResult sendResult = producer->send(msgs);
|
||||
g_tps.Increment();
|
||||
--g_msgCount;
|
||||
auto end = std::chrono::system_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
if (duration.count() >= 500) {
|
||||
std::cout << "send RT more than: " << duration.count() << " ms with msgid: " << sendResult.getMsgId() << endl;
|
||||
}
|
||||
} catch (const MQException& e) {
|
||||
std::cout << "send failed: " << e.what() << std::endl;
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.notify_one();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
RocketmqSendAndConsumerArgs info;
|
||||
if (!ParseArgs(argc, argv, &info)) {
|
||||
exit(-1);
|
||||
}
|
||||
PrintRocketmqSendAndConsumerArgs(info);
|
||||
DefaultMQProducer producer("please_rename_unique_group_name");
|
||||
producer.setNamesrvAddr(info.namesrv);
|
||||
producer.setNamesrvDomain(info.namesrv_domain);
|
||||
producer.setGroupName(info.groupname);
|
||||
producer.setInstanceName(info.groupname);
|
||||
producer.setSessionCredentials("mq acesskey", "mq secretkey", "ALIYUN");
|
||||
producer.setSendMsgTimeout(500);
|
||||
producer.setTcpTransportTryLockTimeout(1000);
|
||||
producer.setTcpTransportConnectTimeout(400);
|
||||
|
||||
producer.start();
|
||||
std::vector<std::shared_ptr<std::thread>> work_pool;
|
||||
auto start = std::chrono::system_clock::now();
|
||||
int msgcount = g_msgCount.load();
|
||||
g_tps.start();
|
||||
|
||||
int threadCount = info.thread_count;
|
||||
for (int j = 0; j < threadCount; j++) {
|
||||
std::shared_ptr<std::thread> th = std::make_shared<std::thread>(SyncProducerWorker, &info, &producer);
|
||||
work_pool.push_back(th);
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.wait(lck);
|
||||
g_quit.store(true);
|
||||
}
|
||||
|
||||
auto end = std::chrono::system_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
|
||||
std::cout << "per msg time: " << duration.count() / (double)msgcount << "ms \n"
|
||||
<< "========================finished==============================\n";
|
||||
|
||||
for (size_t th = 0; th != work_pool.size(); ++th) {
|
||||
work_pool[th]->join();
|
||||
}
|
||||
|
||||
producer.shutdown();
|
||||
|
||||
return 0;
|
||||
}
|
||||
88
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/CAsyncProducer.c
vendored
Normal file
88
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/CAsyncProducer.c
vendored
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "CCommon.h"
|
||||
#include "CMessage.h"
|
||||
#include "CProducer.h"
|
||||
#include "CSendResult.h"
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <memory.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
void thread_sleep(unsigned milliseconds) {
|
||||
#ifdef _WIN32
|
||||
Sleep(milliseconds);
|
||||
#else
|
||||
usleep(milliseconds * 1000); // takes microseconds
|
||||
#endif
|
||||
}
|
||||
|
||||
void SendSuccessCallback(CSendResult result) {
|
||||
printf("async send success, msgid:%s\n", result.msgId);
|
||||
}
|
||||
|
||||
void SendExceptionCallback(CMQException e) {
|
||||
char msg[1024];
|
||||
snprintf(msg, sizeof(msg), "error:%d, msg:%s, file:%s:%d", e.error, e.msg, e.file, e.line);
|
||||
printf("async send exception %s\n", msg);
|
||||
}
|
||||
|
||||
void StartSendMessage(CProducer* producer) {
|
||||
int i = 0;
|
||||
int ret_code = 0;
|
||||
char body[128];
|
||||
CMessage* msg = CreateMessage("T_TestTopic");
|
||||
SetMessageTags(msg, "Test_Tag");
|
||||
SetMessageKeys(msg, "Test_Keys");
|
||||
for (i = 0; i < 10; i++) {
|
||||
memset(body, 0, sizeof(body));
|
||||
snprintf(body, sizeof(body), "new message body, index %d", i);
|
||||
SetMessageBody(msg, body);
|
||||
ret_code = SendMessageAsync(producer, msg, SendSuccessCallback, SendExceptionCallback);
|
||||
printf("async send message[%d] return code: %d\n", i, ret_code);
|
||||
thread_sleep(1000);
|
||||
}
|
||||
DestroyMessage(msg);
|
||||
}
|
||||
|
||||
void CreateProducerAndStartSendMessage(int i) {
|
||||
printf("Producer Initializing.....\n");
|
||||
CProducer* producer = CreateProducer("Group_producer");
|
||||
SetProducerNameServerAddress(producer, "127.0.0.1:9876");
|
||||
if (i == 1) {
|
||||
SetProducerSendMsgTimeout(producer, 3);
|
||||
}
|
||||
StartProducer(producer);
|
||||
printf("Producer start.....\n");
|
||||
StartSendMessage(producer);
|
||||
ShutdownProducer(producer);
|
||||
DestroyProducer(producer);
|
||||
printf("Producer Shutdown!\n");
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
printf("Send Async successCallback.....\n");
|
||||
CreateProducerAndStartSendMessage(0);
|
||||
|
||||
printf("Send Async exceptionCallback.....\n");
|
||||
CreateProducerAndStartSendMessage(1);
|
||||
return 0;
|
||||
}
|
||||
63
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/CBatchProducer.c
vendored
Normal file
63
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/CBatchProducer.c
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "CBatchMessage.h"
|
||||
#include "CCommon.h"
|
||||
#include "CMessage.h"
|
||||
#include "CProducer.h"
|
||||
#include "CSendResult.h"
|
||||
|
||||
void StartSendMessage(CProducer* producer) {
|
||||
int i = 0;
|
||||
int ret_code = 0;
|
||||
char body[128];
|
||||
CBatchMessage* batchMessage = CreateBatchMessage("T_TestTopic");
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
CMessage* msg = CreateMessage("T_TestTopic");
|
||||
SetMessageTags(msg, "Test_Tag");
|
||||
SetMessageKeys(msg, "Test_Keys");
|
||||
memset(body, 0, sizeof(body));
|
||||
snprintf(body, sizeof(body), "new message body, index %d", i);
|
||||
SetMessageBody(msg, body);
|
||||
AddMessage(batchMessage, msg);
|
||||
}
|
||||
CSendResult result;
|
||||
ret_code = SendBatchMessage(producer, batchMessage, &result);
|
||||
printf("SendBatchMessage %s .....\n", ret_code == 0 ? "Success" : ret_code == 11 ? "FAILED" : " It is null value");
|
||||
DestroyBatchMessage(batchMessage);
|
||||
}
|
||||
|
||||
void CreateProducerAndStartSendMessage() {
|
||||
printf("Producer Initializing.....\n");
|
||||
CProducer* producer = CreateProducer("Group_producer");
|
||||
SetProducerNameServerAddress(producer, "127.0.0.1:9876");
|
||||
StartProducer(producer);
|
||||
printf("Producer start.....\n");
|
||||
StartSendMessage(producer);
|
||||
ShutdownProducer(producer);
|
||||
DestroyProducer(producer);
|
||||
printf("Producer Shutdown!\n");
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
printf("Send Batch.....\n");
|
||||
CreateProducerAndStartSendMessage();
|
||||
return 0;
|
||||
}
|
||||
60
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/CMakeLists.txt
vendored
Normal file
60
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
# contributor license agreements. See the NOTICE file distributed with
|
||||
# this work for additional information regarding copyright ownership.
|
||||
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
# (the "License"); you may not use this file except in compliance with
|
||||
# the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
project(example)
|
||||
|
||||
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
|
||||
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include)
|
||||
include_directories(${Boost_INCLUDE_DIRS})
|
||||
|
||||
link_directories(${Boost_LIBRARY_DIRS})
|
||||
link_directories(${LIBEVENT_LIBRARY})
|
||||
link_directories(${JSONCPP_LIBRARY})
|
||||
link_directories(${OPENSSL_LIBRARIES_DIR})
|
||||
|
||||
#if (BUILD_ROCKETMQ_SHARED)
|
||||
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBOOST_ALL_DYN_LINK -shared ")
|
||||
#endif()
|
||||
|
||||
file(GLOB files "*.c*")
|
||||
foreach(file ${files})
|
||||
get_filename_component(basename ${file} NAME_WE)
|
||||
add_executable(${basename} ${file})
|
||||
if(MSVC)
|
||||
if(CMAKE_CONFIGURATION_TYPES STREQUAL "Release")
|
||||
set_target_properties( ${basename} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:LIBCMT" )
|
||||
else()
|
||||
set_target_properties( ${basename} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:LIBCMTD" )
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if (MSVC)
|
||||
if (BUILD_ROCKETMQ_SHARED)
|
||||
target_link_libraries (${basename} rocketmq_shared ${deplibs}
|
||||
${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${LIBEVENT_LIBRARIES} ${JSONCPP_LIBRARIES})
|
||||
else()
|
||||
target_link_libraries (${basename} rocketmq_static ${deplibs}
|
||||
${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${LIBEVENT_LIBRARIES} ${JSONCPP_LIBRARIES})
|
||||
endif()
|
||||
else()
|
||||
if (BUILD_ROCKETMQ_SHARED)
|
||||
target_link_libraries (${basename} rocketmq_shared)
|
||||
else()
|
||||
target_link_libraries (${basename} rocketmq_static)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
endforeach()
|
||||
93
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/COrderlyAsyncProducer.c
vendored
Normal file
93
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/COrderlyAsyncProducer.c
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "CCommon.h"
|
||||
#include "CMessage.h"
|
||||
#include "CProducer.h"
|
||||
#include "CSendResult.h"
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <memory.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
void thread_sleep(unsigned milliseconds) {
|
||||
#ifdef _WIN32
|
||||
Sleep(milliseconds);
|
||||
#else
|
||||
usleep(milliseconds * 1000); // takes microseconds
|
||||
#endif
|
||||
}
|
||||
|
||||
void SendSuccessCallback(CSendResult result) {
|
||||
printf("async send success, msgid:%s\n", result.msgId);
|
||||
}
|
||||
|
||||
void SendExceptionCallback(CMQException e) {
|
||||
char msg[1024];
|
||||
snprintf(msg, sizeof(msg), "error:%d, msg:%s, file:%s:%d", e.error, e.msg, e.file, e.line);
|
||||
printf("async send exception %s\n", msg);
|
||||
}
|
||||
|
||||
int aQueueSelectorCallback(int size, CMessage* msg, void* arg) {
|
||||
return 0;
|
||||
};
|
||||
|
||||
void StartSendMessage(CProducer* producer) {
|
||||
int i = 0;
|
||||
int ret_code = 0;
|
||||
char body[128];
|
||||
CMessage* msg = CreateMessage("topic_COrderlyAsyncProducer");
|
||||
SetMessageTags(msg, "Test_Tag");
|
||||
SetMessageKeys(msg, "Test_Keys");
|
||||
for (i = 0; i < 10; i++) {
|
||||
memset(body, 0, sizeof(body));
|
||||
snprintf(body, sizeof(body), "new message body, index %d", i);
|
||||
SetMessageBody(msg, body);
|
||||
ret_code = SendMessageOrderlyAsync(producer, msg, aQueueSelectorCallback, (void*)&i, SendSuccessCallback,
|
||||
SendExceptionCallback);
|
||||
printf("async send message[%d] return code: %d\n", i, ret_code);
|
||||
thread_sleep(1000);
|
||||
}
|
||||
DestroyMessage(msg);
|
||||
}
|
||||
|
||||
void CreateProducerAndStartSendMessage(int i) {
|
||||
printf("Producer Initializing.....\n");
|
||||
CProducer* producer = CreateProducer("FooBarGroup1");
|
||||
SetProducerNameServerAddress(producer, "192.168.0.149:9876");
|
||||
if (i == 1) {
|
||||
SetProducerSendMsgTimeout(producer, 3);
|
||||
}
|
||||
StartProducer(producer);
|
||||
printf("Producer start.....\n");
|
||||
StartSendMessage(producer);
|
||||
ShutdownProducer(producer);
|
||||
DestroyProducer(producer);
|
||||
printf("Producer Shutdown!\n");
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
printf("COrderlyAsyncProducer successCallback.....\n");
|
||||
CreateProducerAndStartSendMessage(0);
|
||||
|
||||
printf("COrderlyAsyncProducer exceptionCallback.....\n");
|
||||
CreateProducerAndStartSendMessage(1);
|
||||
return 0;
|
||||
}
|
||||
107
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/OrderProducer.cpp
vendored
Normal file
107
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/OrderProducer.cpp
vendored
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <condition_variable>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
using namespace rocketmq;
|
||||
|
||||
std::condition_variable g_finished;
|
||||
std::mutex g_mtx;
|
||||
std::atomic<bool> g_quit(false);
|
||||
|
||||
class SelectMessageQueueByHash : public MessageQueueSelector {
|
||||
public:
|
||||
MQMessageQueue select(const std::vector<MQMessageQueue>& mqs, const MQMessage& msg, void* arg) {
|
||||
int orderId = *static_cast<int*>(arg);
|
||||
int index = orderId % mqs.size();
|
||||
return mqs[index];
|
||||
}
|
||||
};
|
||||
|
||||
SelectMessageQueueByHash g_mySelector;
|
||||
|
||||
void ProducerWorker(RocketmqSendAndConsumerArgs* info, DefaultMQProducer* producer) {
|
||||
while (!g_quit.load()) {
|
||||
if (g_msgCount.load() <= 0) {
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.notify_one();
|
||||
}
|
||||
MQMessage msg(info->topic, // topic
|
||||
"*", // tag
|
||||
info->body); // body
|
||||
|
||||
int orderId = 1;
|
||||
SendResult sendResult =
|
||||
producer->send(msg, &g_mySelector, static_cast<void*>(&orderId), info->retrytimes, info->SelectUnactiveBroker);
|
||||
--g_msgCount;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
RocketmqSendAndConsumerArgs info;
|
||||
if (!ParseArgs(argc, argv, &info)) {
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
DefaultMQProducer producer("please_rename_unique_group_name");
|
||||
PrintRocketmqSendAndConsumerArgs(info);
|
||||
|
||||
producer.setNamesrvAddr(info.namesrv);
|
||||
producer.setNamesrvDomain(info.namesrv_domain);
|
||||
producer.setGroupName(info.groupname);
|
||||
producer.setInstanceName(info.groupname);
|
||||
|
||||
producer.start();
|
||||
|
||||
int msgcount = g_msgCount.load();
|
||||
std::vector<std::shared_ptr<std::thread>> work_pool;
|
||||
|
||||
int threadCount = info.thread_count;
|
||||
for (int j = 0; j < threadCount; j++) {
|
||||
std::shared_ptr<std::thread> th = std::make_shared<std::thread>(ProducerWorker, &info, &producer);
|
||||
work_pool.push_back(th);
|
||||
}
|
||||
|
||||
auto start = std::chrono::system_clock::now();
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.wait(lck);
|
||||
g_quit.store(true);
|
||||
}
|
||||
|
||||
auto end = std::chrono::system_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
|
||||
std::cout << "per msg time: " << duration.count() / (double)msgcount << "ms \n"
|
||||
<< "========================finished==============================\n";
|
||||
|
||||
for (size_t th = 0; th != work_pool.size(); ++th) {
|
||||
work_pool[th]->join();
|
||||
}
|
||||
|
||||
producer.shutdown();
|
||||
|
||||
return 0;
|
||||
}
|
||||
112
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/OrderlyPushConsumer.cpp
vendored
Normal file
112
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/OrderlyPushConsumer.cpp
vendored
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <condition_variable>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
using namespace rocketmq;
|
||||
|
||||
std::condition_variable g_finished;
|
||||
std::mutex g_mtx;
|
||||
std::atomic<int> g_consumedCount(0);
|
||||
std::atomic<bool> g_quit(false);
|
||||
TpsReportService g_tps;
|
||||
|
||||
class MyMsgListener : public MessageListenerOrderly {
|
||||
public:
|
||||
MyMsgListener() {}
|
||||
virtual ~MyMsgListener() {}
|
||||
|
||||
virtual ConsumeStatus consumeMessage(const vector<MQMessageExt>& msgs) {
|
||||
if (g_consumedCount.load() >= g_msgCount) {
|
||||
std::unique_lock<std::mutex> lK(g_mtx);
|
||||
g_quit.store(true);
|
||||
g_finished.notify_one();
|
||||
}
|
||||
for (size_t i = 0; i < msgs.size(); i++) {
|
||||
++g_consumedCount;
|
||||
g_tps.Increment();
|
||||
}
|
||||
return CONSUME_SUCCESS;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
RocketmqSendAndConsumerArgs info;
|
||||
if (!ParseArgs(argc, argv, &info)) {
|
||||
exit(-1);
|
||||
}
|
||||
PrintRocketmqSendAndConsumerArgs(info);
|
||||
DefaultMQPushConsumer consumer("please_rename_unique_group_name");
|
||||
DefaultMQProducer producer("please_rename_unique_group_name");
|
||||
|
||||
producer.setNamesrvAddr(info.namesrv);
|
||||
producer.setGroupName("msg-persist-group_producer_sandbox");
|
||||
producer.start();
|
||||
|
||||
consumer.setNamesrvAddr(info.namesrv);
|
||||
consumer.setNamesrvDomain(info.namesrv_domain);
|
||||
consumer.setGroupName(info.groupname);
|
||||
consumer.setConsumeFromWhere(CONSUME_FROM_LAST_OFFSET);
|
||||
consumer.subscribe(info.topic, "*");
|
||||
consumer.setConsumeThreadCount(info.thread_count);
|
||||
consumer.setConsumeMessageBatchMaxSize(31);
|
||||
if (info.syncpush)
|
||||
consumer.setAsyncPull(false);
|
||||
|
||||
MyMsgListener msglistener;
|
||||
consumer.registerMessageListener(&msglistener);
|
||||
g_tps.start();
|
||||
|
||||
try {
|
||||
consumer.start();
|
||||
} catch (MQClientException& e) {
|
||||
std::cout << e << std::endl;
|
||||
}
|
||||
|
||||
int msgcount = g_msgCount.load();
|
||||
for (int i = 0; i < msgcount; ++i) {
|
||||
MQMessage msg(info.topic, // topic
|
||||
"*", // tag
|
||||
info.body); // body
|
||||
|
||||
try {
|
||||
producer.send(msg);
|
||||
} catch (MQException& e) {
|
||||
std::cout << e << endl; // if catch excepiton , need re-send this msg by
|
||||
// service
|
||||
}
|
||||
}
|
||||
|
||||
while (!g_quit.load()) {
|
||||
std::unique_lock<std::mutex> lk(g_mtx);
|
||||
g_finished.wait(lk);
|
||||
}
|
||||
|
||||
producer.shutdown();
|
||||
consumer.shutdown();
|
||||
return 0;
|
||||
}
|
||||
73
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/Producer.c
vendored
Normal file
73
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/Producer.c
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "CCommon.h"
|
||||
#include "CMessage.h"
|
||||
#include "CProducer.h"
|
||||
#include "CSendResult.h"
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <memory.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
void thread_sleep(unsigned int milliseconds) {
|
||||
#ifdef _WIN32
|
||||
Sleep(milliseconds);
|
||||
#else
|
||||
usleep(milliseconds * 1000); // suspend execution for microsecond intervals
|
||||
#endif
|
||||
}
|
||||
|
||||
void StartSendMessage(CProducer* producer) {
|
||||
int i = 0;
|
||||
char body[256];
|
||||
CMessage* msg = CreateMessage("T_TestTopic");
|
||||
SetMessageTags(msg, "Test_Tag");
|
||||
SetMessageKeys(msg, "Test_Keys");
|
||||
CSendResult result;
|
||||
for (i = 0; i < 3; i++) {
|
||||
memset(body, 0, sizeof(body));
|
||||
snprintf(body, sizeof(body), "new message body, index %d", i);
|
||||
SetMessageBody(msg, body);
|
||||
int status = SendMessageSync(producer, msg, &result);
|
||||
if (status == OK) {
|
||||
printf("send message[%d] result status:%d, msgId:%s\n", i, (int)result.sendStatus, result.msgId);
|
||||
} else {
|
||||
printf("send message[%d] failed !\n", i);
|
||||
}
|
||||
thread_sleep(1000);
|
||||
}
|
||||
DestroyMessage(msg);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
CProducer* producer = CreateProducer("Group_producer");
|
||||
SetProducerNameServerAddress(producer, "127.0.0.1:9876");
|
||||
StartProducer(producer);
|
||||
printf("Producer initialized. \n");
|
||||
|
||||
StartSendMessage(producer);
|
||||
|
||||
ShutdownProducer(producer);
|
||||
DestroyProducer(producer);
|
||||
printf("Producer stopped !\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
95
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/PullConsumeMessage.c
vendored
Normal file
95
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/PullConsumeMessage.c
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "CCommon.h"
|
||||
#include "CMessageExt.h"
|
||||
#include "CMessageQueue.h"
|
||||
#include "CPullConsumer.h"
|
||||
#include "CPullResult.h"
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <memory.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
void thread_sleep(unsigned milliseconds) {
|
||||
#ifdef _WIN32
|
||||
Sleep(milliseconds);
|
||||
#else
|
||||
usleep(milliseconds * 1000); // takes microseconds
|
||||
#endif
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int i = 0, j = 0;
|
||||
int ret = 0;
|
||||
int size = 0;
|
||||
CMessageQueue* mqs = NULL;
|
||||
printf("PullConsumer Initializing....\n");
|
||||
CPullConsumer* consumer = CreatePullConsumer("Group_Consumer_Test");
|
||||
SetPullConsumerNameServerAddress(consumer, "172.17.0.2:9876");
|
||||
StartPullConsumer(consumer);
|
||||
printf("Pull Consumer Start...\n");
|
||||
for (i = 1; i <= 5; i++) {
|
||||
printf("FetchSubscriptionMessageQueues : %d times\n", i);
|
||||
ret = FetchSubscriptionMessageQueues(consumer, "T_TestTopic", &mqs, &size);
|
||||
if (ret != OK) {
|
||||
printf("Get MQ Queue Failed,ErrorCode:%d\n", ret);
|
||||
}
|
||||
printf("Get MQ Size:%d\n", size);
|
||||
for (j = 0; j < size; j++) {
|
||||
int noNewMsg = 0;
|
||||
long long tmpoffset = 0;
|
||||
printf("Pull Message For Topic:%s,Queue:%s,QueueId:%d\n", mqs[j].topic, mqs[j].brokerName, mqs[j].queueId);
|
||||
do {
|
||||
int k = 0;
|
||||
CPullResult pullResult = Pull(consumer, &mqs[j], "*", tmpoffset, 32);
|
||||
if (pullResult.pullStatus != E_BROKER_TIMEOUT) {
|
||||
tmpoffset = pullResult.nextBeginOffset;
|
||||
}
|
||||
printf("PullStatus:%d,MaxOffset:%lld,MinOffset:%lld,NextBegainOffset:%lld", pullResult.pullStatus,
|
||||
pullResult.maxOffset, pullResult.minOffset, pullResult.nextBeginOffset);
|
||||
switch (pullResult.pullStatus) {
|
||||
case E_FOUND:
|
||||
printf("Get Message Size:%d\n", pullResult.size);
|
||||
for (k = 0; k < pullResult.size; ++k) {
|
||||
printf("Got Message ID:%s,Body:%s\n", GetMessageId(pullResult.msgFoundList[k]),
|
||||
GetMessageBody(pullResult.msgFoundList[k]));
|
||||
}
|
||||
break;
|
||||
case E_NO_MATCHED_MSG:
|
||||
noNewMsg = 1;
|
||||
break;
|
||||
default:
|
||||
noNewMsg = 0;
|
||||
}
|
||||
ReleasePullResult(pullResult);
|
||||
thread_sleep(100);
|
||||
} while (noNewMsg == 0);
|
||||
thread_sleep(1000);
|
||||
}
|
||||
thread_sleep(2000);
|
||||
ReleaseSubscriptionMessageQueue(mqs);
|
||||
}
|
||||
thread_sleep(5000);
|
||||
ShutdownPullConsumer(consumer);
|
||||
DestroyPullConsumer(consumer);
|
||||
printf("PullConsumer Shutdown!\n");
|
||||
return 0;
|
||||
}
|
||||
120
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/PullConsumer.cpp
vendored
Normal file
120
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/PullConsumer.cpp
vendored
Normal file
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
using namespace rocketmq;
|
||||
|
||||
std::map<MQMessageQueue, uint64_t> g_offseTable;
|
||||
|
||||
void putMessageQueueOffset(MQMessageQueue mq, uint64_t offset) {
|
||||
g_offseTable[mq] = offset;
|
||||
}
|
||||
|
||||
uint64_t getMessageQueueOffset(MQMessageQueue mq) {
|
||||
map<MQMessageQueue, uint64_t>::iterator it = g_offseTable.find(mq);
|
||||
if (it != g_offseTable.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
RocketmqSendAndConsumerArgs info;
|
||||
if (!ParseArgs(argc, argv, &info)) {
|
||||
exit(-1);
|
||||
}
|
||||
PrintRocketmqSendAndConsumerArgs(info);
|
||||
DefaultMQPullConsumer consumer("please_rename_unique_group_name");
|
||||
consumer.setNamesrvAddr(info.namesrv);
|
||||
consumer.setNamesrvDomain(info.namesrv_domain);
|
||||
consumer.setGroupName(info.groupname);
|
||||
consumer.setInstanceName(info.groupname);
|
||||
consumer.registerMessageQueueListener(info.topic, NULL);
|
||||
consumer.start();
|
||||
std::vector<MQMessageQueue> mqs;
|
||||
|
||||
try {
|
||||
consumer.fetchSubscribeMessageQueues(info.topic, mqs);
|
||||
auto iter = mqs.begin();
|
||||
for (; iter != mqs.end(); ++iter) {
|
||||
std::cout << "mq:" << (*iter).toString() << endl;
|
||||
}
|
||||
} catch (MQException& e) {
|
||||
std::cout << e << endl;
|
||||
}
|
||||
|
||||
auto start = std::chrono::system_clock::now();
|
||||
auto iter = mqs.begin();
|
||||
for (; iter != mqs.end(); ++iter) {
|
||||
MQMessageQueue mq = (*iter);
|
||||
// if cluster model
|
||||
// putMessageQueueOffset(mq, g_consumer.fetchConsumeOffset(mq,true));
|
||||
// if broadcast model
|
||||
// putMessageQueueOffset(mq, your last consume offset);
|
||||
|
||||
bool noNewMsg = false;
|
||||
do {
|
||||
try {
|
||||
PullResult result = consumer.pull(mq, "*", getMessageQueueOffset(mq), 32);
|
||||
g_msgCount += result.msgFoundList.size();
|
||||
std::cout << result.msgFoundList.size() << std::endl;
|
||||
// if pull request timeout or received NULL response, pullStatus will be
|
||||
// setted to BROKER_TIMEOUT,
|
||||
// And nextBeginOffset/minOffset/MaxOffset will be setted to 0
|
||||
if (result.pullStatus != BROKER_TIMEOUT) {
|
||||
putMessageQueueOffset(mq, result.nextBeginOffset);
|
||||
PrintPullResult(&result);
|
||||
} else {
|
||||
cout << "broker timeout occur" << endl;
|
||||
}
|
||||
switch (result.pullStatus) {
|
||||
case FOUND:
|
||||
case NO_MATCHED_MSG:
|
||||
case OFFSET_ILLEGAL:
|
||||
case BROKER_TIMEOUT:
|
||||
break;
|
||||
case NO_NEW_MSG:
|
||||
noNewMsg = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} catch (MQClientException& e) {
|
||||
std::cout << e << std::endl;
|
||||
}
|
||||
} while (!noNewMsg);
|
||||
}
|
||||
|
||||
auto end = std::chrono::system_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
|
||||
std::cout << "msg count: " << g_msgCount.load() << "\n";
|
||||
std::cout << "per msg time: " << duration.count() / (double)g_msgCount.load() << "ms \n"
|
||||
<< "========================finished==============================\n";
|
||||
|
||||
consumer.shutdown();
|
||||
return 0;
|
||||
}
|
||||
63
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/PushConsumeMessage.c
vendored
Normal file
63
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/PushConsumeMessage.c
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "CCommon.h"
|
||||
#include "CMessageExt.h"
|
||||
#include "CPushConsumer.h"
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <memory.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
void thread_sleep(unsigned milliseconds) {
|
||||
#ifdef _WIN32
|
||||
Sleep(milliseconds);
|
||||
#else
|
||||
usleep(milliseconds * 1000); // takes microseconds
|
||||
#endif
|
||||
}
|
||||
|
||||
int doConsumeMessage(struct CPushConsumer* consumer, CMessageExt* msgExt) {
|
||||
printf("Hello,doConsumeMessage by Application!\n");
|
||||
printf("Msg Topic:%s\n", GetMessageTopic(msgExt));
|
||||
printf("Msg Tags:%s\n", GetMessageTags(msgExt));
|
||||
printf("Msg Keys:%s\n", GetMessageKeys(msgExt));
|
||||
printf("Msg Body:%s\n", GetMessageBody(msgExt));
|
||||
return E_CONSUME_SUCCESS;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int i = 0;
|
||||
printf("PushConsumer Initializing....\n");
|
||||
CPushConsumer* consumer = CreatePushConsumer("Group_Consumer_Test");
|
||||
SetPushConsumerNameServerAddress(consumer, "172.17.0.2:9876");
|
||||
Subscribe(consumer, "T_TestTopic", "*");
|
||||
RegisterMessageCallback(consumer, doConsumeMessage);
|
||||
StartPushConsumer(consumer);
|
||||
printf("Push Consumer Start...\n");
|
||||
for (i = 0; i < 10; i++) {
|
||||
printf("Now Running : %d S\n", i * 10);
|
||||
thread_sleep(10000);
|
||||
}
|
||||
ShutdownPushConsumer(consumer);
|
||||
DestroyPushConsumer(consumer);
|
||||
printf("PushConsumer Shutdown!\n");
|
||||
return 0;
|
||||
}
|
||||
125
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/PushConsumer.cpp
vendored
Normal file
125
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/PushConsumer.cpp
vendored
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
std::mutex g_mtx;
|
||||
std::condition_variable g_finished;
|
||||
TpsReportService g_tps;
|
||||
|
||||
using namespace rocketmq;
|
||||
|
||||
class MyMsgListener : public MessageListenerConcurrently {
|
||||
public:
|
||||
MyMsgListener() {}
|
||||
virtual ~MyMsgListener() {}
|
||||
|
||||
virtual ConsumeStatus consumeMessage(const std::vector<MQMessageExt>& msgs) {
|
||||
g_msgCount.store(g_msgCount.load() - msgs.size());
|
||||
for (size_t i = 0; i < msgs.size(); ++i) {
|
||||
g_tps.Increment();
|
||||
// cout << "msg body: "<< msgs[i].getBody() << endl;
|
||||
}
|
||||
|
||||
if (g_msgCount.load() <= 0) {
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.notify_one();
|
||||
}
|
||||
return CONSUME_SUCCESS;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
RocketmqSendAndConsumerArgs info;
|
||||
if (!ParseArgs(argc, argv, &info)) {
|
||||
exit(-1);
|
||||
}
|
||||
PrintRocketmqSendAndConsumerArgs(info);
|
||||
DefaultMQPushConsumer consumer("please_rename_unique_group_name");
|
||||
DefaultMQProducer producer("please_rename_unique_group_name");
|
||||
producer.setSessionCredentials("AccessKey", "SecretKey", "ALIYUN");
|
||||
producer.setTcpTransportTryLockTimeout(1000);
|
||||
producer.setTcpTransportConnectTimeout(400);
|
||||
producer.setNamesrvDomain(info.namesrv_domain);
|
||||
producer.setNamesrvAddr(info.namesrv);
|
||||
producer.setGroupName("msg-persist-group_producer_sandbox");
|
||||
producer.start();
|
||||
|
||||
consumer.setNamesrvAddr(info.namesrv);
|
||||
consumer.setGroupName(info.groupname);
|
||||
consumer.setSessionCredentials("AccessKey", "SecretKey", "ALIYUN");
|
||||
consumer.setConsumeThreadCount(info.thread_count);
|
||||
consumer.setNamesrvDomain(info.namesrv_domain);
|
||||
consumer.setConsumeFromWhere(CONSUME_FROM_LAST_OFFSET);
|
||||
|
||||
if (info.syncpush)
|
||||
consumer.setAsyncPull(false); // set sync pull
|
||||
if (info.broadcasting) {
|
||||
consumer.setMessageModel(rocketmq::BROADCASTING);
|
||||
}
|
||||
|
||||
consumer.setInstanceName(info.groupname);
|
||||
|
||||
consumer.subscribe(info.topic, "*");
|
||||
consumer.setConsumeThreadCount(15);
|
||||
consumer.setTcpTransportTryLockTimeout(1000);
|
||||
consumer.setTcpTransportConnectTimeout(400);
|
||||
|
||||
MyMsgListener msglistener;
|
||||
consumer.registerMessageListener(&msglistener);
|
||||
|
||||
try {
|
||||
consumer.start();
|
||||
} catch (MQClientException& e) {
|
||||
cout << e << endl;
|
||||
}
|
||||
g_tps.start();
|
||||
|
||||
int msgcount = g_msgCount.load();
|
||||
for (int i = 0; i < msgcount; ++i) {
|
||||
MQMessage msg(info.topic, // topic
|
||||
"*", // tag
|
||||
info.body); // body
|
||||
|
||||
// std::this_thread::sleep_for(std::chrono::seconds(100000));
|
||||
try {
|
||||
producer.send(msg);
|
||||
} catch (MQException& e) {
|
||||
std::cout << e << endl; // if catch excepiton , need re-send this msg by
|
||||
// service
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.wait(lck);
|
||||
}
|
||||
producer.shutdown();
|
||||
consumer.shutdown();
|
||||
return 0;
|
||||
}
|
||||
51
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/PushConsumerOrderly.c
vendored
Normal file
51
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/PushConsumerOrderly.c
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef WIN32
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include "CCommon.h"
|
||||
#include "CMessageExt.h"
|
||||
#include "CPushConsumer.h"
|
||||
|
||||
int doConsumeMessage(struct CPushConsumer* consumer, CMessageExt* msgExt) {
|
||||
printf("Hello,doConsumeMessage by Application!\n");
|
||||
printf("Msg Topic:%s\n", GetMessageTopic(msgExt));
|
||||
printf("Msg Tags:%s\n", GetMessageTags(msgExt));
|
||||
printf("Msg Keys:%s\n", GetMessageKeys(msgExt));
|
||||
printf("Msg Body:%s\n", GetMessageBody(msgExt));
|
||||
return E_CONSUME_SUCCESS;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int i = 0;
|
||||
printf("PushConsumer Initializing....\n");
|
||||
CPushConsumer* consumer = CreatePushConsumer("Group_Consumer_Test");
|
||||
SetPushConsumerNameServerAddress(consumer, "127.0.0.1:9876");
|
||||
Subscribe(consumer, "test", "*");
|
||||
RegisterMessageCallbackOrderly(consumer, doConsumeMessage);
|
||||
StartPushConsumer(consumer);
|
||||
printf("Push Consumer Start...\n");
|
||||
for (i = 0; i < 10; i++) {
|
||||
printf("Now Running : %d S\n", i * 10);
|
||||
sleep(10);
|
||||
}
|
||||
ShutdownPushConsumer(consumer);
|
||||
DestroyPushConsumer(consumer);
|
||||
printf("PushConsumer Shutdown!\n");
|
||||
return 0;
|
||||
}
|
||||
1
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/README.md
vendored
Normal file
1
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/README.md
vendored
Normal file
@@ -0,0 +1 @@
|
||||
1. AsyncProducer 2. OrderlyProducer 3. SyncProducer
|
||||
64
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/SendDelayMsg.cpp
vendored
Normal file
64
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/SendDelayMsg.cpp
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <condition_variable>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
using namespace rocketmq;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
RocketmqSendAndConsumerArgs info;
|
||||
if (!ParseArgs(argc, argv, &info)) {
|
||||
exit(-1);
|
||||
}
|
||||
PrintRocketmqSendAndConsumerArgs(info);
|
||||
DefaultMQProducer producer("please_rename_unique_group_name");
|
||||
producer.setNamesrvAddr(info.namesrv);
|
||||
producer.setNamesrvDomain(info.namesrv_domain);
|
||||
producer.setGroupName(info.groupname);
|
||||
producer.setInstanceName(info.groupname);
|
||||
|
||||
producer.setSendMsgTimeout(500);
|
||||
producer.setTcpTransportTryLockTimeout(1000);
|
||||
producer.setTcpTransportConnectTimeout(400);
|
||||
|
||||
producer.start();
|
||||
|
||||
MQMessage msg(info.topic, // topic
|
||||
"*", // tag
|
||||
info.body); // body
|
||||
|
||||
// messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h
|
||||
// 2h
|
||||
msg.setDelayTimeLevel(5); // 1m
|
||||
try {
|
||||
SendResult sendResult = producer.send(msg, info.SelectUnactiveBroker);
|
||||
} catch (const MQException& e) {
|
||||
std::cout << "send failed: " << std::endl;
|
||||
}
|
||||
|
||||
producer.shutdown();
|
||||
return 0;
|
||||
}
|
||||
108
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/SyncProducer.cpp
vendored
Normal file
108
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/SyncProducer.cpp
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <condition_variable>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
using namespace rocketmq;
|
||||
|
||||
std::atomic<bool> g_quit;
|
||||
std::mutex g_mtx;
|
||||
std::condition_variable g_finished;
|
||||
TpsReportService g_tps;
|
||||
|
||||
void SyncProducerWorker(RocketmqSendAndConsumerArgs* info, DefaultMQProducer* producer) {
|
||||
while (!g_quit.load()) {
|
||||
if (g_msgCount.load() <= 0) {
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.notify_one();
|
||||
}
|
||||
MQMessage msg(info->topic, // topic
|
||||
"*", // tag
|
||||
info->body); // body
|
||||
try {
|
||||
auto start = std::chrono::system_clock::now();
|
||||
SendResult sendResult = producer->send(msg, info->SelectUnactiveBroker);
|
||||
g_tps.Increment();
|
||||
--g_msgCount;
|
||||
auto end = std::chrono::system_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
if (duration.count() >= 500) {
|
||||
std::cout << "send RT more than: " << duration.count() << " ms with msgid: " << sendResult.getMsgId() << endl;
|
||||
}
|
||||
} catch (const MQException& e) {
|
||||
std::cout << "send failed: " << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
RocketmqSendAndConsumerArgs info;
|
||||
if (!ParseArgs(argc, argv, &info)) {
|
||||
exit(-1);
|
||||
}
|
||||
PrintRocketmqSendAndConsumerArgs(info);
|
||||
DefaultMQProducer producer("please_rename_unique_group_name");
|
||||
producer.setNamesrvAddr(info.namesrv);
|
||||
producer.setNamesrvDomain(info.namesrv_domain);
|
||||
producer.setGroupName(info.groupname);
|
||||
producer.setInstanceName(info.groupname);
|
||||
producer.setSessionCredentials("mq acesskey", "mq secretkey", "ALIYUN");
|
||||
producer.setSendMsgTimeout(500);
|
||||
producer.setTcpTransportTryLockTimeout(1000);
|
||||
producer.setTcpTransportConnectTimeout(400);
|
||||
|
||||
producer.start();
|
||||
std::vector<std::shared_ptr<std::thread>> work_pool;
|
||||
auto start = std::chrono::system_clock::now();
|
||||
int msgcount = g_msgCount.load();
|
||||
g_tps.start();
|
||||
|
||||
int threadCount = info.thread_count;
|
||||
for (int j = 0; j < threadCount; j++) {
|
||||
std::shared_ptr<std::thread> th = std::make_shared<std::thread>(SyncProducerWorker, &info, &producer);
|
||||
work_pool.push_back(th);
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.wait(lck);
|
||||
g_quit.store(true);
|
||||
}
|
||||
|
||||
auto end = std::chrono::system_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
|
||||
std::cout << "per msg time: " << duration.count() / (double)msgcount << "ms \n"
|
||||
<< "========================finished==============================\n";
|
||||
|
||||
for (size_t th = 0; th != work_pool.size(); ++th) {
|
||||
work_pool[th]->join();
|
||||
}
|
||||
|
||||
producer.shutdown();
|
||||
|
||||
return 0;
|
||||
}
|
||||
135
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/TransactionProducer.cpp
vendored
Normal file
135
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/TransactionProducer.cpp
vendored
Normal file
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include "TransactionListener.h"
|
||||
#include "TransactionMQProducer.h"
|
||||
#include "TransactionSendResult.h"
|
||||
#include "common.h"
|
||||
|
||||
using namespace rocketmq;
|
||||
|
||||
std::atomic<bool> g_quit;
|
||||
std::mutex g_mtx;
|
||||
std::condition_variable g_finished;
|
||||
TpsReportService g_tps;
|
||||
|
||||
class MyTransactionListener : public TransactionListener {
|
||||
virtual LocalTransactionState executeLocalTransaction(const MQMessage& msg, void* arg) {
|
||||
if (!arg) {
|
||||
std::cout << "executeLocalTransaction transactionId:" << msg.getTransactionId()
|
||||
<< ", return state: COMMIT_MESAGE " << endl;
|
||||
return LocalTransactionState::COMMIT_MESSAGE;
|
||||
}
|
||||
|
||||
LocalTransactionState state = (LocalTransactionState)(*(int*)arg % 3);
|
||||
std::cout << "executeLocalTransaction transactionId:" << msg.getTransactionId() << ", return state: " << state
|
||||
<< endl;
|
||||
return state;
|
||||
}
|
||||
|
||||
virtual LocalTransactionState checkLocalTransaction(const MQMessageExt& msg) {
|
||||
std::cout << "checkLocalTransaction enter msg:" << msg.toString() << endl;
|
||||
return LocalTransactionState::COMMIT_MESSAGE;
|
||||
}
|
||||
};
|
||||
|
||||
void SyncProducerWorker(RocketmqSendAndConsumerArgs* info, TransactionMQProducer* producer) {
|
||||
while (!g_quit.load()) {
|
||||
if (g_msgCount.load() <= 0) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(60));
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.notify_one();
|
||||
break;
|
||||
}
|
||||
|
||||
MQMessage msg(info->topic, // topic
|
||||
"*", // tag
|
||||
info->body); // body
|
||||
try {
|
||||
auto start = std::chrono::system_clock::now();
|
||||
std::cout << "before sendMessageInTransaction" << endl;
|
||||
LocalTransactionState state = LocalTransactionState::UNKNOWN;
|
||||
TransactionSendResult sendResult = producer->sendMessageInTransaction(msg, &state);
|
||||
std::cout << "after sendMessageInTransaction msgId: " << sendResult.getMsgId() << endl;
|
||||
g_tps.Increment();
|
||||
--g_msgCount;
|
||||
auto end = std::chrono::system_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
if (duration.count() >= 500) {
|
||||
std::cout << "send RT more than: " << duration.count() << " ms with msgid: " << sendResult.getMsgId() << endl;
|
||||
}
|
||||
} catch (const MQException& e) {
|
||||
std::cout << "send failed: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
RocketmqSendAndConsumerArgs info;
|
||||
if (!ParseArgs(argc, argv, &info)) {
|
||||
exit(-1);
|
||||
}
|
||||
PrintRocketmqSendAndConsumerArgs(info);
|
||||
TransactionMQProducer producer("please_rename_unique_group_name");
|
||||
producer.setNamesrvAddr(info.namesrv);
|
||||
producer.setNamesrvDomain(info.namesrv_domain);
|
||||
producer.setGroupName(info.groupname);
|
||||
producer.setInstanceName(info.groupname);
|
||||
producer.setSessionCredentials("mq acesskey", "mq secretkey", "ALIYUN");
|
||||
producer.setSendMsgTimeout(500);
|
||||
producer.setTcpTransportTryLockTimeout(1000);
|
||||
producer.setTcpTransportConnectTimeout(400);
|
||||
producer.setLogLevel(eLOG_LEVEL_DEBUG);
|
||||
producer.setTransactionListener(new MyTransactionListener());
|
||||
producer.start();
|
||||
std::vector<std::shared_ptr<std::thread>> work_pool;
|
||||
auto start = std::chrono::system_clock::now();
|
||||
int msgcount = g_msgCount.load();
|
||||
g_tps.start();
|
||||
|
||||
int threadCount = info.thread_count;
|
||||
for (int j = 0; j < threadCount; j++) {
|
||||
std::shared_ptr<std::thread> th = std::make_shared<std::thread>(SyncProducerWorker, &info, &producer);
|
||||
work_pool.push_back(th);
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(g_mtx);
|
||||
g_finished.wait(lck);
|
||||
g_quit.store(true);
|
||||
}
|
||||
|
||||
auto end = std::chrono::system_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
|
||||
std::cout << "per msg time: " << duration.count() / (double)msgcount << "ms \n"
|
||||
<< "========================finished==============================\n";
|
||||
|
||||
for (size_t th = 0; th != work_pool.size(); ++th) {
|
||||
work_pool[th]->join();
|
||||
}
|
||||
|
||||
producer.shutdown();
|
||||
|
||||
return 0;
|
||||
}
|
||||
220
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/common.h
vendored
Normal file
220
source/third_party/rocketmq-client-cpp-2.2.0-source-release/example/common.h
vendored
Normal file
@@ -0,0 +1,220 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef ROCKETMQ_CLIENT4CPP_EXAMPLE_COMMON_H_
|
||||
#define ROCKETMQ_CLIENT4CPP_EXAMPLE_COMMON_H_
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#ifndef WIN32
|
||||
#include "unistd.h"
|
||||
#endif
|
||||
#include "Arg_helper.h"
|
||||
#include "DefaultMQProducer.h"
|
||||
#include "DefaultMQPullConsumer.h"
|
||||
#include "DefaultMQPushConsumer.h"
|
||||
using namespace std;
|
||||
|
||||
std::atomic<int> g_msgCount(1);
|
||||
|
||||
class RocketmqSendAndConsumerArgs {
|
||||
public:
|
||||
RocketmqSendAndConsumerArgs()
|
||||
: body("msgbody for test"),
|
||||
thread_count(std::thread::hardware_concurrency()),
|
||||
broadcasting(false),
|
||||
syncpush(false),
|
||||
SelectUnactiveBroker(false),
|
||||
IsAutoDeleteSendCallback(false),
|
||||
retrytimes(5),
|
||||
PrintMoreInfo(false) {}
|
||||
|
||||
public:
|
||||
std::string namesrv;
|
||||
std::string namesrv_domain;
|
||||
std::string groupname;
|
||||
std::string topic;
|
||||
std::string body;
|
||||
int thread_count;
|
||||
bool broadcasting;
|
||||
bool syncpush;
|
||||
bool SelectUnactiveBroker; // default select active broker
|
||||
bool IsAutoDeleteSendCallback;
|
||||
int retrytimes; // default retry 5 times;
|
||||
bool PrintMoreInfo;
|
||||
};
|
||||
|
||||
class TpsReportService {
|
||||
public:
|
||||
TpsReportService() : tps_interval_(1), quit_flag_(false), tps_count_(0) {}
|
||||
void start() {
|
||||
if (tps_thread_ == NULL) {
|
||||
std::cout << "tps_thread_ is null" << std::endl;
|
||||
return;
|
||||
}
|
||||
tps_thread_.reset(new std::thread(std::bind(&TpsReportService::TpsReport, this)));
|
||||
}
|
||||
|
||||
~TpsReportService() {
|
||||
quit_flag_.store(true);
|
||||
if (tps_thread_ == NULL) {
|
||||
std::cout << "tps_thread_ is null" << std::endl;
|
||||
return;
|
||||
}
|
||||
if (tps_thread_->joinable())
|
||||
tps_thread_->join();
|
||||
}
|
||||
|
||||
void Increment() { ++tps_count_; }
|
||||
|
||||
void TpsReport() {
|
||||
while (!quit_flag_.load()) {
|
||||
std::this_thread::sleep_for(tps_interval_);
|
||||
std::cout << "tps: " << tps_count_.load() << std::endl;
|
||||
tps_count_.store(0);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::chrono::seconds tps_interval_;
|
||||
std::shared_ptr<std::thread> tps_thread_;
|
||||
std::atomic<bool> quit_flag_;
|
||||
std::atomic<long> tps_count_;
|
||||
};
|
||||
|
||||
void PrintPullResult(rocketmq::PullResult* result) {
|
||||
std::cout << result->toString() << std::endl;
|
||||
if (result->pullStatus == rocketmq::FOUND) {
|
||||
std::cout << result->toString() << endl;
|
||||
std::vector<rocketmq::MQMessageExt>::iterator it = result->msgFoundList.begin();
|
||||
for (; it != result->msgFoundList.end(); ++it) {
|
||||
cout << "=======================================================" << endl << (*it).toString() << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void PrintRocketmqSendAndConsumerArgs(const RocketmqSendAndConsumerArgs& info) {
|
||||
std::cout << "nameserver: " << info.namesrv << endl
|
||||
<< "topic: " << info.topic << endl
|
||||
<< "groupname: " << info.groupname << endl
|
||||
<< "produce content: " << info.body << endl
|
||||
<< "msg count: " << g_msgCount.load() << endl
|
||||
<< "thread count: " << info.thread_count << endl;
|
||||
}
|
||||
|
||||
static void help() {
|
||||
std::cout << "need option,like follow: \n"
|
||||
<< "-n nameserver addr, if not set -n and -i ,no nameSrv will be got \n"
|
||||
"-i nameserver domain name, if not set -n and -i ,no nameSrv will be "
|
||||
"got \n"
|
||||
"-g groupname \n"
|
||||
"-t msg topic \n"
|
||||
"-m messagecout(default value: 1) \n"
|
||||
"-c content(default value: only test ) \n"
|
||||
"-b (BROADCASTING model, default value: CLUSTER) \n"
|
||||
"-s sync push(default is async push)\n"
|
||||
"-r setup retry times(default value: 5 times)\n"
|
||||
"-u select active broker to send msg(default value: false)\n"
|
||||
"-d use AutoDeleteSendcallback by cpp client(defalut value: false) \n"
|
||||
"-T thread count of send msg or consume msg(defalut value: system cpu "
|
||||
"core number) \n"
|
||||
"-v print more details information \n";
|
||||
}
|
||||
|
||||
static bool ParseArgs(int argc, char* argv[], RocketmqSendAndConsumerArgs* info) {
|
||||
#ifndef WIN32
|
||||
int ch;
|
||||
while ((ch = getopt(argc, argv, "n:i:g:t:m:c:b:s:h:r:T:bu")) != -1) {
|
||||
switch (ch) {
|
||||
case 'n':
|
||||
info->namesrv.insert(0, optarg);
|
||||
break;
|
||||
case 'i':
|
||||
info->namesrv_domain.insert(0, optarg);
|
||||
break;
|
||||
case 'g':
|
||||
info->groupname.insert(0, optarg);
|
||||
break;
|
||||
case 't':
|
||||
info->topic.insert(0, optarg);
|
||||
break;
|
||||
case 'm':
|
||||
g_msgCount.store(atoi(optarg));
|
||||
break;
|
||||
case 'c':
|
||||
info->body.insert(0, optarg);
|
||||
break;
|
||||
case 'b':
|
||||
info->broadcasting = true;
|
||||
break;
|
||||
case 's':
|
||||
info->syncpush = true;
|
||||
break;
|
||||
case 'r':
|
||||
info->retrytimes = atoi(optarg);
|
||||
break;
|
||||
case 'u':
|
||||
info->SelectUnactiveBroker = true;
|
||||
break;
|
||||
case 'T':
|
||||
info->thread_count = atoi(optarg);
|
||||
break;
|
||||
case 'v':
|
||||
info->PrintMoreInfo = true;
|
||||
break;
|
||||
case 'h':
|
||||
help();
|
||||
return false;
|
||||
default:
|
||||
help();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#else
|
||||
rocketmq::Arg_helper arg_help(argc, argv);
|
||||
info->namesrv = arg_help.get_option_value("-n");
|
||||
info->namesrv_domain = arg_help.get_option_value("-i");
|
||||
info->groupname = arg_help.get_option_value("-g");
|
||||
info->topic = arg_help.get_option_value("-t");
|
||||
info->broadcasting = atoi(arg_help.get_option_value("-b").c_str());
|
||||
string msgContent(arg_help.get_option_value("-c"));
|
||||
if (!msgContent.empty())
|
||||
info->body = msgContent;
|
||||
info->syncpush = atoi(arg_help.get_option_value("-s").c_str());
|
||||
int retrytimes = atoi(arg_help.get_option_value("-r").c_str());
|
||||
if (retrytimes > 0)
|
||||
info->retrytimes = retrytimes;
|
||||
info->SelectUnactiveBroker = atoi(arg_help.get_option_value("-u").c_str());
|
||||
int thread_count = atoi(arg_help.get_option_value("-T").c_str());
|
||||
if (thread_count > 0)
|
||||
info->thread_count = thread_count;
|
||||
info->PrintMoreInfo = atoi(arg_help.get_option_value("-v").c_str());
|
||||
g_msgCount = atoi(arg_help.get_option_value("-m").c_str());
|
||||
#endif
|
||||
if (info->groupname.empty() || info->topic.empty() || (info->namesrv_domain.empty() && info->namesrv.empty())) {
|
||||
std::cout << "please use -g to setup groupname and -t setup topic \n";
|
||||
help();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif // ROCKETMQ_CLIENT4CPP_EXAMPLE_COMMON_H_
|
||||
Reference in New Issue
Block a user