@@ -662,6 +662,14 @@ private final class ListenerConsumer implements SchedulingAwareRunnable, Consume
662
662
663
663
private final boolean stopImmediate = this .containerProperties .isStopImmediate ();
664
664
665
+ private final Set <TopicPartition > pausedPartitions = new HashSet <>();
666
+
667
+ private final Map <TopicPartition , Long > lastReceivePartition ;
668
+
669
+ private final Map <TopicPartition , Long > lastAlertPartition ;
670
+
671
+ private final Map <TopicPartition , Boolean > wasIdlePartition ;
672
+
665
673
private Map <TopicPartition , OffsetMetadata > definedPartitions ;
666
674
667
675
private int count ;
@@ -676,10 +684,6 @@ private final class ListenerConsumer implements SchedulingAwareRunnable, Consume
676
684
677
685
private long lastAlertAt = this .lastReceive ;
678
686
679
- private final Map <TopicPartition , Long > lastReceivePartition ;
680
-
681
- private final Map <TopicPartition , Long > lastAlertPartition ;
682
-
683
687
private long nackSleep = -1 ;
684
688
685
689
private int nackIndex ;
@@ -696,8 +700,6 @@ private final class ListenerConsumer implements SchedulingAwareRunnable, Consume
696
700
697
701
private boolean wasIdle ;
698
702
699
- private final Map <TopicPartition , Boolean > wasIdlePartition ;
700
-
701
703
private boolean batchFailed ;
702
704
703
705
private volatile boolean consumerPaused ;
@@ -706,8 +708,6 @@ private final class ListenerConsumer implements SchedulingAwareRunnable, Consume
706
708
707
709
private volatile long lastPoll = System .currentTimeMillis ();
708
710
709
- private final Set <TopicPartition > pausedPartitions ;
710
-
711
711
@ SuppressWarnings (UNCHECKED )
712
712
ListenerConsumer (GenericMessageListener <?> listener , ListenerType listenerType ) {
713
713
Properties consumerProperties = propertiesFromProperties ();
@@ -795,7 +795,6 @@ else if (listener instanceof MessageListener) {
795
795
this .lastReceivePartition = new HashMap <>();
796
796
this .lastAlertPartition = new HashMap <>();
797
797
this .wasIdlePartition = new HashMap <>();
798
- this .pausedPartitions = new HashSet <>();
799
798
}
800
799
801
800
private Properties propertiesFromProperties () {
@@ -2918,12 +2917,15 @@ private class ListenerConsumerRebalanceListener implements ConsumerRebalanceList
2918
2917
this .userListener instanceof ConsumerAwareRebalanceListener
2919
2918
? (ConsumerAwareRebalanceListener ) this .userListener : null ;
2920
2919
2920
+ private final Collection <TopicPartition > revoked = new LinkedList <>();
2921
+
2921
2922
ListenerConsumerRebalanceListener () {
2922
2923
}
2923
2924
2924
2925
@ Override
2925
2926
public void onPartitionsRevoked (Collection <TopicPartition > partitions ) {
2926
2927
try {
2928
+ this .revoked .addAll (partitions );
2927
2929
if (this .consumerAwareListener != null ) {
2928
2930
this .consumerAwareListener .onPartitionsRevokedBeforeCommit (ListenerConsumer .this .consumer ,
2929
2931
partitions );
@@ -2961,11 +2963,7 @@ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
2961
2963
2962
2964
@ Override
2963
2965
public void onPartitionsAssigned (Collection <TopicPartition > partitions ) {
2964
- if (ListenerConsumer .this .consumerPaused ) {
2965
- ListenerConsumer .this .consumer .pause (partitions );
2966
- ListenerConsumer .this .logger .warn ("Paused consumer resumed by Kafka due to rebalance; "
2967
- + "consumer paused again, so the initial poll() will never return any records" );
2968
- }
2966
+ repauseIfNeeded (partitions );
2969
2967
ListenerConsumer .this .assignedPartitions .addAll (partitions );
2970
2968
if (ListenerConsumer .this .commitCurrentOnAssignment
2971
2969
&& !collectAndCommitIfNecessary (partitions )) {
@@ -2982,6 +2980,27 @@ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
2982
2980
}
2983
2981
}
2984
2982
2983
+ private void repauseIfNeeded (Collection <TopicPartition > partitions ) {
2984
+ if (ListenerConsumer .this .consumerPaused ) {
2985
+ ListenerConsumer .this .consumer .pause (partitions );
2986
+ ListenerConsumer .this .logger .warn ("Paused consumer resumed by Kafka due to rebalance; "
2987
+ + "consumer paused again, so the initial poll() will never return any records" );
2988
+ }
2989
+ Collection <TopicPartition > toRepause = new LinkedList <>();
2990
+ partitions .forEach (tp -> {
2991
+ if (isPartitionPauseRequested (tp )) {
2992
+ toRepause .add (tp );
2993
+ }
2994
+ });
2995
+ if (!ListenerConsumer .this .consumerPaused && toRepause .size () > 0 ) {
2996
+ ListenerConsumer .this .consumer .pause (toRepause );
2997
+ }
2998
+ this .revoked .removeAll (toRepause );
2999
+ this .revoked .forEach (tp -> resumePartition (tp ));
3000
+ ListenerConsumer .this .pausedPartitions .removeAll (this .revoked );
3001
+ this .revoked .clear ();
3002
+ }
3003
+
2985
3004
private boolean collectAndCommitIfNecessary (Collection <TopicPartition > partitions ) {
2986
3005
// Commit initial positions - this is generally redundant but
2987
3006
// it protects us from the case when another consumer starts
0 commit comments