@@ -714,17 +714,17 @@ private final class ListenerConsumer implements SchedulingAwareRunnable, Consume
714
714
715
715
private final @ Nullable Duration syncCommitTimeout ;
716
716
717
- private final @ Nullable RecordInterceptor <K , V > recordInterceptor =
717
+ private final List < RecordInterceptor <K , V >> recordInterceptors =
718
718
!isInterceptBeforeTx () || this .transactionManager == null
719
- ? getRecordInterceptor ()
720
- : null ;
719
+ ? getRecordInterceptors ()
720
+ : new ArrayList <>() ;
721
721
722
- private final @ Nullable RecordInterceptor <K , V > earlyRecordInterceptor =
722
+ private final List < RecordInterceptor <K , V >> earlyRecordInterceptors =
723
723
isInterceptBeforeTx () && this .transactionManager != null
724
- ? getRecordInterceptor ()
725
- : null ;
724
+ ? getRecordInterceptors ()
725
+ : new ArrayList <>() ;
726
726
727
- private final @ Nullable RecordInterceptor <K , V > commonRecordInterceptor = getRecordInterceptor ();
727
+ private final List < RecordInterceptor <K , V >> commonRecordInterceptors = getRecordInterceptors ();
728
728
729
729
private final @ Nullable BatchInterceptor <K , V > batchInterceptor =
730
730
!isInterceptBeforeTx () || this .transactionManager == null
@@ -738,7 +738,7 @@ private final class ListenerConsumer implements SchedulingAwareRunnable, Consume
738
738
739
739
private final @ Nullable BatchInterceptor <K , V > commonBatchInterceptor = getBatchInterceptor ();
740
740
741
- private final @ Nullable ThreadStateProcessor pollThreadStateProcessor ;
741
+ private final List < ThreadStateProcessor > pollThreadStateProcessor ;
742
742
743
743
private final ConsumerSeekCallback seekCallback = new InitialOrIdleSeekCallback ();
744
744
@@ -1040,9 +1040,20 @@ private void obtainClusterId() {
1040
1040
}
1041
1041
}
1042
1042
1043
- @ Nullable
1044
- private ThreadStateProcessor setUpPollProcessor (boolean batch ) {
1045
- return batch ? this .commonBatchInterceptor : this .commonRecordInterceptor ;
1043
+ private List <ThreadStateProcessor > setUpPollProcessor (boolean batch ) {
1044
+ if (batch ) {
1045
+ if (this .commonBatchInterceptor != null ) {
1046
+ List <ThreadStateProcessor > threadStateProcessors = new ArrayList <>();
1047
+ threadStateProcessors .add (this .commonBatchInterceptor );
1048
+ return threadStateProcessors ;
1049
+ }
1050
+ else {
1051
+ return new ArrayList <>();
1052
+ }
1053
+ }
1054
+ else {
1055
+ return new ArrayList <>(this .commonRecordInterceptors );
1056
+ }
1046
1057
}
1047
1058
1048
1059
@ Nullable
@@ -1548,9 +1559,7 @@ private void invokeIfHaveRecords(@Nullable ConsumerRecords<K, V> records) {
1548
1559
}
1549
1560
1550
1561
private void clearThreadState () {
1551
- if (this .pollThreadStateProcessor != null ) {
1552
- this .pollThreadStateProcessor .clearThreadState (this .consumer );
1553
- }
1562
+ this .pollThreadStateProcessor .forEach (threadStateProcessor -> threadStateProcessor .clearThreadState (this .consumer ));
1554
1563
}
1555
1564
1556
1565
private void checkIdlePartitions () {
@@ -1708,9 +1717,7 @@ private ConsumerRecords<K, V> pollConsumer() {
1708
1717
}
1709
1718
1710
1719
private void beforePoll () {
1711
- if (this .pollThreadStateProcessor != null ) {
1712
- this .pollThreadStateProcessor .setupThreadState (this .consumer );
1713
- }
1720
+ this .pollThreadStateProcessor .forEach (threadStateProcessor -> threadStateProcessor .setupThreadState (this .consumer ));
1714
1721
}
1715
1722
1716
1723
private synchronized void captureOffsets (ConsumerRecords <K , V > records ) {
@@ -2548,9 +2555,7 @@ private void invokeRecordListenerInTx(final ConsumerRecords<K, V> records) {
2548
2555
this .logger .error (ex , "Transaction rolled back" );
2549
2556
recordAfterRollback (iterator , cRecord , ex );
2550
2557
}
2551
- if (this .commonRecordInterceptor != null ) {
2552
- this .commonRecordInterceptor .afterRecord (cRecord , this .consumer );
2553
- }
2558
+ this .commonRecordInterceptors .forEach (interceptor -> interceptor .afterRecord (cRecord , this .consumer ));
2554
2559
if (this .nackSleepDurationMillis >= 0 ) {
2555
2560
handleNack (records , cRecord );
2556
2561
break ;
@@ -2627,9 +2632,7 @@ private void doInvokeWithRecords(final ConsumerRecords<K, V> records) {
2627
2632
}
2628
2633
this .logger .trace (() -> "Processing " + KafkaUtils .format (cRecord ));
2629
2634
doInvokeRecordListener (cRecord , iterator );
2630
- if (this .commonRecordInterceptor != null ) {
2631
- this .commonRecordInterceptor .afterRecord (cRecord , this .consumer );
2632
- }
2635
+ this .commonRecordInterceptors .forEach (interceptor -> interceptor .afterRecord (cRecord , this .consumer ));
2633
2636
if (this .nackSleepDurationMillis >= 0 ) {
2634
2637
handleNack (records , cRecord );
2635
2638
break ;
@@ -2680,14 +2683,16 @@ private ConsumerRecords<K, V> checkEarlyIntercept(ConsumerRecords<K, V> nextArg)
2680
2683
private ConsumerRecord <K , V > checkEarlyIntercept (ConsumerRecord <K , V > recordArg ) {
2681
2684
internalHeaders (recordArg );
2682
2685
ConsumerRecord <K , V > cRecord = recordArg ;
2683
- if (this .earlyRecordInterceptor != null ) {
2684
- cRecord = this .earlyRecordInterceptor .intercept (cRecord , this .consumer );
2686
+
2687
+ for (RecordInterceptor <K , V > earlyRecordInterceptor : this .earlyRecordInterceptors ) {
2688
+ cRecord = earlyRecordInterceptor .intercept (cRecord , this .consumer );
2685
2689
if (cRecord == null ) {
2686
2690
this .logger .debug (() -> "RecordInterceptor returned null, skipping: "
2687
- + KafkaUtils .format (recordArg ));
2691
+ + KafkaUtils .format (recordArg ));
2688
2692
ackCurrent (recordArg );
2689
- this .earlyRecordInterceptor .success (recordArg , this .consumer );
2690
- this .earlyRecordInterceptor .afterRecord (recordArg , this .consumer );
2693
+ earlyRecordInterceptor .success (recordArg , this .consumer );
2694
+ earlyRecordInterceptor .afterRecord (recordArg , this .consumer );
2695
+ break ;
2691
2696
}
2692
2697
}
2693
2698
return cRecord ;
@@ -2848,13 +2853,13 @@ private void commitOffsetsIfNeededAfterHandlingError(final ConsumerRecord<K, V>
2848
2853
}
2849
2854
2850
2855
private void recordInterceptAfter (ConsumerRecord <K , V > records , @ Nullable Exception exception ) {
2851
- if (this .commonRecordInterceptor != null ) {
2856
+ if (! this .commonRecordInterceptors . isEmpty () ) {
2852
2857
try {
2853
2858
if (exception == null ) {
2854
- this .commonRecordInterceptor . success (records , this .consumer );
2859
+ this .commonRecordInterceptors . forEach ( interceptor -> interceptor . success (records , this .consumer ) );
2855
2860
}
2856
2861
else {
2857
- this .commonRecordInterceptor . failure (records , exception , this .consumer );
2862
+ this .commonRecordInterceptors . forEach ( interceptor -> interceptor . failure (records , exception , this .consumer ) );
2858
2863
}
2859
2864
}
2860
2865
catch (Exception e ) {
@@ -2888,8 +2893,11 @@ private void invokeOnMessage(final ConsumerRecord<K, V> cRecord) {
2888
2893
2889
2894
private void doInvokeOnMessage (final ConsumerRecord <K , V > recordArg ) {
2890
2895
ConsumerRecord <K , V > cRecord = recordArg ;
2891
- if (this .recordInterceptor != null ) {
2892
- cRecord = this .recordInterceptor .intercept (cRecord , this .consumer );
2896
+ for (RecordInterceptor <K , V > recordInterceptor : this .recordInterceptors ) {
2897
+ cRecord = recordInterceptor .intercept (cRecord , this .consumer );
2898
+ if (cRecord == null ) {
2899
+ break ;
2900
+ }
2893
2901
}
2894
2902
if (cRecord == null ) {
2895
2903
this .logger .debug (() -> "RecordInterceptor returned null, skipping: "
0 commit comments