Repository navigation
Expand file tree
/
Copy pathMQClient.cpp
More file actions
203 lines (165 loc) · 6.55 KB
/
Copy pathMQClient.cpp
File metadata and controls
203 lines (165 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* 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 "MQClient.h"
#include "Logging.h"
#include "MQClientFactory.h"
#include "MQClientManager.h"
#include "TopicPublishInfo.h"
#include "UtilAll.h"
namespace rocketmq {
#define ROCKETMQCPP_VERSION "1.0.1"
#define BUILD_DATE "03-14-2018"
// display version: strings bin/librocketmq.so |grep VERSION
const char *rocketmq_build_time =
"VERSION: " ROCKETMQCPP_VERSION ", BUILD DATE: " BUILD_DATE " ";
//<!************************************************************************
MQClient::MQClient() {
string NAMESRV_ADDR_ENV = "NAMESRV_ADDR";
if (const char *addr = getenv(NAMESRV_ADDR_ENV.c_str()))
m_namesrvAddr = addr;
else
m_namesrvAddr = "";
m_instanceName = "DEFAULT";
m_clientFactory = NULL;
m_serviceState = CREATE_JUST;
m_pullThreadNum = boost::thread::hardware_concurrency();
m_tcpConnectTimeout = 3000; // 3s
m_tcpTransportTryLockTimeout = 3; // 3s
m_unitName = "";
}
MQClient::~MQClient() {}
string MQClient::getMQClientId() const {
string clientIP = UtilAll::getLocalAddress();
string processId = UtilAll::to_string(getpid());
return processId + "-" + clientIP + "@" + m_instanceName;
}
//<!groupName;
const string &MQClient::getGroupName() const { return m_GroupName; }
void MQClient::setGroupName(const string &groupname) {
m_GroupName = groupname;
}
const string &MQClient::getNamesrvAddr() const { return m_namesrvAddr; }
void MQClient::setNamesrvAddr(const string &namesrvAddr) {
m_namesrvAddr = namesrvAddr;
}
const string &MQClient::getNamesrvDomain() const { return m_namesrvDomain; }
void MQClient::setNamesrvDomain(const string &namesrvDomain) {
m_namesrvDomain = namesrvDomain;
}
const string &MQClient::getInstanceName() const { return m_instanceName; }
void MQClient::setInstanceName(const string &instanceName) {
m_instanceName = instanceName;
}
void MQClient::createTopic(const string &key, const string &newTopic,
int queueNum) {
try {
getFactory()->createTopic(key, newTopic, queueNum, m_SessionCredentials);
} catch (MQException &e) {
LOG_ERROR(e.what());
}
}
int64 MQClient::earliestMsgStoreTime(const MQMessageQueue &mq) {
return getFactory()->earliestMsgStoreTime(mq, m_SessionCredentials);
}
QueryResult MQClient::queryMessage(const string &topic, const string &key,
int maxNum, int64 begin, int64 end) {
return getFactory()->queryMessage(topic, key, maxNum, begin, end,
m_SessionCredentials);
}
int64 MQClient::minOffset(const MQMessageQueue &mq) {
return getFactory()->minOffset(mq, m_SessionCredentials);
}
int64 MQClient::maxOffset(const MQMessageQueue &mq) {
return getFactory()->maxOffset(mq, m_SessionCredentials);
}
int64 MQClient::searchOffset(const MQMessageQueue &mq, uint64_t timestamp) {
return getFactory()->searchOffset(mq, timestamp, m_SessionCredentials);
}
MQMessageExt *MQClient::viewMessage(const string &msgId) {
return getFactory()->viewMessage(msgId, m_SessionCredentials);
}
vector<MQMessageQueue> MQClient::getTopicMessageQueueInfo(const string &topic) {
boost::weak_ptr<TopicPublishInfo> weak_topicPublishInfo(
getFactory()->tryToFindTopicPublishInfo(topic, m_SessionCredentials));
boost::shared_ptr<TopicPublishInfo> topicPublishInfo(
weak_topicPublishInfo.lock());
if (topicPublishInfo) {
return topicPublishInfo->getMessageQueueList();
}
THROW_MQEXCEPTION(
MQClientException,
"could not find MessageQueue Info of topic: [" + topic + "].", -1);
}
void MQClient::start() {
if (getFactory() == NULL) {
m_clientFactory = MQClientManager::getInstance()->getMQClientFactory(
getMQClientId(), m_pullThreadNum, m_tcpConnectTimeout,
m_tcpTransportTryLockTimeout, m_unitName);
}
LOG_INFO(
"MQClient "
"start,groupname:%s,clientID:%s,instanceName:%s,nameserveraddr:%s",
getGroupName().c_str(), getMQClientId().c_str(),
getInstanceName().c_str(), getNamesrvAddr().c_str());
}
void MQClient::shutdown() { m_clientFactory = NULL; }
MQClientFactory *MQClient::getFactory() const { return m_clientFactory; }
bool MQClient::isServiceStateOk() { return m_serviceState == RUNNING; }
void MQClient::setLogLevel(elogLevel inputLevel) {
ALOG_ADAPTER->setLogLevel(inputLevel);
}
elogLevel MQClient::getLogLevel() { return ALOG_ADAPTER->getLogLevel(); }
void MQClient::setLogFileSizeAndNum(int fileNum, long perFileSize) {
ALOG_ADAPTER->setLogFileNumAndSize(fileNum, perFileSize);
}
void MQClient::setTcpTransportPullThreadNum(int num) {
if (num > m_pullThreadNum) {
m_pullThreadNum = num;
}
}
const int MQClient::getTcpTransportPullThreadNum() const {
return m_pullThreadNum;
}
void MQClient::setTcpTransportConnectTimeout(uint64_t timeout) {
m_tcpConnectTimeout = timeout;
}
const uint64_t MQClient::getTcpTransportConnectTimeout() const {
return m_tcpConnectTimeout;
}
void MQClient::setTcpTransportTryLockTimeout(uint64_t timeout) {
if (timeout < 1000) {
timeout = 1000;
}
m_tcpTransportTryLockTimeout = timeout / 1000;
}
const uint64_t MQClient::getTcpTransportTryLockTimeout() const {
return m_tcpTransportTryLockTimeout;
}
void MQClient::setUnitName(string unitName) { m_unitName = unitName; }
const string &MQClient::getUnitName() { return m_unitName; }
void MQClient::setSessionCredentials(const string &input_accessKey,
const string &input_secretKey,
const string &input_onsChannel) {
m_SessionCredentials.setAccessKey(input_accessKey);
m_SessionCredentials.setSecretKey(input_secretKey);
m_SessionCredentials.setAuthChannel(input_onsChannel);
}
const SessionCredentials &MQClient::getSessionCredentials() const {
return m_SessionCredentials;
}
//<!************************************************************************
} //<!end namespace;