@@ -136,10 +136,10 @@ public static Map<String, Object> producerProps(String brokers) {
136136 * @param <V> the value type.
137137 * @return the record.
138138 * @throws IllegalStateException if exactly one record is not received.
139- * @see #getSingleRecord(Consumer, String, long )
139+ * @see #getSingleRecord(Consumer, String, Duration )
140140 */
141141 public static <K , V > ConsumerRecord <K , V > getSingleRecord (Consumer <K , V > consumer , String topic ) {
142- return getSingleRecord (consumer , topic , 60000 ); // NOSONAR magic #
142+ return getSingleRecord (consumer , topic , Duration . ofSeconds ( 60 ) ); // NOSONAR magic #
143143 }
144144
145145 /**
@@ -152,14 +152,31 @@ public static <K, V> ConsumerRecord<K, V> getSingleRecord(Consumer<K, V> consume
152152 * @return the record.
153153 * @throws IllegalStateException if exactly one record is not received.
154154 * @since 2.0
155+ * @deprecated in favor of {@link #getSingleRecord(Consumer, String, Duration)}
155156 */
157+ @ Deprecated
156158 public static <K , V > ConsumerRecord <K , V > getSingleRecord (Consumer <K , V > consumer , String topic , long timeout ) {
157- long expire = System .currentTimeMillis () + timeout ;
159+ return getSingleRecord (consumer , topic , Duration .ofMillis (timeout ));
160+ }
161+
162+ /**
163+ * Poll the consumer, expecting a single record for the specified topic.
164+ * @param consumer the consumer.
165+ * @param topic the topic.
166+ * @param timeout max duration to wait for records; forwarded to {@link Consumer#poll(Duration)}.
167+ * @param <K> the key type.
168+ * @param <V> the value type.
169+ * @return the record.
170+ * @throws IllegalStateException if exactly one record is not received.
171+ * @since 2.9.3
172+ */
173+ public static <K , V > ConsumerRecord <K , V > getSingleRecord (Consumer <K , V > consumer , String topic , Duration timeout ) {
174+ long expire = System .currentTimeMillis () + timeout .toMillis ();
158175 ConsumerRecords <K , V > received ;
159176 Iterator <ConsumerRecord <K , V >> iterator ;
160- long remaining = timeout ;
177+ long remaining = timeout . toMillis () ;
161178 do {
162- received = getRecords (consumer , remaining );
179+ received = getRecords (consumer , Duration . ofMillis ( remaining ) );
163180 iterator = received .records (topic ).iterator ();
164181 Map <TopicPartition , Long > reset = new HashMap <>();
165182 received .forEach (rec -> {
@@ -198,11 +215,31 @@ public static <K, V> ConsumerRecord<K, V> getSingleRecord(Consumer<K, V> consume
198215 * @param timeout the timeout.
199216 * @return the record or null if no record received.
200217 * @since 2.3
218+ * @deprecated in favor of {@link #getOneRecord(String, String, String, int, boolean, boolean, Duration)}
219+ */
220+ @ Nullable
221+ @ Deprecated
222+ public static ConsumerRecord <?, ?> getOneRecord (String brokerAddresses , String group , String topic , int partition ,
223+ boolean seekToLast , boolean commit , long timeout ) {
224+ return getOneRecord (brokerAddresses , group , topic , partition , seekToLast , commit , Duration .ofMillis (timeout ));
225+ }
226+
227+ /**
228+ * Get a single record for the group from the topic/partition. Optionally, seeking to the current last record.
229+ * @param brokerAddresses the broker address(es).
230+ * @param group the group.
231+ * @param topic the topic.
232+ * @param partition the partition.
233+ * @param seekToLast true to fetch an existing last record, if present.
234+ * @param commit commit offset after polling or not.
235+ * @param timeout the timeout.
236+ * @return the record or null if no record received.
237+ * @since 2.9.3
201238 */
202239 @ Nullable
203240 @ SuppressWarnings ({ "rawtypes" , "unchecked" })
204241 public static ConsumerRecord <?, ?> getOneRecord (String brokerAddresses , String group , String topic , int partition ,
205- boolean seekToLast , boolean commit , long timeout ) {
242+ boolean seekToLast , boolean commit , Duration timeout ) {
206243
207244 Map <String , Object > consumerConfig = consumerProps (brokerAddresses , group , "false" );
208245 consumerConfig .put (ConsumerConfig .MAX_POLL_RECORDS_CONFIG , 1 );
@@ -215,7 +252,7 @@ public static <K, V> ConsumerRecord<K, V> getSingleRecord(Consumer<K, V> consume
215252 consumer .seek (topicPart , consumer .position (topicPart ) - 1 );
216253 }
217254 }
218- ConsumerRecords <?, ?> records = consumer .poll (Duration . ofMillis ( timeout ) );
255+ ConsumerRecords <?, ?> records = consumer .poll (timeout );
219256 ConsumerRecord <?, ?> record = records .count () == 1 ? records .iterator ().next () : null ;
220257 if (record != null && commit ) {
221258 consumer .commitSync ();
@@ -298,44 +335,74 @@ public static Map<TopicPartition, Long> getEndOffsets(Consumer<?, ?> consumer, S
298335 * @param <K> the key type.
299336 * @param <V> the value type.
300337 * @return the records.
301- * @see #getRecords(Consumer, long )
338+ * @see #getRecords(Consumer, Duration )
302339 */
303340 public static <K , V > ConsumerRecords <K , V > getRecords (Consumer <K , V > consumer ) {
304- return getRecords (consumer , 60000 ); // NOSONAR magic #
341+ return getRecords (consumer , Duration . ofSeconds ( 60 ) ); // NOSONAR magic #
305342 }
306343
307344 /**
308345 * Poll the consumer for records.
309346 * @param consumer the consumer.
310- * @param timeout max time in milliseconds to wait for records; forwarded to
311- * {@link Consumer#poll(long)}.
347+ * @param timeout max time in milliseconds to wait for records; forwarded to {@link Consumer#poll(long)}.
312348 * @param <K> the key type.
313349 * @param <V> the value type.
314350 * @return the records.
315351 * @throws IllegalStateException if the poll returns null (since 2.3.4).
316352 * @since 2.0
353+ * @deprecated in favor of {@link #getRecords(Consumer, Duration)}
317354 */
355+ @ Deprecated
318356 public static <K , V > ConsumerRecords <K , V > getRecords (Consumer <K , V > consumer , long timeout ) {
319357 return getRecords (consumer , timeout , -1 );
320358 }
321359
322360 /**
323361 * Poll the consumer for records.
324362 * @param consumer the consumer.
325- * @param timeout max time in milliseconds to wait for records; forwarded to
326- * {@link Consumer#poll(long)}.
363+ * @param timeout max time in milliseconds to wait for records; forwarded to {@link Consumer#poll(Duration)}.
364+ * @param <K> the key type.
365+ * @param <V> the value type.
366+ * @return the records.
367+ * @throws IllegalStateException if the poll returns null (since 2.3.4).
368+ * @since 2.9.3
369+ */
370+ public static <K , V > ConsumerRecords <K , V > getRecords (Consumer <K , V > consumer , Duration timeout ) {
371+ return getRecords (consumer , timeout , -1 );
372+ }
373+
374+ /**
375+ * Poll the consumer for records.
376+ * @param consumer the consumer.
377+ * @param timeout max time in milliseconds to wait for records; forwarded to {@link Consumer#poll(long)}.
327378 * @param <K> the key type.
328379 * @param <V> the value type.
329- * @param minRecords wait until the timeout or at least this number of receords are
330- * received.
380+ * @param minRecords wait until the timeout or at least this number of records are received.
331381 * @return the records.
332382 * @throws IllegalStateException if the poll returns null.
333383 * @since 2.4.2
384+ * @deprecated in favor of {#{@link #getRecords(Consumer, Duration, int)}}
334385 */
386+ @ Deprecated
335387 public static <K , V > ConsumerRecords <K , V > getRecords (Consumer <K , V > consumer , long timeout , int minRecords ) {
388+ return getRecords (consumer , Duration .ofMillis (timeout ), minRecords );
389+ }
390+
391+ /**
392+ * Poll the consumer for records.
393+ * @param consumer the consumer.
394+ * @param timeout max time in milliseconds to wait for records; forwarded to {@link Consumer#poll(Duration)}.
395+ * @param <K> the key type.
396+ * @param <V> the value type.
397+ * @param minRecords wait until the timeout or at least this number of records are received.
398+ * @return the records.
399+ * @throws IllegalStateException if the poll returns null.
400+ * @since 2.9.3
401+ */
402+ public static <K , V > ConsumerRecords <K , V > getRecords (Consumer <K , V > consumer , Duration timeout , int minRecords ) {
336403 logger .debug ("Polling..." );
337404 Map <TopicPartition , List <ConsumerRecord <K , V >>> records = new HashMap <>();
338- long remaining = timeout ;
405+ long remaining = timeout . toMillis () ;
339406 int count = 0 ;
340407 do {
341408 long t1 = System .currentTimeMillis ();
0 commit comments