23
23
import java .util .Collections ;
24
24
import java .util .HashMap ;
25
25
import java .util .Iterator ;
26
+ import java .util .LinkedHashSet ;
26
27
import java .util .List ;
27
28
import java .util .Map ;
28
29
import java .util .Set ;
@@ -120,6 +121,8 @@ public class KafkaMessageSource<K, V> extends AbstractMessageSource<Object> impl
120
121
121
122
private final ConsumerProperties consumerProperties ;
122
123
124
+ private final Collection <TopicPartition > assignedPartitions = new LinkedHashSet <>();
125
+
123
126
private Duration pollTimeout ;
124
127
125
128
private RecordMessageConverter messageConverter = new MessagingMessageConverter ();
@@ -136,8 +139,6 @@ public class KafkaMessageSource<K, V> extends AbstractMessageSource<Object> impl
136
139
137
140
private volatile Consumer <K , V > consumer ;
138
141
139
- private volatile Collection <TopicPartition > assignedPartitions = new ArrayList <>();
140
-
141
142
private volatile boolean pausing ;
142
143
143
144
private volatile boolean paused ;
@@ -233,6 +234,24 @@ public KafkaMessageSource(ConsumerFactory<K, V> consumerFactory,
233
234
this .commitTimeout = consumerProperties .getSyncCommitTimeout ();
234
235
}
235
236
237
+ /**
238
+ * Return the currently assigned partitions.
239
+ * @return the partitions.
240
+ * @since 3.2.2
241
+ */
242
+ public Collection <TopicPartition > getAssignedPartitions () {
243
+ return Collections .unmodifiableCollection (this .assignedPartitions );
244
+ }
245
+
246
+ /**
247
+ * Return true if the source is currently paused.
248
+ * @return true if paused.
249
+ * @since 3.2.2
250
+ */
251
+ public boolean isPaused () {
252
+ return this .paused ;
253
+ }
254
+
236
255
@ Override
237
256
protected void onInit () {
238
257
if (!StringUtils .hasText (this .consumerProperties .getClientId ())) {
@@ -478,7 +497,7 @@ protected void createConsumer() {
478
497
479
498
@ Override
480
499
public void onPartitionsRevoked (Collection <TopicPartition > partitions ) {
481
- KafkaMessageSource .this .assignedPartitions .clear ( );
500
+ KafkaMessageSource .this .assignedPartitions .removeAll ( partitions );
482
501
if (KafkaMessageSource .this .logger .isInfoEnabled ()) {
483
502
KafkaMessageSource .this .logger
484
503
.info ("Partitions revoked: " + partitions );
@@ -495,9 +514,27 @@ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
495
514
496
515
}
497
516
517
+ @ Override
518
+ public void onPartitionsLost (Collection <TopicPartition > partitions ) {
519
+ if (providedRebalanceListener != null ) {
520
+ if (isConsumerAware ) {
521
+ ((ConsumerAwareRebalanceListener ) providedRebalanceListener ).onPartitionsLost (partitions );
522
+ }
523
+ else {
524
+ providedRebalanceListener .onPartitionsLost (partitions );
525
+ }
526
+ }
527
+ onPartitionsRevoked (partitions );
528
+ }
529
+
498
530
@ Override
499
531
public void onPartitionsAssigned (Collection <TopicPartition > partitions ) {
500
- KafkaMessageSource .this .assignedPartitions = new ArrayList <>(partitions );
532
+ KafkaMessageSource .this .assignedPartitions .addAll (partitions );
533
+ if (KafkaMessageSource .this .paused ) {
534
+ KafkaMessageSource .this .consumer .pause (KafkaMessageSource .this .assignedPartitions );
535
+ KafkaMessageSource .this .logger .warn ("Paused consumer resumed by Kafka due to rebalance; "
536
+ + "consumer paused again, so the initial poll() will never return any records" );
537
+ }
501
538
if (KafkaMessageSource .this .logger .isInfoEnabled ()) {
502
539
KafkaMessageSource .this .logger
503
540
.info ("Partitions assigned: " + partitions );
@@ -524,7 +561,7 @@ else if (this.consumerProperties.getTopicPartitions() != null) {
524
561
.map (TopicPartitionOffset ::getTopicPartition )
525
562
.collect (Collectors .toList ());
526
563
this .consumer .assign (topicPartitionsToAssign );
527
- this .assignedPartitions = new ArrayList <> (topicPartitionsToAssign );
564
+ this .assignedPartitions . addAll (topicPartitionsToAssign );
528
565
529
566
TopicPartitionOffset [] partitions = this .consumerProperties .getTopicPartitions ();
530
567
0 commit comments