@@ -379,10 +379,14 @@ class CPPKAFKA_API Consumer : public KafkaHandleBase {
379
379
* This can return one or more messages
380
380
*
381
381
* \param max_batch_size The maximum amount of messages expected
382
+ * \param alloc The optionally supplied allocator for allocating messages
382
383
*
383
384
* \return A list of messages
384
385
*/
385
- MessageList poll_batch (size_t max_batch_size);
386
+ template <typename Allocator>
387
+ std::vector<Message, Allocator> poll_batch (size_t max_batch_size,
388
+ const Allocator& alloc);
389
+ std::vector<Message> poll_batch (size_t max_batch_size);
386
390
387
391
/* *
388
392
* \brief Polls for a batch of messages
@@ -391,10 +395,16 @@ class CPPKAFKA_API Consumer : public KafkaHandleBase {
391
395
*
392
396
* \param max_batch_size The maximum amount of messages expected
393
397
* \param timeout The timeout for this operation
398
+ * \param alloc The optionally supplied allocator for allocating messages
394
399
*
395
400
* \return A list of messages
396
401
*/
397
- MessageList poll_batch (size_t max_batch_size, std::chrono::milliseconds timeout);
402
+ template <typename Allocator>
403
+ std::vector<Message, Allocator> poll_batch (size_t max_batch_size,
404
+ std::chrono::milliseconds timeout,
405
+ const Allocator& alloc);
406
+ std::vector<Message> poll_batch (size_t max_batch_size,
407
+ std::chrono::milliseconds timeout);
398
408
399
409
/* *
400
410
* \brief Get the global event queue servicing this consumer corresponding to
@@ -430,6 +440,7 @@ class CPPKAFKA_API Consumer : public KafkaHandleBase {
430
440
private:
431
441
static void rebalance_proxy (rd_kafka_t *handle, rd_kafka_resp_err_t error,
432
442
rd_kafka_topic_partition_list_t *partitions, void *opaque);
443
+ static Queue get_queue (rd_kafka_queue_t * handle);
433
444
void close ();
434
445
void commit (const Message& msg, bool async);
435
446
void commit (const TopicPartitionList* topic_partitions, bool async);
@@ -440,6 +451,30 @@ class CPPKAFKA_API Consumer : public KafkaHandleBase {
440
451
RebalanceErrorCallback rebalance_error_callback_;
441
452
};
442
453
454
+ // Implementations
455
+ template <typename Allocator>
456
+ std::vector<Message, Allocator> Consumer::poll_batch (size_t max_batch_size,
457
+ const Allocator& alloc) {
458
+ return poll_batch (max_batch_size, get_timeout (), alloc);
459
+ }
460
+
461
+ template <typename Allocator>
462
+ std::vector<Message, Allocator> Consumer::poll_batch (size_t max_batch_size,
463
+ std::chrono::milliseconds timeout,
464
+ const Allocator& alloc) {
465
+ std::vector<rd_kafka_message_t *> raw_messages (max_batch_size);
466
+ // Note that this will leak the queue when using rdkafka < 0.11.5 (see get_queue comment)
467
+ Queue queue (get_queue (rd_kafka_queue_get_consumer (get_handle ())));
468
+ ssize_t result = rd_kafka_consume_batch_queue (queue.get_handle () , timeout.count (), raw_messages.data (),
469
+ raw_messages.size ());
470
+ if (result == -1 ) {
471
+ check_error (rd_kafka_last_error ());
472
+ // on the off-chance that check_error() does not throw an error
473
+ return std::vector<Message, Allocator>(alloc);
474
+ }
475
+ return std::vector<Message, Allocator>(raw_messages.begin (), raw_messages.begin () + result, alloc);
476
+ }
477
+
443
478
} // cppkafka
444
479
445
480
#endif // CPP_KAFKA_CONSUMER_H
0 commit comments