This commit is contained in:
lnk
2025-04-10 16:07:39 +08:00
parent 633682ae0c
commit 2a8f2c996c
3 changed files with 41 additions and 2 deletions

View File

@@ -401,6 +401,8 @@ public:
// 设置 nameserver 地址
SetProducerNameServerAddress(producer_, nameServer.c_str());
SetProducerSessionCredentials(producer_, G_MQCONSUMER_ACCESSKEY.c_str(),G_MQCONSUMER_SECRETKEY.c_str(), "");
// 启动生产者
StartProducer(producer_);
@@ -411,6 +413,32 @@ public:
RocketMQProducer(const RocketMQProducer&) = delete;
RocketMQProducer& operator=(const RocketMQProducer&) = delete;
void printSendResult(const CSendResult& result) {
std::cout << "SendResult:" << std::endl;
std::cout << " Status: ";
switch (result.sendStatus) {
case E_SEND_OK:
std::cout << "E_SEND_OK";
break;
case E_SEND_FLUSH_DISK_TIMEOUT:
std::cout << "E_SEND_FLUSH_DISK_TIMEOUT";
break;
case E_SEND_FLUSH_SLAVE_TIMEOUT:
std::cout << "E_SEND_FLUSH_SLAVE_TIMEOUT";
break;
case E_SEND_SLAVE_NOT_AVAILABLE:
std::cout << "E_SEND_SLAVE_NOT_AVAILABLE";
break;
default:
std::cout << "UNKNOWN(" << result.sendStatus << ")";
break;
}
std::cout << std::endl;
std::cout << " MsgID : " << result.msgId << std::endl;
std::cout << " Offset: " << result.offset << std::endl;
}
// 发送消息
void sendMessage(const char* strbody, const char* topic, const std::string& tags, const std::string& keys) {
CSendResult result;
@@ -439,12 +467,23 @@ public:
);
//调试用
/*
/*int sendResult = SendMessageSync(
producer_,
msg,
&result
);
int ret = SendMessageSync(producer_, msg, &result);
if (ret == 0) {
printSendResult(result);
} else {
std::cerr << "SendMessageSync failed with ret=" << ret << std::endl;
}*/
if (sendResult == 0) { // 假设返回 0 表示成功
std::cout << "Message sent successfully.topic:" << topic <<std::endl;
} else {
std::cout << "Failed to send message." << std::endl;
}*/
}
// 销毁消息
DestroyMessage(msg);