Repository navigation
Expand file tree
/
Copy pathMessageImpl.cpp
More file actions
182 lines (144 loc) · 4.78 KB
/
Copy pathMessageImpl.cpp
File metadata and controls
182 lines (144 loc) · 4.78 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
/*
* 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 "MessageImpl.h"
#include <sstream> // std::stringstream
#include <utility> // std::move
#include "MQMessageConst.h"
#include "MessageSysFlag.h"
#include "UtilAll.h"
namespace rocketmq {
MessageImpl::MessageImpl() : MessageImpl(null, null) {}
MessageImpl::MessageImpl(const std::string& topic, const std::string& body)
: MessageImpl(topic, null, null, 0, body, true) {}
MessageImpl::MessageImpl(const std::string& topic,
const std::string& tags,
const std::string& keys,
int32_t flag,
const std::string& body,
bool waitStoreMsgOK)
: topic_(topic), flag_(flag), body_(body) {
if (tags.length() > 0) {
set_tags(tags);
}
if (keys.length() > 0) {
set_keys(keys);
}
set_wait_store_msg_ok(waitStoreMsgOK);
}
MessageImpl::~MessageImpl() = default;
const std::string& MessageImpl::topic() const {
return topic_;
}
void MessageImpl::set_topic(const std::string& topic) {
topic_ = topic;
}
void MessageImpl::set_topic(const char* topic, int len) {
topic_.clear();
topic_.append(topic, len);
}
const std::string& MessageImpl::tags() const {
return getProperty(MQMessageConst::PROPERTY_TAGS);
}
void MessageImpl::set_tags(const std::string& tags) {
putProperty(MQMessageConst::PROPERTY_TAGS, tags);
}
const std::string& MessageImpl::keys() const {
return getProperty(MQMessageConst::PROPERTY_KEYS);
}
void MessageImpl::set_keys(const std::string& keys) {
putProperty(MQMessageConst::PROPERTY_KEYS, keys);
}
void MessageImpl::set_keys(const std::vector<std::string>& keys) {
if (keys.empty()) {
return;
}
std::string strKeys;
auto it = keys.begin();
strKeys += *it;
for (it++; it != keys.end(); it++) {
strKeys += MQMessageConst::KEY_SEPARATOR;
strKeys += *it;
}
set_keys(strKeys);
}
int MessageImpl::delay_time_level() const {
std::string tmp = getProperty(MQMessageConst::PROPERTY_DELAY_TIME_LEVEL);
if (!tmp.empty()) {
return atoi(tmp.c_str());
}
return 0;
}
void MessageImpl::set_delay_time_level(int level) {
putProperty(MQMessageConst::PROPERTY_DELAY_TIME_LEVEL, UtilAll::to_string(level));
}
bool MessageImpl::wait_store_msg_ok() const {
std::string tmp = getProperty(MQMessageConst::PROPERTY_WAIT_STORE_MSG_OK);
return tmp.empty() || UtilAll::stob(tmp);
}
void MessageImpl::set_wait_store_msg_ok(bool waitStoreMsgOK) {
putProperty(MQMessageConst::PROPERTY_WAIT_STORE_MSG_OK, UtilAll::to_string(waitStoreMsgOK));
}
int32_t MessageImpl::flag() const {
return flag_;
}
void MessageImpl::set_flag(int32_t flag) {
flag_ = flag;
}
const std::string& MessageImpl::body() const {
return body_;
}
void MessageImpl::set_body(const std::string& body) {
body_ = body;
}
void MessageImpl::set_body(std::string&& body) {
body_ = std::move(body);
}
const std::string& MessageImpl::transaction_id() const {
return transaction_id_;
}
void MessageImpl::set_transaction_id(const std::string& transactionId) {
transaction_id_ = transactionId;
}
const std::map<std::string, std::string>& MessageImpl::properties() const {
return properties_;
}
void MessageImpl::set_properties(const std::map<std::string, std::string>& properties) {
properties_ = properties;
}
void MessageImpl::set_properties(std::map<std::string, std::string>&& properties) {
properties_ = std::move(properties);
}
const std::string& MessageImpl::getProperty(const std::string& name) const {
const auto& it = properties_.find(name);
if (it != properties_.end()) {
return it->second;
}
return null;
}
void MessageImpl::putProperty(const std::string& name, const std::string& value) {
properties_[name] = value;
}
void MessageImpl::clearProperty(const std::string& name) {
properties_.erase(name);
}
std::string MessageImpl::toString() const {
std::stringstream ss;
ss << "Message [topic=" << topic_ << ", flag=" << flag_ << ", tag=" << tags() << ", transactionId='"
<< transaction_id_ + "']";
return ss.str();
}
} // namespace rocketmq