Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions example/BatchProducer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* 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>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new line after copyright?

#include <stdlib.h>
#include <string.h>
#include <condition_variable>
#include <iomanip>
#include <iomanip>
#include <iostream>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include "common.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new line before line28?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think need a new line between header files


using namespace rocketmq;
using namespace std;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to remove this using namespace std; statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't matter for cpp file

boost::atomic<bool> g_quit;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use std::atomic instead of boost::atomic.

Or do you have any other consideration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy from another sample

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto start = std::chrono::system_clock::now();
auto start = UtilAll::currentTimeMillis();

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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use log instead of std::cout

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i suggest use std::cout or printf for samples because it can see the output in screen at once

<< " 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;
}
16 changes: 8 additions & 8 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ foreach(file ${files})
endif()

if (MSVC)
if (BUILD_ROCKETMQ_SHARED)
target_link_libraries (${basename} rocketmq_shared ${deplibs}
${Boost_LIBRARIES} ${LIBEVENT_LIBRARIES} ${JSONCPP_LIBRARIES})
else()
target_link_libraries (${basename} rocketmq_static ${deplibs}
${Boost_LIBRARIES} ${LIBEVENT_LIBRARIES} ${JSONCPP_LIBRARIES})
if (BUILD_ROCKETMQ_SHARED)
target_link_libraries (${basename} rocketmq_shared ${deplibs}
${Boost_LIBRARIES} ${LIBEVENT_LIBRARIES} ${JSONCPP_LIBRARIES})
else()
target_link_libraries (${basename} rocketmq_static ${deplibs}
${Boost_LIBRARIES} ${LIBEVENT_LIBRARIES} ${JSONCPP_LIBRARIES})
endif()
else()
if (BUILD_ROCKETMQ_SHARED)
target_link_libraries (${basename} rocketmq_shared)
target_link_libraries (${basename} rocketmq_shared)
else()
target_link_libraries (${basename} rocketmq_static)
target_link_libraries (${basename} rocketmq_static)
endif()
endif()

Expand Down
12 changes: 12 additions & 0 deletions include/BatchMessage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef __BATCHMESSAGE_H__
#define __BATCHMESSAGE_H__
#include "MQMessage.h"
#include <string>
namespace rocketmq {
class BatchMessage : public MQMessage {
public:
static std::string encode(std::vector <MQMessage> &msgs);
static std::string encode(MQMessage &message);
};
}
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a new line at the end of this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

4 changes: 4 additions & 0 deletions include/DefaultMQProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "MQProducer.h"
#include "RocketMQClient.h"
#include "SendResult.h"
#include "BatchMessage.h"

namespace rocketmq {
//<!***************************************************************************
Expand All @@ -43,6 +44,8 @@ class ROCKETMQCLIENT_API DefaultMQProducer : public MQProducer {
virtual SendResult send(MQMessage& msg, MessageQueueSelector* selector,
void* arg, int autoRetryTimes,
bool bActiveBroker = false);
virtual SendResult send(std::vector<MQMessage>& msgs);
virtual SendResult send(std::vector<MQMessage>& msgs, const MQMessageQueue& mq);
virtual void send(MQMessage& msg, SendCallback* pSendCallback,
bool bSelectActiveBroker = false);
virtual void send(MQMessage& msg, const MQMessageQueue& mq,
Expand Down Expand Up @@ -95,6 +98,7 @@ class ROCKETMQCLIENT_API DefaultMQProducer : public MQProducer {
SendResult sendKernelImpl(MQMessage& msg, const MQMessageQueue& mq,
int communicationMode, SendCallback* pSendCallback);
bool tryToCompressMessage(MQMessage& msg);
BatchMessage buildBatchMessage(std::vector<MQMessage>& msgs);

private:
int m_sendMsgTimeout;
Expand Down
Loading