merlimat closed pull request #2010: Enable C++ message listener to receive
messages even if receiver queu…
URL: https://github.com/apache/incubator-pulsar/pull/2010
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/pulsar-client-cpp/lib/ClientImpl.cc
b/pulsar-client-cpp/lib/ClientImpl.cc
index d8d07e79e3..1d46cd94f7 100644
--- a/pulsar-client-cpp/lib/ClientImpl.cc
+++ b/pulsar-client-cpp/lib/ClientImpl.cc
@@ -82,7 +82,8 @@ ClientImpl::ClientImpl(const std::string& serviceUrl, const
ClientConfiguration&
#ifdef USE_LOG4CXX
if (!clientConfiguration.getLogConfFilePath().empty()) {
// A log4cxx log file was passed through deprecated parameter. Use
that to configure Log4CXX
-
LogUtils::setLoggerFactory(Log4CxxLogger::create(clientConfiguration.getLogConfFilePath()));
+ LogUtils::setLoggerFactory(
+
Log4CxxLoggerFactory::create(clientConfiguration.getLogConfFilePath()));
} else {
// Use default simple console logger
LogUtils::setLoggerFactory(SimpleLoggerFactory::create());
diff --git a/pulsar-client-cpp/lib/ConsumerImpl.cc
b/pulsar-client-cpp/lib/ConsumerImpl.cc
index a50394f6c6..498e332931 100644
--- a/pulsar-client-cpp/lib/ConsumerImpl.cc
+++ b/pulsar-client-cpp/lib/ConsumerImpl.cc
@@ -180,8 +180,12 @@ void ConsumerImpl::handleCreateConsumer(const
ClientConnectionPtr& cnx, Result r
}
LOG_DEBUG(getName() << "Send initial flow permits: " <<
config_.getReceiverQueueSize());
- if ((consumerTopicType_ == NonPartitioned || !firstTime) &&
config_.getReceiverQueueSize() != 0) {
- receiveMessages(cnx, config_.getReceiverQueueSize());
+ if (consumerTopicType_ == NonPartitioned || !firstTime) {
+ if (config_.getReceiverQueueSize() != 0) {
+ receiveMessages(cnx, config_.getReceiverQueueSize());
+ } else if (messageListener_) {
+ receiveMessages(cnx, 1);
+ }
}
consumerCreatedPromise_.setValue(shared_from_this());
} else {
@@ -285,7 +289,8 @@ void ConsumerImpl::messageReceived(const
ClientConnectionPtr& cnx, const proto::
numOfMessageReceived = receiveIndividualMessagesFromBatch(cnx, m);
} else {
// config_.getReceiverQueueSize() != 0 or waiting For ZeroQueueSize
Message`
- if (config_.getReceiverQueueSize() != 0) {
+ if (config_.getReceiverQueueSize() != 0 ||
+ (config_.getReceiverQueueSize() == 0 && messageListener_)) {
incomingMessages_.push(m);
} else {
Lock lock(mutex_);
diff --git a/pulsar-client-cpp/tests/ZeroQueueSizeTest.cc
b/pulsar-client-cpp/tests/ZeroQueueSizeTest.cc
new file mode 100644
index 0000000000..e1cd1fcadf
--- /dev/null
+++ b/pulsar-client-cpp/tests/ZeroQueueSizeTest.cc
@@ -0,0 +1,115 @@
+/**
+ * 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 <gtest/gtest.h>
+#include <pulsar/Client.h>
+#include <lib/Latch.h>
+#include "ConsumerTest.h"
+DECLARE_LOG_OBJECT()
+
+using namespace pulsar;
+
+static int totalMessages = 10;
+static int globalCount = 0;
+static std::string lookupUrl = "pulsar://localhost:8885";
+static std::string contentBase = "msg-";
+
+static void messageListenerFunction(Consumer consumer, const Message& msg,
Latch& latch) {
+ ASSERT_EQ(0, ConsumerTest::getNumOfMessagesInQueue(consumer));
+ std::ostringstream ss;
+ ss << contentBase << globalCount;
+ ASSERT_EQ(ss.str(), msg.getDataAsString());
+ globalCount++;
+ latch.countdown();
+ ASSERT_EQ(0, ConsumerTest::getNumOfMessagesInQueue(consumer));
+}
+
+TEST(ZeroQueueSizeTest, testProduceConsume) {
+ Client client(lookupUrl);
+ std::string topicName = "persistent://prop/unit/ns1/zero-queue-size";
+ std::string subName = "my-sub-name";
+
+ Producer producer;
+ Result result = client.createProducer(topicName, producer);
+ ASSERT_EQ(ResultOk, result);
+
+ Consumer consumer;
+ ConsumerConfiguration consConfig;
+ consConfig.setReceiverQueueSize(0);
+ result = client.subscribe(topicName, subName, consConfig, consumer);
+ ASSERT_EQ(ResultOk, result);
+
+ for (int i = 0; i < totalMessages; i++) {
+ std::ostringstream ss;
+ ss << contentBase << i;
+ Message msg = MessageBuilder().setContent(ss.str()).build();
+ result = producer.send(msg);
+ ASSERT_EQ(ResultOk, result);
+ }
+
+ for (int i = 0; i < totalMessages; i++) {
+ ASSERT_EQ(0, ConsumerTest::getNumOfMessagesInQueue(consumer));
+ std::ostringstream ss;
+ ss << contentBase << i;
+ Message receivedMsg;
+ consumer.receive(receivedMsg);
+ ASSERT_EQ(ss.str(), receivedMsg.getDataAsString());
+ ASSERT_EQ(0, ConsumerTest::getNumOfMessagesInQueue(consumer));
+ }
+
+ consumer.unsubscribe();
+ consumer.close();
+ producer.close();
+ client.close();
+}
+
+TEST(ZeroQueueSizeTest, testMessageListener) {
+ Client client(lookupUrl);
+ std::string topicName =
"persistent://prop/unit/ns/zero-queue-size-listener";
+ std::string subName = "my-sub-name";
+
+ Producer producer;
+ Result result = client.createProducer(topicName, producer);
+ ASSERT_EQ(ResultOk, result);
+
+ Consumer consumer;
+ ConsumerConfiguration consConfig;
+ consConfig.setReceiverQueueSize(0);
+ Latch latch(totalMessages);
+ consConfig.setMessageListener(boost::bind(messageListenerFunction, _1, _2,
latch));
+ result = client.subscribe(topicName, subName, consConfig, consumer);
+ ASSERT_EQ(ResultOk, result);
+
+ globalCount = 0;
+
+ for (int i = 0; i < totalMessages; i++) {
+ std::ostringstream ss;
+ ss << contentBase << i;
+ Message msg = MessageBuilder().setContent(ss.str()).build();
+ result = producer.send(msg);
+ ASSERT_EQ(ResultOk, result);
+ }
+
+ ASSERT_TRUE(latch.wait(milliseconds(30 * 1000)));
+ ASSERT_EQ(globalCount, totalMessages);
+
+ consumer.unsubscribe();
+ consumer.close();
+ producer.close();
+ client.close();
+}
----------------------------------------------------------------
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