vongosling closed pull request #10: Support order producer
URL: https://github.com/apache/rocketmq-client-cpp/pull/10
 
 
   

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/include/CProducer.h b/include/CProducer.h
index c8bd029..9cfb0cd 100644
--- a/include/CProducer.h
+++ b/include/CProducer.h
@@ -20,14 +20,13 @@
 
 #include "CMessage.h"
 #include "CSendResult.h"
-
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-//typedef struct _CProducer_ _CProducer;
-typedef struct CProducer CProducer;
 
+typedef struct CProducer CProducer;
+typedef int(*QueueSelectorCallback)(int size, CMessage *msg, void *arg);
 
 CProducer *CreateProducer(const char *groupId);
 int DestroyProducer(CProducer *producer);
@@ -48,6 +47,7 @@ int SetProducerMaxMessageSize(CProducer *producer, int size);
 
 int SendMessageSync(CProducer *producer, CMessage *msg, CSendResult *result);
 int SendMessageOneway(CProducer *producer,CMessage *msg);
+int SendMessageOrderly(CProducer *producer, CMessage *msg, 
QueueSelectorCallback callback, void *arg, int autoRetryTimes, CSendResult 
*result);
 #ifdef __cplusplus
 };
 #endif
diff --git a/src/extern/CProducer.cpp b/src/extern/CProducer.cpp
index 0bf307a..a97f1f1 100644
--- a/src/extern/CProducer.cpp
+++ b/src/extern/CProducer.cpp
@@ -19,12 +19,31 @@
 #include "CProducer.h"
 #include "CCommon.h"
 #include <string.h>
-
+#include "CMessage.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 using namespace rocketmq;
+
+class SelectMessageQueue : public MessageQueueSelector {
+ public:
+    SelectMessageQueue(QueueSelectorCallback callback){
+        m_pCallback = callback;
+    }
+
+    MQMessageQueue select(const std::vector<MQMessageQueue> &mqs,
+                        const MQMessage &msg, void *arg) {
+        CMessage * message = (CMessage *) &msg;
+        //Get the index of sending MQMessageQueue through callback function.
+        int index = m_pCallback(mqs.size(),message,arg);
+        return mqs[index];
+    }
+  private:
+    QueueSelectorCallback m_pCallback;
+};
+
+
 CProducer *CreateProducer(const char *groupId) {
     if (groupId == NULL) {
         return NULL;
@@ -103,6 +122,23 @@ int SendMessageOneway(CProducer *producer,CMessage *msg) {
     return OK;
 }
 
+int SendMessageOrderly(CProducer *producer, CMessage *msg, 
QueueSelectorCallback callback, void *arg, int autoRetryTimes, CSendResult 
*result) {
+    if(producer == NULL || msg == NULL || callback == NULL || arg == NULL || 
result == NULL){
+        return NULL_POINTER;
+    }
+    DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *) producer;
+    MQMessage *message = (MQMessage *) msg;
+    //Constructing SelectMessageQueue objects through function pointer callback
+    SelectMessageQueue selectMessageQueue(callback);
+    SendResult sendResult = 
defaultMQProducer->send(*message,&selectMessageQueue,arg,autoRetryTimes);
+    //Convert SendStatus to CSendStatus
+    result->sendStatus = CSendStatus((int)sendResult.getSendStatus());
+    result->offset = sendResult.getQueueOffset();
+    strncpy(result->msgId, sendResult.getMsgId().c_str(), 
MAX_MESSAGE_ID_LENGTH - 1);
+    result->msgId[MAX_MESSAGE_ID_LENGTH - 1] = 0;
+    return OK;
+}
+
 int SetProducerGroupName(CProducer *producer, const char *groupName) {
     if (producer == NULL) {
         return NULL_POINTER;


 

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to