-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export send batch messages api in c style. #139
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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 <stdio.h> | ||
#include "CBatchMessage.h" | ||
#include "CCommon.h" | ||
#include "CMessage.h" | ||
#include "CProducer.h" | ||
#include "CSendResult.h" | ||
|
||
void StartSendMessage(CProducer* producer) { | ||
int i = 0; | ||
int ret_code = 0; | ||
char body[128]; | ||
CBatchMessage* batchMessage = CreateBatchMessage("T_TestTopic"); | ||
|
||
for (i = 0; i < 10; i++) { | ||
CMessage* msg = CreateMessage("T_TestTopic"); | ||
SetMessageTags(msg, "Test_Tag"); | ||
SetMessageKeys(msg, "Test_Keys"); | ||
memset(body, 0, sizeof(body)); | ||
snprintf(body, sizeof(body), "new message body, index %d", i); | ||
SetMessageBody(msg, body); | ||
addMessage(batchMessage, msg); | ||
} | ||
CSendResult result; | ||
int ok = SendBatchMessage(producer, batchMessage, &result); | ||
printf("SendBatchMessage is %s .....\n", ok == 0 ? "Success" : ok == 11 ? "FAILED" : " It is null value"); | ||
DestroyBatchMessage(batchMessage); | ||
} | ||
|
||
void CreateProducerAndStartSendMessage() { | ||
printf("Producer Initializing.....\n"); | ||
CProducer* producer = CreateProducer("Group_producer"); | ||
SetProducerNameServerAddress(producer, "127.0.0.1:9876"); | ||
StartProducer(producer); | ||
printf("Producer start.....\n"); | ||
StartSendMessage(producer); | ||
ShutdownProducer(producer); | ||
DestroyProducer(producer); | ||
printf("Producer Shutdown!\n"); | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
printf("Send Batch.....\n"); | ||
CreateProducerAndStartSendMessage(); | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#ifndef __C_BATCHMESSAGE_H__ | ||
#define __C_BATCHMESSAGE_H__ | ||
#include "CCommon.h" | ||
#include "CMessage.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
typedef struct CBatchMessage CBatchMessage; | ||
|
||
ROCKETMQCLIENT_API CBatchMessage* CreateBatchMessage(); | ||
ROCKETMQCLIENT_API int addMessage(CBatchMessage* batchMsg, CMessage* msg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename with big camel-case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already processed |
||
ROCKETMQCLIENT_API int DestroyBatchMessage(CBatchMessage* batchMsg); | ||
|
||
#ifdef __cplusplus | ||
}; | ||
#endif | ||
#endif //__C_BATCHMESSAGE_H__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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 <vector> | ||
|
||
#include "CBatchMessage.h" | ||
#include "CCommon.h" | ||
#include "CMessage.h" | ||
#include "MQMessage.h" | ||
|
||
using std::vector; | ||
|
||
#ifdef __cplusplus | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this file is cpp, the if is unnecessary. and i think the extern is unnecessary too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the C interface and must be ifdef There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个肯定是要用c++编译器编译的,只要用了c++编译器,就一定会定义__cplusplus,所以你的说法是没道理的。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. c接口应该只要在声明的时候用extern “c”包起来就可以了,.cpp实现的时候没必要再次用extern c。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 其他extern实现类,都是这样实现的。如果需要进行整理,大家讨论下。 |
||
extern "C" { | ||
#endif | ||
|
||
using namespace rocketmq; | ||
|
||
CBatchMessage *CreateBatchMessage() { | ||
vector<MQMessage> *msgs = new vector<MQMessage>(); | ||
return (CBatchMessage *) msgs; | ||
} | ||
|
||
int addMessage(CBatchMessage *batchMsg, CMessage *msg) { | ||
if (msg == NULL) { | ||
return NULL_POINTER; | ||
} | ||
if (batchMsg == NULL) { | ||
return NULL_POINTER; | ||
} | ||
MQMessage *message = (MQMessage *) msg; | ||
((vector<MQMessage> *) batchMsg)->push_back(*message); | ||
return OK; | ||
} | ||
int DestroyBatchMessage(CBatchMessage *batchMsg) { | ||
if (batchMsg == NULL) { | ||
return NULL_POINTER; | ||
} | ||
delete (vector<MQMessage> *) batchMsg; | ||
return OK; | ||
} | ||
|
||
#ifdef __cplusplus | ||
}; | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only call CreateProducerAndStartSendMessage in main, so i think the function is unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code structure is clear and consistent with the C case in other examples, which is easy to understand.
代码结构清晰点,同时与其他example中c案例一致,便于理解