@@ -403,6 +403,8 @@ private final class ListenerConsumer implements SchedulingAwareRunnable, Consume
403
403
404
404
private final GenericMessageListener <?> genericListener ;
405
405
406
+ private final ConsumerSeekAware consumerSeekAwareListener ;
407
+
406
408
private final MessageListener <K , V > listener ;
407
409
408
410
private final BatchMessageListener <K , V > batchListener ;
@@ -506,6 +508,7 @@ private final class ListenerConsumer implements SchedulingAwareRunnable, Consume
506
508
507
509
this .transactionTemplate = determineTransactionTemplate ();
508
510
this .genericListener = listener ;
511
+ this .consumerSeekAwareListener = checkConsumerSeekAware (listener );
509
512
subscribeOrAssignTopics (this .consumer );
510
513
GenericErrorHandler <?> errHandler = KafkaMessageListenerContainer .this .getGenericErrorHandler ();
511
514
if (listener instanceof BatchMessageListener ) {
@@ -565,6 +568,11 @@ else if (listener instanceof MessageListener) {
565
568
}
566
569
}
567
570
571
+ @ Nullable
572
+ private ConsumerSeekAware checkConsumerSeekAware (GenericMessageListener <?> candidate ) {
573
+ return candidate instanceof ConsumerSeekAware ? (ConsumerSeekAware ) candidate : null ;
574
+ }
575
+
568
576
boolean isConsumerPaused () {
569
577
return this .consumerPaused ;
570
578
}
@@ -694,17 +702,16 @@ protected ErrorHandler determineErrorHandler(GenericErrorHandler<?> errHandler)
694
702
}
695
703
696
704
private void seekPartitions (Collection <TopicPartition > partitions , boolean idle ) {
697
- (( ConsumerSeekAware ) this .genericListener ) .registerSeekCallback (this );
705
+ this .consumerSeekAwareListener .registerSeekCallback (this );
698
706
Map <TopicPartition , Long > current = new HashMap <>();
699
707
for (TopicPartition topicPartition : partitions ) {
700
708
current .put (topicPartition , ListenerConsumer .this .consumer .position (topicPartition ));
701
709
}
702
710
if (idle ) {
703
- (( ConsumerSeekAware ) ListenerConsumer . this .genericListener ) .onIdleContainer (current , this .seekCallback );
711
+ this .consumerSeekAwareListener .onIdleContainer (current , this .seekCallback );
704
712
}
705
713
else {
706
- ((ConsumerSeekAware ) ListenerConsumer .this .genericListener ).onPartitionsAssigned (current ,
707
- this .seekCallback );
714
+ this .consumerSeekAwareListener .onPartitionsAssigned (current , this .seekCallback );
708
715
}
709
716
}
710
717
@@ -729,8 +736,8 @@ public boolean isLongLived() {
729
736
@ Override
730
737
public void run () {
731
738
this .consumerThread = Thread .currentThread ();
732
- if (this .genericListener instanceof ConsumerSeekAware ) {
733
- (( ConsumerSeekAware ) this .genericListener ) .registerSeekCallback (this );
739
+ if (this .consumerSeekAwareListener != null ) {
740
+ this .consumerSeekAwareListener .registerSeekCallback (this );
734
741
}
735
742
KafkaUtils .setConsumerGroupId (this .consumerGroupId );
736
743
this .count = 0 ;
@@ -838,7 +845,7 @@ private void checkIdle() {
838
845
publishIdleContainerEvent (now - this .lastReceive , this .isConsumerAwareListener
839
846
? this .consumer : null , this .consumerPaused );
840
847
this .lastAlertAt = now ;
841
- if (this .genericListener instanceof ConsumerSeekAware ) {
848
+ if (this .consumerSeekAwareListener != null ) {
842
849
Collection <TopicPartition > partitions = getAssignedPartitions ();
843
850
if (partitions != null ) {
844
851
seekPartitions (partitions , true );
@@ -879,8 +886,8 @@ private void wrapUp() {
879
886
if (this .errorHandler != null ) {
880
887
this .errorHandler .clearThreadState ();
881
888
}
882
- if (ListenerConsumer . this .genericListener instanceof ConsumerSeekAware ) {
883
- (( ConsumerSeekAware ) ListenerConsumer . this .genericListener ) .onPartitionsRevoked (partitions );
889
+ if (this .consumerSeekAwareListener != null ) {
890
+ this .consumerSeekAwareListener .onPartitionsRevoked (partitions );
884
891
}
885
892
this .logger .info (() -> getGroupId () + ": Consumer stopped" );
886
893
publishConsumerStoppedEvent ();
@@ -1797,8 +1804,8 @@ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
1797
1804
this .consumerAwareListener .onPartitionsRevokedAfterCommit (ListenerConsumer .this .consumer ,
1798
1805
partitions );
1799
1806
}
1800
- if (ListenerConsumer .this .genericListener instanceof ConsumerSeekAware ) {
1801
- (( ConsumerSeekAware ) ListenerConsumer .this .genericListener ) .onPartitionsRevoked (partitions );
1807
+ if (ListenerConsumer .this .consumerSeekAwareListener != null ) {
1808
+ ListenerConsumer .this .consumerSeekAwareListener .onPartitionsRevoked (partitions );
1802
1809
}
1803
1810
}
1804
1811
finally {
0 commit comments