|
27 | 27 |
|
28 | 28 | namespace rocketmq {
|
29 | 29 | //<!***************************************************************************
|
30 |
| -AsyncCallbackWrap::AsyncCallbackWrap(AsyncCallback* pAsyncCallback, |
31 |
| - MQClientAPIImpl* pclientAPI) |
32 |
| - : m_pAsyncCallBack(pAsyncCallback), m_pClientAPI(pclientAPI) {} |
| 30 | +AsyncCallbackWrap::AsyncCallbackWrap(AsyncCallback *pAsyncCallback, MQClientAPIImpl *pClientAPI) |
| 31 | + : m_pAsyncCallBack(pAsyncCallback), m_pClientAPI(pClientAPI) {} |
33 | 32 |
|
34 | 33 | AsyncCallbackWrap::~AsyncCallbackWrap() {
|
35 |
| - m_pAsyncCallBack = NULL; |
36 |
| - m_pClientAPI = NULL; |
| 34 | + m_pAsyncCallBack = nullptr; |
| 35 | + m_pClientAPI = nullptr; |
37 | 36 | }
|
38 | 37 |
|
39 | 38 | //<!************************************************************************
|
40 |
| -SendCallbackWrap::SendCallbackWrap(const string& brokerName, |
41 |
| - const MQMessage& msg, |
42 |
| - AsyncCallback* pAsyncCallback, |
43 |
| - MQClientAPIImpl* pclientAPI) |
44 |
| - : AsyncCallbackWrap(pAsyncCallback, pclientAPI), |
45 |
| - m_msg(msg), |
46 |
| - m_brokerName(brokerName) {} |
| 39 | +SendCallbackWrap::SendCallbackWrap( |
| 40 | + const string &brokerName, const MQMessage &msg, SendCallback *pAsyncCallback, MQClientAPIImpl *pClientAPI) |
| 41 | + : AsyncCallbackWrap(pAsyncCallback, pClientAPI), m_msg(msg), m_brokerName(brokerName) {} |
47 | 42 |
|
48 | 43 | void SendCallbackWrap::onException() {
|
49 |
| - if (m_pAsyncCallBack == NULL) return; |
| 44 | + if (m_pAsyncCallBack == nullptr) { |
| 45 | + return; |
| 46 | + } |
50 | 47 |
|
51 |
| - SendCallback* pCallback = static_cast<SendCallback*>(m_pAsyncCallBack); |
52 |
| - if (pCallback) { |
| 48 | + auto *pCallback = static_cast<SendCallback *>(m_pAsyncCallBack); |
| 49 | + if (pCallback != nullptr) { |
53 | 50 | unique_ptr<MQException> exception(new MQException(
|
54 |
| - "send msg failed due to wait response timeout or network error", -1, |
55 |
| - __FILE__, __LINE__)); |
| 51 | + "send msg failed due to wait response timeout or network error", -1, __FILE__, __LINE__)); |
56 | 52 | pCallback->onException(*exception);
|
57 | 53 | if (pCallback->getSendCallbackType() == autoDeleteSendCallback) {
|
58 | 54 | deleteAndZero(pCallback);
|
59 | 55 | }
|
60 | 56 | }
|
61 | 57 | }
|
62 | 58 |
|
63 |
| -void SendCallbackWrap::operationComplete(ResponseFuture* pResponseFuture, |
64 |
| - bool bProducePullRequest) { |
| 59 | +void SendCallbackWrap::onComplete(ResponseFuture *pResponseFuture) { |
65 | 60 | unique_ptr<RemotingCommand> pResponse(pResponseFuture->getCommand());
|
66 | 61 |
|
67 |
| - if (m_pAsyncCallBack == NULL) { |
| 62 | + if (m_pAsyncCallBack == nullptr) { |
68 | 63 | return;
|
69 | 64 | }
|
70 | 65 | int opaque = pResponseFuture->getOpaque();
|
71 |
| - SendCallback* pCallback = static_cast<SendCallback*>(m_pAsyncCallBack); |
| 66 | + auto *pCallback = static_cast<SendCallback *>(m_pAsyncCallBack); |
72 | 67 |
|
73 | 68 | if (!pResponse) {
|
74 |
| - string err = "unknow reseaon"; |
| 69 | + string err = "unknown reason"; |
75 | 70 | if (!pResponseFuture->isSendRequestOK()) {
|
76 | 71 | err = "send request failed";
|
77 |
| - |
78 | 72 | } else if (pResponseFuture->isTimeOut()) {
|
79 |
| - // pResponseFuture->setAsyncResponseFlag(); |
80 | 73 | err = "wait response timeout";
|
81 | 74 | }
|
82 |
| - if (pCallback) { |
| 75 | + |
| 76 | + if (pCallback != nullptr) { |
83 | 77 | MQException exception(err, -1, __FILE__, __LINE__);
|
84 | 78 | pCallback->onException(exception);
|
85 | 79 | }
|
86 | 80 | LOG_ERROR("send failed of:%d", pResponseFuture->getOpaque());
|
87 | 81 | } else {
|
88 | 82 | try {
|
89 |
| - SendResult ret = m_pClientAPI->processSendResponse(m_brokerName, m_msg, pResponse.get()); |
90 |
| - if (pCallback) { |
91 |
| - LOG_DEBUG("operationComplete: processSendResponse success, opaque:%d, maxRetryTime:%d, retrySendTimes:%d", opaque, pResponseFuture->getMaxRetrySendTimes(), pResponseFuture->getRetrySendTimes()); |
92 |
| - pCallback->onSuccess(ret); |
93 |
| - } |
94 |
| - } catch (MQException& e) { |
95 |
| - LOG_ERROR("operationComplete: processSendResponse exception: %s", e.what()); |
96 |
| - |
97 |
| - //broker may return exception, need consider retry send |
98 |
| - int maxRetryTimes = pResponseFuture->getMaxRetrySendTimes(); |
99 |
| - int retryTimes = pResponseFuture->getRetrySendTimes(); |
100 |
| - if (pResponseFuture->getAsyncFlag() && retryTimes < maxRetryTimes && maxRetryTimes > 1) { |
101 |
| - |
102 |
| - int64 left_timeout_ms = pResponseFuture->leftTime(); |
103 |
| - string brokerAddr = pResponseFuture->getBrokerAddr(); |
104 |
| - const RemotingCommand& requestCommand = pResponseFuture->getRequestCommand(); |
105 |
| - retryTimes += 1; |
106 |
| - LOG_WARN("retry send, opaque:%d, sendTimes:%d, maxRetryTimes:%d, left_timeout:%lld, brokerAddr:%s, msg:%s", |
107 |
| - opaque, retryTimes, maxRetryTimes, left_timeout_ms, brokerAddr.data(), m_msg.toString().data()); |
108 |
| - |
109 |
| - bool exception_flag = false; |
110 |
| - try { |
111 |
| - m_pClientAPI->sendMessageAsync(pResponseFuture->getBrokerAddr(), m_brokerName, m_msg, (RemotingCommand&)requestCommand, pCallback, left_timeout_ms, maxRetryTimes, retryTimes); |
112 |
| - } catch (MQClientException& e) { |
113 |
| - LOG_ERROR("retry send exception:%s, opaque:%d, retryTimes:%d, msg:%s, not retry send again", e.what(), opaque, retryTimes, m_msg.toString().data()); |
114 |
| - exception_flag = true; |
115 |
| - } |
116 |
| - |
117 |
| - if (exception_flag == false) { |
118 |
| - return; //send retry again, here need return |
119 |
| - } |
120 |
| - } |
121 |
| - |
122 |
| - if (pCallback) { |
123 |
| - MQException exception("process send response error", -1, __FILE__, |
124 |
| - __LINE__); |
125 |
| - pCallback->onException(exception); |
126 |
| - } |
| 83 | + SendResult ret = m_pClientAPI->processSendResponse(m_brokerName, m_msg, pResponse.get()); |
| 84 | + |
| 85 | + if (pCallback != nullptr) { |
| 86 | + LOG_DEBUG("operationComplete: processSendResponse success, opaque:%d, maxRetryTime:%d, retrySendTimes:%d", |
| 87 | + opaque, pResponseFuture->getMaxRetrySendTimes(), pResponseFuture->getRetrySendTimes()); |
| 88 | + pCallback->onSuccess(ret); |
| 89 | + } |
| 90 | + } catch (MQException &e) { |
| 91 | + LOG_ERROR("operationComplete: processSendResponse exception: %s", e.what()); |
| 92 | + |
| 93 | + //broker may return exception, need consider retry send |
| 94 | + int maxRetryTimes = pResponseFuture->getMaxRetrySendTimes(); |
| 95 | + int retryTimes = pResponseFuture->getRetrySendTimes(); |
| 96 | + if (pResponseFuture->getAsyncFlag() && retryTimes < maxRetryTimes && maxRetryTimes > 1) { |
| 97 | + |
| 98 | + int64 left_timeout_ms = pResponseFuture->leftTime(); |
| 99 | + string brokerAddr = pResponseFuture->getBrokerAddr(); |
| 100 | + auto &requestCommand = const_cast<RemotingCommand &>(pResponseFuture->getRequestCommand()); |
| 101 | + retryTimes += 1; |
| 102 | + LOG_WARN("retry send, opaque:%d, sendTimes:%d, maxRetryTimes:%d, left_timeout:%lld, brokerAddr:%s, msg:%s", |
| 103 | + opaque, retryTimes, maxRetryTimes, left_timeout_ms, brokerAddr.data(), m_msg.toString().data()); |
| 104 | + |
| 105 | + bool exception_flag = false; |
| 106 | + try { |
| 107 | + m_pClientAPI->sendMessageAsync(pResponseFuture->getBrokerAddr(), m_brokerName, m_msg, requestCommand, |
| 108 | + pCallback, left_timeout_ms, maxRetryTimes, retryTimes); |
| 109 | + } catch (MQClientException &e) { |
| 110 | + LOG_ERROR("retry send exception:%s, opaque:%d, retryTimes:%d, msg:%s, not retry send again", |
| 111 | + e.what(), opaque, retryTimes, m_msg.toString().data()); |
| 112 | + exception_flag = true; |
| 113 | + } |
| 114 | + |
| 115 | + if (!exception_flag) { |
| 116 | + return; //send retry again, here need return |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (pCallback != nullptr) { |
| 121 | + MQException exception("process send response error", -1, __FILE__, __LINE__); |
| 122 | + pCallback->onException(exception); |
| 123 | + } |
127 | 124 | }
|
128 | 125 | }
|
129 |
| - if (pCallback && pCallback->getSendCallbackType() == autoDeleteSendCallback) { |
| 126 | + |
| 127 | + if (pCallback != nullptr && pCallback->getSendCallbackType() == autoDeleteSendCallback) { |
130 | 128 | deleteAndZero(pCallback);
|
131 | 129 | }
|
132 | 130 | }
|
133 | 131 |
|
134 | 132 | //<!************************************************************************
|
135 |
| -PullCallbackWarp::PullCallbackWarp(AsyncCallback* pAsyncCallback, |
136 |
| - MQClientAPIImpl* pclientAPI, void* pArg) |
137 |
| - : AsyncCallbackWrap(pAsyncCallback, pclientAPI) { |
138 |
| - m_pArg = *static_cast<AsyncArg*>(pArg); |
| 133 | +PullCallbackWarp::PullCallbackWarp(AsyncCallback *pAsyncCallback, MQClientAPIImpl *pClientAPI, void *pArg) |
| 134 | + : AsyncCallbackWrap(pAsyncCallback, pClientAPI) { |
| 135 | + m_pArg = *static_cast<AsyncArg *>(pArg); |
139 | 136 | }
|
140 | 137 |
|
141 |
| -PullCallbackWarp::~PullCallbackWarp() {} |
| 138 | +PullCallbackWarp::~PullCallbackWarp() = default; |
142 | 139 |
|
143 | 140 | void PullCallbackWarp::onException() {
|
144 |
| - if (m_pAsyncCallBack == NULL) return; |
| 141 | + if (m_pAsyncCallBack == nullptr) { |
| 142 | + return; |
| 143 | + } |
145 | 144 |
|
146 |
| - PullCallback* pCallback = static_cast<PullCallback*>(m_pAsyncCallBack); |
147 |
| - if (pCallback) { |
| 145 | + auto *pCallback = static_cast<PullCallback *>(m_pAsyncCallBack); |
| 146 | + if (pCallback != nullptr) { |
148 | 147 | MQException exception("wait response timeout", -1, __FILE__, __LINE__);
|
149 | 148 | pCallback->onException(exception);
|
150 | 149 | } else {
|
151 | 150 | LOG_ERROR("PullCallback is NULL, AsyncPull could not continue");
|
152 | 151 | }
|
153 | 152 | }
|
154 | 153 |
|
155 |
| -void PullCallbackWarp::operationComplete(ResponseFuture* pResponseFuture, |
156 |
| - bool bProducePullRequest) { |
| 154 | +void PullCallbackWarp::onComplete(ResponseFuture *pResponseFuture) { |
157 | 155 | unique_ptr<RemotingCommand> pResponse(pResponseFuture->getCommand());
|
158 |
| - if (m_pAsyncCallBack == NULL) { |
| 156 | + if (m_pAsyncCallBack == nullptr) { |
159 | 157 | LOG_ERROR("m_pAsyncCallBack is NULL, AsyncPull could not continue");
|
160 | 158 | return;
|
161 | 159 | }
|
162 |
| - PullCallback* pCallback = static_cast<PullCallback*>(m_pAsyncCallBack); |
| 160 | + auto *pCallback = static_cast<PullCallback *>(m_pAsyncCallBack); |
163 | 161 | if (!pResponse) {
|
164 |
| - string err = "unknow reseaon"; |
| 162 | + string err = "unknown reason"; |
165 | 163 | if (!pResponseFuture->isSendRequestOK()) {
|
166 | 164 | err = "send request failed";
|
167 |
| - |
168 | 165 | } else if (pResponseFuture->isTimeOut()) {
|
169 |
| - // pResponseFuture->setAsyncResponseFlag(); |
170 | 166 | err = "wait response timeout";
|
171 | 167 | }
|
172 | 168 | MQException exception(err, -1, __FILE__, __LINE__);
|
173 |
| - LOG_ERROR("Async pull exception of opaque:%d", |
174 |
| - pResponseFuture->getOpaque()); |
175 |
| - if (pCallback && bProducePullRequest) pCallback->onException(exception); |
| 169 | + LOG_ERROR("Async pull exception of opaque:%d", pResponseFuture->getOpaque()); |
| 170 | + |
| 171 | + if (pCallback != nullptr) { |
| 172 | + pCallback->onException(exception); |
| 173 | + } |
176 | 174 | } else {
|
177 | 175 | try {
|
178 | 176 | if (m_pArg.pPullWrapper) {
|
179 |
| - unique_ptr<PullResult> pullResult( |
180 |
| - m_pClientAPI->processPullResponse(pResponse.get())); |
181 |
| - PullResult result = m_pArg.pPullWrapper->processPullResult( |
182 |
| - m_pArg.mq, pullResult.get(), &m_pArg.subData); |
183 |
| - if (pCallback) |
184 |
| - pCallback->onSuccess(m_pArg.mq, result, bProducePullRequest); |
| 177 | + unique_ptr<PullResult> pullResult(m_pClientAPI->processPullResponse(pResponse.get())); |
| 178 | + PullResult result = m_pArg.pPullWrapper->processPullResult(m_pArg.mq, pullResult.get(), &m_pArg.subData); |
| 179 | + |
| 180 | + if (pCallback != nullptr) { |
| 181 | + pCallback->onSuccess(m_pArg.mq, result, true); |
| 182 | + } |
185 | 183 | } else {
|
186 | 184 | LOG_ERROR("pPullWrapper had been destroyed with consumer");
|
187 | 185 | }
|
188 |
| - } catch (MQException& e) { |
| 186 | + } catch (MQException &e) { |
189 | 187 | LOG_ERROR(e.what());
|
190 | 188 | MQException exception("pullResult error", -1, __FILE__, __LINE__);
|
191 |
| - if (pCallback && bProducePullRequest) pCallback->onException(exception); |
| 189 | + |
| 190 | + if (pCallback != nullptr) { |
| 191 | + pCallback->onException(exception); |
| 192 | + } |
192 | 193 | }
|
193 | 194 | }
|
194 | 195 | }
|
|
0 commit comments