@@ -2581,35 +2581,28 @@ private void doInvokeBatchOnMessage(final ConsumerRecords<K, V> records,
25812581 private void invokeBatchErrorHandler (final ConsumerRecords <K , V > records ,
25822582 List <ConsumerRecord <K , V >> list , RuntimeException rte ) {
25832583
2584- if (Objects .requireNonNull (this .commonErrorHandler ).seeksAfterHandling () || this .transactionManager != null
2585- || rte instanceof CommitFailedException ) {
2586-
2587- try {
2584+ try {
2585+ if (Objects .requireNonNull (this .commonErrorHandler ).seeksAfterHandling ()
2586+ || this .transactionManager != null || rte instanceof CommitFailedException ) {
25882587 this .commonErrorHandler .handleBatch (rte , records , this .consumer ,
25892588 KafkaMessageListenerContainer .this .thisOrParentContainer ,
25902589 () -> invokeBatchOnMessageWithRecordsOrList (records , list ));
25912590 }
2592- catch (RecordInRetryException e ) {
2593- removeOffsetsInBatch (list );
2594- throw e ;
2595- }
2596- }
25972591 else {
2598- try {
2599- ConsumerRecords <K , V > afterHandling = this .commonErrorHandler .handleBatchAndReturnRemaining (rte ,
2600- records , this .consumer , KafkaMessageListenerContainer .this .thisOrParentContainer ,
2601- () -> invokeBatchOnMessageWithRecordsOrList (records , list ));
2602- if (afterHandling != null && !afterHandling .isEmpty ()) {
2603- this .remainingRecords = afterHandling ;
2604- this .pauseForPending = true ;
2605- }
2606- }
2607- catch (RecordInRetryException e ) {
2608- removeOffsetsInBatch (list );
2609- throw e ;
2592+ ConsumerRecords <K , V > afterHandling = this .commonErrorHandler .handleBatchAndReturnRemaining (rte ,
2593+ records , this .consumer , KafkaMessageListenerContainer .this .thisOrParentContainer ,
2594+ () -> invokeBatchOnMessageWithRecordsOrList (records , list ));
2595+ if (afterHandling != null && !afterHandling .isEmpty ()) {
2596+ this .remainingRecords = afterHandling ;
2597+ this .pauseForPending = true ;
26102598 }
26112599 }
26122600 }
2601+ catch (RecordInRetryException e ) {
2602+ removeOffsetsInBatch (list );
2603+ throw e ;
2604+ }
2605+ }
26132606
26142607 private void invokeRecordListener (final ConsumerRecords <K , V > records ) {
26152608 if (this .transactionTemplate != null ) {
@@ -3021,8 +3014,7 @@ private void invokeErrorHandlerBySingleRecord(FailedRecordTuple<K, V> failedReco
30213014 catch (Exception ex ) { // NO SONAR
30223015 this .logger .error (ex , "Failed to commit before handling error" );
30233016 }
3024- List <ConsumerRecord <?, ?>> retryRecords = new ArrayList <>();
3025- retryRecords .add (cRecord );
3017+ List <ConsumerRecord <?, ?>> retryRecords = List .of (cRecord );
30263018 try {
30273019 this .commonErrorHandler .handleRemaining (rte , retryRecords , this .consumer ,
30283020 KafkaMessageListenerContainer .this .thisOrParentContainer );
@@ -3056,54 +3048,54 @@ private void invokeErrorHandlerBySingleRecord(FailedRecordTuple<K, V> failedReco
30563048 private void invokeErrorHandler (final ConsumerRecord <K , V > cRecord ,
30573049 Iterator <ConsumerRecord <K , V >> iterator , RuntimeException rte ) {
30583050
3059- if (Objects .requireNonNull (this .commonErrorHandler ).seeksAfterHandling ()
3060- || rte instanceof CommitFailedException ) {
3061-
3062- try {
3063- if (this .producer == null ) {
3064- processCommits ();
3051+ List <ConsumerRecord <?, ?>> retryRecords = List .of (cRecord );
3052+ try {
3053+ if (Objects .requireNonNull (this .commonErrorHandler ).seeksAfterHandling ()
3054+ || rte instanceof CommitFailedException ) {
3055+ try {
3056+ if (this .producer == null ) {
3057+ processCommits ();
3058+ }
30653059 }
3066- }
3067- catch (Exception ex ) { // NO SONAR
3068- this .logger .error (ex , "Failed to commit before handling error" );
3069- }
3070- List <ConsumerRecord <?, ?>> retryRecords = new ArrayList <>();
3060+ catch (Exception ex ) { // NO SONAR
3061+ this .logger .error (ex , "Failed to commit before handling error" );
3062+ }
3063+ retryRecords = new ArrayList <>();
30713064 retryRecords .add (cRecord );
30723065 while (iterator .hasNext ()) {
30733066 retryRecords .add (iterator .next ());
30743067 }
3068+ this .commonErrorHandler .handleRemaining (rte , retryRecords , this .consumer ,
3069+ KafkaMessageListenerContainer .this .thisOrParentContainer );
3070+ }
3071+ else {
3072+ boolean handled = false ;
30753073 try {
3076- this .commonErrorHandler .handleRemaining (rte , retryRecords , this .consumer ,
3074+ handled = this .commonErrorHandler .handleOne (rte , cRecord , this .consumer ,
30773075 KafkaMessageListenerContainer .this .thisOrParentContainer );
30783076 }
3079- catch (RecordInRetryException e ) {
3080- removeOffsetsInBatch (retryRecords );
3081- throw e ;
3077+ catch (Exception ex ) {
3078+ this .logger .error (ex , "ErrorHandler threw unexpected exception" );
30823079 }
3083- }
3084- else {
3085- boolean handled = false ;
3086- try {
3087- handled = this .commonErrorHandler .handleOne (rte , cRecord , this .consumer ,
3088- KafkaMessageListenerContainer .this .thisOrParentContainer );
3089- }
3090- catch (Exception ex ) {
3091- this .logger .error (ex , "ErrorHandler threw unexpected exception" );
3092- }
3093- Map <TopicPartition , List <ConsumerRecord <K , V >>> records = new LinkedHashMap <>();
3094- if (!handled ) {
3095- records .computeIfAbsent (new TopicPartition (cRecord .topic (), cRecord .partition ()),
3096- tp -> new ArrayList <>()).add (cRecord );
3097- while (iterator .hasNext ()) {
3098- ConsumerRecord <K , V > next = iterator .next ();
3099- records .computeIfAbsent (new TopicPartition (next .topic (), next .partition ()),
3100- tp -> new ArrayList <>()).add (next );
3080+ Map <TopicPartition , List <ConsumerRecord <K , V >>> records = new LinkedHashMap <>();
3081+ if (!handled ) {
3082+ records .computeIfAbsent (new TopicPartition (cRecord .topic (), cRecord .partition ()),
3083+ tp -> new ArrayList <>()).add (cRecord );
3084+ while (iterator .hasNext ()) {
3085+ ConsumerRecord <K , V > next = iterator .next ();
3086+ records .computeIfAbsent (new TopicPartition (next .topic (), next .partition ()),
3087+ tp -> new ArrayList <>()).add (next );
3088+ }
3089+ }
3090+ if (!records .isEmpty ()) {
3091+ this .remainingRecords = new ConsumerRecords <>(records , Map .of ());
3092+ this .pauseForPending = true ;
31013093 }
31023094 }
3103- if (! records . isEmpty ()) {
3104- this . remainingRecords = new ConsumerRecords <>( records , Map . of ());
3105- this . pauseForPending = true ;
3106- }
3095+ }
3096+ catch ( RecordInRetryException e ) {
3097+ removeOffsetsInBatch ( retryRecords ) ;
3098+ throw e ;
31073099 }
31083100 }
31093101
0 commit comments