[pulsar-client-cpp] When I use the `consumer.receiveAsync()`, how to properly confirm message?
See original GitHub issueI want use consumer.receiveAsync()
to receive a single message from topic. but i don’t know how to confirm message properly, because the receiveAsync()
's callback function don’t provide a consumer target as parameter.
// >>> lib/Consumer.cc#receiveAsync
void Consumer::receiveAsync(ReceiveCallback callback) {
if (!impl_) {
Message msg;
callback(ResultConsumerNotInitialized, msg);
return;
}
impl_->receiveAsync(callback);
}
// callback function
// >>> include/pulsar/ConsumerConfiguration.h#ReceiveCallback
typedef std::function<void(Result, const Message& msg)> ReceiveCallback;
OR the message has already been confirmed before callback function called? My Demo. i don’t know how to properly confirm message. confused.
#include <iostream>
#include <pulsar/Client.h>
#include <lib/LogUtils.h>
DECLARE_LOG_OBJECT()
using namespace pulsar;
using namespace std;
void callback(Result code, const Message& msg) {
cout << "Received code: " << code << "Msg Length" << msg.getLength() << endl;
// consumer.acknowledge(? messageId ); ?
}
int main() {
Client client("pulsar://localhost:6650");
Consumer consumer;
ConsumerConfiguration config;
config.setConsumerType(ConsumerShared);
Result result =
client.subscribe("persistent://HJ13/Test/performance_test", "consumerRecvAsync", config, consumer);
if (result != ResultOk) {
LOG_ERROR("Failed to subscribe: " << result);
return -1;
}
consumer.receiveAsync(callback);
// consumer.acknowledge(? messageId ); ?
// Wait
int n;
std::cin >> n;
client.close();
}
I want confirm message like pulsar-client-cpp/examples/SampleConsumerListener.cc
...
void listener(Consumer consumer, const Message& msg) {
LOG_INFO("Got message " << msg << " with content '" << msg.getDataAsString() << "'");
consumer.acknowledge(msg.getMessageId());
}
...
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
pulsar-client-cpp: pulsar::Consumer Class Reference
receiveAsync() should be called subsequently once callback gets completed with received message. Else it creates backlog of receive requests in the application.
Read more >Consumer (Pulsar Client :: API 2.4.2 API) - Apache Pulsar
Retrieves a message when it will be available and completes CompletableFuture with received message. receiveAsync() should be called subsequently once returned ...
Read more >Pulsar Java client
You can use a Pulsar Java client to create the Java producer, consumer, readers and TableView of messages and to perform administrative tasks....
Read more >Pulsar C++ client
You can use a Pulsar C++ client to create producers, consumers, and readers. For Pulsar features that C++ clients support, see Client Feature...
Read more >The Pulsar Java client
The Pulsar Java client can be used both to create Java producers, consumers, and readers of messages and to perform administrative tasks.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s a thing about API design. Though both
MessageListener
andReceiveCallback
arestd::function
callbacks, they’re much different. Let’s see what Java API does first.The
MessageListener
is a real callback and it’s noted that the callback is responsible for asking messages.There’s no
ReceiveCallback
in Java client becausereceiveAsync
just returns a future ofMessage
. C++ client has no future before C++11 while it seems that the Pulsar C++ client is written with C++03 initially. Even with C++11, thestd::future
is not so convenient as Java’sCompletableFuture
.Therefore, C++ client API usually passes an extra callback argument
std::function<Result, const T&>
to replace Java API’sCompletableFuture<T>
.The
receiveAsync
is only responsible for processing the future ofMessage
but not how you acknowledge it. For example, you can also capture a list ofMessageId
and acknowledge it later.Here we don’t acknowledge each message because in failover subscription mode, it’s unnecessary and inefficient. Though you can set
AckGrouingTimeMs
andAckGroupingMaxSize
now, the above code is just an example. What I want to say is:receiveAsync
only focus on the received message in the future.MessageListener
requires you to handle the acknowledgement.The difference makes that
MessageListener
has aConsumer
parameter whileReceiveCallback
doesn’t.Use lambda capture