Optimize type detection for DefaultMQProducer.sendKernelImpl and cproducer.SendMessageAsync
diff --git a/src/extern/CProducer.cpp b/src/extern/CProducer.cpp
index 265116e..849ac97 100644
--- a/src/extern/CProducer.cpp
+++ b/src/extern/CProducer.cpp
@@ -15,16 +15,18 @@
* limitations under the License.
*/
-#include "DefaultMQProducer.h"
+#include "CProducer.h"
+
+#include <string.h>
+#include <typeindex>
+
#include "AsyncCallback.h"
#include "CBatchMessage.h"
-#include "CProducer.h"
#include "CCommon.h"
-#include "CSendResult.h"
-#include "CMessage.h"
#include "CMQException.h"
-#include <string.h>
-#include <typeinfo>
+#include "CMessage.h"
+#include "CSendResult.h"
+#include "DefaultMQProducer.h"
#ifdef __cplusplus
extern "C" {
@@ -207,7 +209,7 @@
defaultMQProducer->send(*message, cSendCallback);
} catch (exception& e) {
if (cSendCallback != NULL) {
- if (typeid(e) == typeid(MQException)) {
+ if (std::type_index(typeid(e)) == std::type_index(typeid(MQException))) {
MQException& mqe = (MQException&)e;
cSendCallback->onException(mqe);
}
@@ -382,4 +384,4 @@
}
#ifdef __cplusplus
};
-#endif
\ No newline at end of file
+#endif
diff --git a/src/producer/DefaultMQProducer.cpp b/src/producer/DefaultMQProducer.cpp
index 190c38d..20ba968 100644
--- a/src/producer/DefaultMQProducer.cpp
+++ b/src/producer/DefaultMQProducer.cpp
@@ -16,7 +16,11 @@
*/
#include "DefaultMQProducer.h"
+
#include <assert.h>
+#include <typeindex>
+
+#include "BatchMessage.h"
#include "CommandHeader.h"
#include "CommunicationMode.h"
#include "Logging.h"
@@ -26,11 +30,9 @@
#include "MQClientManager.h"
#include "MQDecoder.h"
#include "MQProtos.h"
+#include "StringIdMaker.h"
#include "TopicPublishInfo.h"
#include "Validators.h"
-#include "StringIdMaker.h"
-#include "BatchMessage.h"
-#include <typeinfo>
namespace rocketmq {
@@ -390,8 +392,7 @@
if (!brokerAddr.empty()) {
try {
- BatchMessage batchMessage;
- bool isBatchMsg = (typeid(msg).name() == typeid(batchMessage).name());
+ bool isBatchMsg = std::type_index(typeid(msg)) == std::type_index(typeid(BatchMessage));
// msgId is produced by client, offsetMsgId produced by broker. (same with java sdk)
if (!isBatchMsg) {
string unique_id = StringIdMaker::get_mutable_instance().get_unique_id();
@@ -466,7 +467,7 @@
lastmq = mq;
} else {
LOG_INFO("sendAutoRetrySelectImpl with times:%d", times);
- vector<MQMessageQueue> mqs(topicPublishInfo->getMessageQueueList());
+ std::vector<MQMessageQueue> mqs(topicPublishInfo->getMessageQueueList());
for (size_t i = 0; i < mqs.size(); i++) {
if (mqs[i] == lastmq)
mq_index = i;
@@ -571,4 +572,4 @@
}
//<!***************************************************************************
-} //<!end namespace;
+} // namespace rocketmq