@@ -51,6 +51,10 @@ static const std::string CFG_DEAD_LETTER_POLICY = "deadLetterPolicy";
5151static const std::string CFG_DLQ_POLICY_TOPIC = " deadLetterTopic" ;
5252static const std::string CFG_DLQ_POLICY_MAX_REDELIVER_COUNT = " maxRedeliverCount" ;
5353static const std::string CFG_DLQ_POLICY_INIT_SUB_NAME = " initialSubscriptionName" ;
54+ static const std::string CFG_BATCH_RECEIVE_POLICY = " batchReceivePolicy" ;
55+ static const std::string CFG_BATCH_RECEIVE_POLICY_MAX_NUM_MESSAGES = " maxNumMessages" ;
56+ static const std::string CFG_BATCH_RECEIVE_POLICY_MAX_NUM_BYTES = " maxNumBytes" ;
57+ static const std::string CFG_BATCH_RECEIVE_POLICY_TIMEOUT_MS = " timeoutMs" ;
5458
5559static const std::map<std::string, pulsar_consumer_type> SUBSCRIPTION_TYPE = {
5660 {" Exclusive" , pulsar_ConsumerExclusive},
@@ -74,7 +78,7 @@ static const std::map<std::string, pulsar_consumer_crypto_failure_action> CONSUM
7478
7579void FinalizeListenerCallback (Napi::Env env, MessageListenerCallback *cb, void *) { delete cb; }
7680
77- ConsumerConfig::ConsumerConfig (const Napi::Object &consumerConfig, pulsar_message_listener messageListener )
81+ ConsumerConfig::ConsumerConfig ()
7882 : topic(" " ),
7983 topicsPattern(" " ),
8084 subscription(" " ),
@@ -83,7 +87,10 @@ ConsumerConfig::ConsumerConfig(const Napi::Object &consumerConfig, pulsar_messag
8387 listener(nullptr ) {
8488 this ->cConsumerConfig = std::shared_ptr<pulsar_consumer_configuration_t >(
8589 pulsar_consumer_configuration_create (), pulsar_consumer_configuration_free);
90+ }
8691
92+ void ConsumerConfig::InitConfig (std::shared_ptr<ThreadSafeDeferred> deferred,
93+ const Napi::Object &consumerConfig, pulsar_message_listener messageListener) {
8794 if (consumerConfig.Has (CFG_TOPIC) && consumerConfig.Get (CFG_TOPIC).IsString ()) {
8895 this ->topic = consumerConfig.Get (CFG_TOPIC).ToString ().Utf8Value ();
8996 }
@@ -101,9 +108,21 @@ ConsumerConfig::ConsumerConfig(const Napi::Object &consumerConfig, pulsar_messag
101108 this ->topicsPattern = consumerConfig.Get (CFG_TOPICS_PATTERN).ToString ().Utf8Value ();
102109 }
103110
111+ if (this ->topic .empty () && this ->topics .size () == 0 && this ->topicsPattern .empty ()) {
112+ deferred->Reject (
113+ std::string (" Topic, topics or topicsPattern is required and must be specified as a string when "
114+ " creating consumer" ));
115+ return ;
116+ }
117+
104118 if (consumerConfig.Has (CFG_SUBSCRIPTION) && consumerConfig.Get (CFG_SUBSCRIPTION).IsString ()) {
105119 this ->subscription = consumerConfig.Get (CFG_SUBSCRIPTION).ToString ().Utf8Value ();
106120 }
121+ if (subscription.empty ()) {
122+ deferred->Reject (
123+ std::string (" Subscription is required and must be specified as a string when creating consumer" ));
124+ return ;
125+ }
107126
108127 if (consumerConfig.Has (CFG_SUBSCRIPTION_TYPE) && consumerConfig.Get (CFG_SUBSCRIPTION_TYPE).IsString ()) {
109128 std::string subscriptionType = consumerConfig.Get (CFG_SUBSCRIPTION_TYPE).ToString ().Utf8Value ();
@@ -139,18 +158,25 @@ ConsumerConfig::ConsumerConfig(const Napi::Object &consumerConfig, pulsar_messag
139158
140159 if (consumerConfig.Has (CFG_ACK_TIMEOUT) && consumerConfig.Get (CFG_ACK_TIMEOUT).IsNumber ()) {
141160 this ->ackTimeoutMs = consumerConfig.Get (CFG_ACK_TIMEOUT).ToNumber ().Int64Value ();
142- if (this ->ackTimeoutMs == 0 || this ->ackTimeoutMs >= MIN_ACK_TIMEOUT_MILLIS) {
143- pulsar_consumer_set_unacked_messages_timeout_ms (this ->cConsumerConfig .get (), this ->ackTimeoutMs );
161+ if (this ->ackTimeoutMs != 0 && ackTimeoutMs < MIN_ACK_TIMEOUT_MILLIS) {
162+ std::string msg (" Ack timeout should be 0 or greater than or equal to " +
163+ std::to_string (MIN_ACK_TIMEOUT_MILLIS));
164+ deferred->Reject (msg);
165+ return ;
144166 }
167+ pulsar_consumer_set_unacked_messages_timeout_ms (this ->cConsumerConfig .get (), this ->ackTimeoutMs );
145168 }
146169
147170 if (consumerConfig.Has (CFG_NACK_REDELIVER_TIMEOUT) &&
148171 consumerConfig.Get (CFG_NACK_REDELIVER_TIMEOUT).IsNumber ()) {
149172 this ->nAckRedeliverTimeoutMs = consumerConfig.Get (CFG_NACK_REDELIVER_TIMEOUT).ToNumber ().Int64Value ();
150- if (this ->nAckRedeliverTimeoutMs >= 0 ) {
151- pulsar_configure_set_negative_ack_redelivery_delay_ms (this ->cConsumerConfig .get (),
152- this ->nAckRedeliverTimeoutMs );
173+ if (nAckRedeliverTimeoutMs < 0 ) {
174+ std::string msg (" NAck timeout should be greater than or equal to zero" );
175+ deferred->Reject (msg);
176+ return ;
153177 }
178+ pulsar_configure_set_negative_ack_redelivery_delay_ms (this ->cConsumerConfig .get (),
179+ this ->nAckRedeliverTimeoutMs );
154180 }
155181
156182 if (consumerConfig.Has (CFG_RECV_QUEUE) && consumerConfig.Get (CFG_RECV_QUEUE).IsNumber ()) {
@@ -265,6 +291,39 @@ ConsumerConfig::ConsumerConfig(const Napi::Object &consumerConfig, pulsar_messag
265291 }
266292 pulsar_consumer_configuration_set_dlq_policy (this ->cConsumerConfig .get (), &dlq_policy);
267293 }
294+
295+ if (consumerConfig.Has (CFG_BATCH_RECEIVE_POLICY) &&
296+ consumerConfig.Get (CFG_BATCH_RECEIVE_POLICY).IsObject ()) {
297+ Napi::Object propObj = consumerConfig.Get (CFG_BATCH_RECEIVE_POLICY).ToObject ();
298+ int maxNumMessages = -1 ;
299+ if (propObj.Has (CFG_BATCH_RECEIVE_POLICY_MAX_NUM_MESSAGES) &&
300+ propObj.Get (CFG_BATCH_RECEIVE_POLICY_MAX_NUM_MESSAGES).IsNumber ()) {
301+ maxNumMessages = propObj.Get (CFG_BATCH_RECEIVE_POLICY_MAX_NUM_MESSAGES).ToNumber ().Int32Value ();
302+ }
303+ int maxNumBytes = 10 * 1024 * 1024 ;
304+ if (propObj.Has (CFG_BATCH_RECEIVE_POLICY_MAX_NUM_BYTES) &&
305+ propObj.Get (CFG_BATCH_RECEIVE_POLICY_MAX_NUM_BYTES).IsNumber ()) {
306+ maxNumBytes = propObj.Get (CFG_BATCH_RECEIVE_POLICY_MAX_NUM_BYTES).ToNumber ().Int64Value ();
307+ }
308+ int timeoutMs = 100 ;
309+ if (propObj.Has (CFG_BATCH_RECEIVE_POLICY_TIMEOUT_MS) &&
310+ propObj.Get (CFG_BATCH_RECEIVE_POLICY_TIMEOUT_MS).IsNumber ()) {
311+ timeoutMs = propObj.Get (CFG_BATCH_RECEIVE_POLICY_TIMEOUT_MS).ToNumber ().Int64Value ();
312+ }
313+ if (maxNumMessages <= 0 && maxNumBytes <= 0 && timeoutMs <= 0 ) {
314+ std::string msg (" At least one of maxNumMessages, maxNumBytes and timeoutMs must be specified." );
315+ deferred->Reject (msg);
316+ return ;
317+ }
318+ pulsar_consumer_batch_receive_policy_t batch_receive_policy{maxNumMessages, maxNumBytes, timeoutMs};
319+ int result = pulsar_consumer_configuration_set_batch_receive_policy (this ->cConsumerConfig .get (),
320+ &batch_receive_policy);
321+ if (result == -1 ) {
322+ std::string msg (" Set batch receive policy failed: C client returned failure" );
323+ deferred->Reject (msg);
324+ return ;
325+ }
326+ }
268327}
269328
270329ConsumerConfig::~ConsumerConfig () {
0 commit comments