You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2018/11/20 09:54:53 UTC

[GitHub] vongosling closed pull request #9: Support Pull Consumer

vongosling closed pull request #9: Support Pull Consumer
URL: https://github.com/apache/rocketmq-client-cpp/pull/9
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..00b4d47
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.idea
+cmake-build-debug/
diff --git a/example/PullConsumeMessage.c b/example/PullConsumeMessage.c
new file mode 100644
index 0000000..5890cbc
--- /dev/null
+++ b/example/PullConsumeMessage.c
@@ -0,0 +1,48 @@
+/*
+* 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 WIN32
+#include <unistd.h>
+#endif
+#include <stdio.h>
+
+
+
+#include "CPullConsumer.h"
+#include "CCommon.h"
+#include "CMessageExt.h"
+#include "CPullResult.h"
+#include "CMessageQueue.h"
+
+
+int main(int argc,char * argv [])
+{
+    int i = 0;
+    printf("PullConsumer Initializing....\n");
+    CPullConsumer* consumer = CreatePullConsumer("Group_Consumer_Test");
+    SetPullConsumerNameServerAddress(consumer,"172.17.0.2:9876");
+    StartPullConsumer(consumer);
+    printf("Pull Consumer Start...\n");
+    for( i=0; i<10; i++)
+    {
+        printf("Now Running : %d S\n",i*10);
+        sleep(10);
+    }
+    ShutdownPullConsumer(consumer);
+    DestroyPullConsumer(consumer);
+    printf("PullConsumer Shutdown!\n");
+    return 0;
+}
diff --git a/include/CMessageQueue.h b/include/CMessageQueue.h
index 72f1e61..273f536 100644
--- a/include/CMessageQueue.h
+++ b/include/CMessageQueue.h
@@ -29,6 +29,7 @@ typedef struct _CMessageQueue_ {
     char      brokerName[MAX_BROKER_NAME_ID_LENGTH];
     int       queueId;
 } CMessageQueue;
+
 #ifdef __cplusplus
 };
 #endif
diff --git a/include/CPullConsumer.h b/include/CPullConsumer.h
index 77df836..6b86a20 100644
--- a/include/CPullConsumer.h
+++ b/include/CPullConsumer.h
@@ -26,6 +26,7 @@
 extern "C" {
 #endif
 
+
 typedef struct CPullConsumer CPullConsumer;
 
 
@@ -42,9 +43,11 @@ int SetPullConsumerLogPath(CPullConsumer *consumer, const char *logPath);
 int SetPullConsumerLogFileNumAndSize(CPullConsumer *consumer, int fileNum, long fileSize);
 int SetPullConsumerLogLevel(CPullConsumer *consumer, CLogLevel level);
 
-int fetchSubscribeMessageQueues(CPullConsumer *consumer, const char *topic, CMessageQueue *mqs , int size);
-CPullResult pull(const CMessageQueue *mq, const char *subExpression, long long offset, int maxNums);
+int FetchSubscriptionMessageQueues(CPullConsumer *consumer, const char *topic, CMessageQueue **mqs , int* size);
+int ReleaseSubscriptionMessageQueue(CMessageQueue *mqs);
 
+CPullResult Pull(CPullConsumer *consumer,const CMessageQueue *mq, const char *subExpression, long long offset, int maxNums);
+int ReleasePullResult(CPullResult pullResult);
 #ifdef __cplusplus
 };
 #endif
diff --git a/include/CPullResult.h b/include/CPullResult.h
index 7d234c3..eb44fbd 100644
--- a/include/CPullResult.h
+++ b/include/CPullResult.h
@@ -38,7 +38,7 @@ typedef struct _CPullResult_ {
     long long  nextBeginOffset;
     long long  minOffset;
     long long  maxOffset;
-    CMessageExt* msgFoundList;
+    CMessageExt** msgFoundList;
     int size;
 } CPullResult;
 
diff --git a/src/extern/CPullConsumer.cpp b/src/extern/CPullConsumer.cpp
index aff591f..65daae3 100644
--- a/src/extern/CPullConsumer.cpp
+++ b/src/extern/CPullConsumer.cpp
@@ -21,6 +21,7 @@
 #include "CCommon.h"
 
 using namespace rocketmq;
+using namespace std;
 
 #ifdef __cplusplus
 extern "C" {
@@ -55,14 +56,14 @@ int ShutdownPullConsumer(CPullConsumer *consumer) {
     ((DefaultMQPullConsumer *) consumer)->shutdown();
     return OK;
 }
-int SetPullConsumerGroupID(CPullConsumer *consumer,const char *groupId){
+int SetPullConsumerGroupID(CPullConsumer *consumer, const char *groupId) {
     if (consumer == NULL || groupId == NULL) {
         return NULL_POINTER;
     }
     ((DefaultMQPullConsumer *) consumer)->setGroupName(groupId);
     return OK;
 }
-const char * GetPullConsumerGroupID(CPullConsumer *consumer){
+const char *GetPullConsumerGroupID(CPullConsumer *consumer) {
     if (consumer == NULL) {
         return NULL;
     }
@@ -76,7 +77,7 @@ int SetPullConsumerNameServerAddress(CPullConsumer *consumer, const char *namesr
     return OK;
 }
 int SetPullConsumerSessionCredentials(CPullConsumer *consumer, const char *accessKey, const char *secretKey,
-                                     const char *channel) {
+                                      const char *channel) {
     if (consumer == NULL) {
         return NULL_POINTER;
     }
@@ -97,7 +98,7 @@ int SetPullConsumerLogFileNumAndSize(CPullConsumer *consumer, int fileNum, long
     if (consumer == NULL) {
         return NULL_POINTER;
     }
-    ((DefaultMQPullConsumer *) consumer)->setLogFileSizeAndNum(fileNum,fileSize);
+    ((DefaultMQPullConsumer *) consumer)->setLogFileSizeAndNum(fileNum, fileSize);
     return OK;
 }
 
@@ -105,23 +106,101 @@ int SetPullConsumerLogLevel(CPullConsumer *consumer, CLogLevel level) {
     if (consumer == NULL) {
         return NULL_POINTER;
     }
-    ((DefaultMQPullConsumer *) consumer)->setLogLevel((elogLevel)level);
+    ((DefaultMQPullConsumer *) consumer)->setLogLevel((elogLevel) level);
     return OK;
 }
 
-int fetchSubscribeMessageQueues(CPullConsumer *consumer, const char *topic, CMessageQueue *mqs , int size){
+int FetchSubscriptionMessageQueues(CPullConsumer *consumer, const char *topic, CMessageQueue **mqs, int *size) {
     if (consumer == NULL) {
         return NULL_POINTER;
     }
-    //ToDo, Add implement
+    unsigned int index = 0;
+    std::vector<MQMessageQueue> fullMQ;
+    try {
+        ((DefaultMQPullConsumer *) consumer)->fetchSubscribeMessageQueues(topic, fullMQ);
+        *size = fullMQ.size();
+        //Alloc memory to save the pointer to CPP MessageQueue, and the MessageQueues may be changed.
+        //Thus, this memory should be released by users using @ReleaseSubscribeMessageQueue every time.
+        *mqs = (CMessageQueue *) malloc(*size * sizeof(CMessageQueue));
+        if (*mqs == NULL) {
+            *size = 0;
+            *mqs = NULL;
+            return NULL_POINTER;
+        }
+        auto iter = fullMQ.begin();
+        for (index = 0; iter != fullMQ.end() && index <= fullMQ.size(); ++iter, index++) {
+            strncpy(mqs[index]->topic, iter->getTopic().c_str(), MAX_TOPIC_LENGTH - 1);
+            strncpy(mqs[index]->brokerName, iter->getBrokerName().c_str(), MAX_BROKER_NAME_ID_LENGTH - 1);
+            mqs[index]->queueId = iter->getQueueId();
+        }
+    } catch (MQException &e) {
+        *size = 0;
+        *mqs = NULL;
+    }
+    return OK;
+}
+int ReleaseSubscriptionMessageQueue(CMessageQueue *mqs) {
+    if (mqs == NULL) {
+        return NULL_POINTER;
+    }
+    free((void *) mqs);
+    mqs = NULL;
     return OK;
 }
-CPullResult pull(const CMessageQueue *mq, const char *subExpression, long long offset, int maxNums){
-    CPullResult pullResult ;
-    memset(&pullResult,0, sizeof(CPullResult));
-    //ToDo, Add implement
+CPullResult Pull(CPullConsumer *consumer, const CMessageQueue *mq, const char *subExpression, long long offset, int maxNums) {
+    CPullResult pullResult;
+    memset(&pullResult, 0, sizeof(CPullResult));
+    MQMessageQueue messageQueue(mq->topic, mq->brokerName, mq->queueId);
+    PullResult cppPullResult;
+    cppPullResult = ((DefaultMQPullConsumer *) consumer)->pull(messageQueue, subExpression, offset, maxNums);
+
+    switch (cppPullResult.pullStatus) {
+        case FOUND: {
+            pullResult.pullStatus = E_FOUND;
+            pullResult.maxOffset = cppPullResult.maxOffset;
+            pullResult.minOffset = cppPullResult.minOffset;
+            pullResult.nextBeginOffset = cppPullResult.nextBeginOffset;
+            pullResult.size = cppPullResult.msgFoundList.size();
+            //Alloc memory to save the pointer to CPP MQMessageExt, which will be release by the CPP SDK core.
+            //Thus, this memory should be released by users using @ReleasePullResult
+            pullResult.msgFoundList = (CMessageExt **) malloc(pullResult.size * sizeof(CMessageExt *));
+            for (size_t i = 0; i < cppPullResult.msgFoundList.size(); i++) {
+                MQMessageExt *msg = const_cast<MQMessageExt *>(&cppPullResult.msgFoundList[i]);
+                pullResult.msgFoundList[i] = (CMessageExt *) (msg);
+            }
+            break;
+        }
+        case NO_NEW_MSG: {
+            pullResult.pullStatus = E_NO_NEW_MSG;
+            break;
+        }
+        case NO_MATCHED_MSG: {
+            pullResult.pullStatus = E_NO_MATCHED_MSG;
+            break;
+        }
+        case OFFSET_ILLEGAL: {
+            pullResult.pullStatus = E_OFFSET_ILLEGAL;
+            break;
+        }
+        case BROKER_TIMEOUT: {
+            pullResult.pullStatus = E_BROKER_TIMEOUT;
+            break;
+        }
+        default:
+            pullResult.pullStatus = E_NO_NEW_MSG;
+            break;
+
+    }
     return pullResult;
 }
+int ReleasePullResult(CPullResult pullResult) {
+    if (pullResult.size == 0 || pullResult.msgFoundList == NULL) {
+        return NULL_POINTER;
+    }
+    free((void *) pullResult.msgFoundList);
+    pullResult.msgFoundList = NULL;
+    return OK;
+}
 
 #ifdef __cplusplus
 };


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services