16
16
17
17
package org .springframework .kafka .listener ;
18
18
19
- import java .time .Duration ;
20
19
import java .util .ArrayList ;
21
20
import java .util .Collections ;
22
21
import java .util .HashMap ;
23
22
import java .util .HashSet ;
24
- import java .util .Iterator ;
25
23
import java .util .List ;
26
24
import java .util .Map ;
27
25
import java .util .Set ;
@@ -120,7 +118,7 @@ public void setReclassifyOnExceptionChange(boolean reclassifyOnExceptionChange)
120
118
@ Override
121
119
protected void notRetryable (Stream <Class <? extends Exception >> notRetryable ) {
122
120
if (this .fallbackBatchHandler instanceof ExceptionClassifier handler ) {
123
- notRetryable .forEach (ex -> handler . addNotRetryableExceptions ( ex ) );
121
+ notRetryable .forEach (handler :: addNotRetryableExceptions );
124
122
}
125
123
}
126
124
@@ -178,7 +176,6 @@ protected <K, V> ConsumerRecords<K, V> handle(Exception thrownException, Consume
178
176
else {
179
177
return String .format ("Record not found in batch, index %d out of bounds (0, %d); "
180
178
+ "re-seeking batch" , index , data .count () - 1 );
181
-
182
179
}
183
180
});
184
181
fallback (thrownException , data , consumer , container , invokeListener );
@@ -201,11 +198,9 @@ private int findIndex(ConsumerRecords<?, ?> data, ConsumerRecord<?, ?> record) {
201
198
return -1 ;
202
199
}
203
200
int i = 0 ;
204
- Iterator <?> iterator = data .iterator ();
205
- while (iterator .hasNext ()) {
206
- ConsumerRecord <?, ?> candidate = (ConsumerRecord <?, ?>) iterator .next ();
207
- if (candidate .topic ().equals (record .topic ()) && candidate .partition () == record .partition ()
208
- && candidate .offset () == record .offset ()) {
201
+ for (ConsumerRecord <?, ?> datum : data ) {
202
+ if (datum .topic ().equals (record .topic ()) && datum .partition () == record .partition ()
203
+ && datum .offset () == record .offset ()) {
209
204
break ;
210
205
}
211
206
i ++;
@@ -220,29 +215,25 @@ private <K, V> ConsumerRecords<K, V> seekOrRecover(Exception thrownException, @N
220
215
if (data == null ) {
221
216
return ConsumerRecords .empty ();
222
217
}
223
- Iterator <?> iterator = data .iterator ();
224
- List <ConsumerRecord <?, ?>> toCommit = new ArrayList <>();
225
218
List <ConsumerRecord <?, ?>> remaining = new ArrayList <>();
226
219
int index = indexArg ;
227
- while ( iterator . hasNext ()) {
228
- ConsumerRecord <?, ?> record = ( ConsumerRecord <?, ?>) iterator . next ();
220
+ Map < TopicPartition , OffsetAndMetadata > offsets = new HashMap <>();
221
+ for ( ConsumerRecord <?, ?> datum : data ) {
229
222
if (index -- > 0 ) {
230
- toCommit .add (record );
223
+ offsets .compute (new TopicPartition (datum .topic (), datum .partition ()),
224
+ (key , val ) -> ListenerUtils .createOffsetAndMetadata (container , datum .offset () + 1 ));
231
225
}
232
226
else {
233
- remaining .add (record );
227
+ remaining .add (datum );
234
228
}
235
229
}
236
- Map <TopicPartition , OffsetAndMetadata > offsets = new HashMap <>();
237
- toCommit .forEach (rec -> offsets .compute (new TopicPartition (rec .topic (), rec .partition ()),
238
- (key , val ) -> ListenerUtils .createOffsetAndMetadata (container , rec .offset () + 1 )));
239
230
if (offsets .size () > 0 ) {
240
231
commit (consumer , container , offsets );
241
232
}
242
233
if (isSeekAfterError ()) {
243
234
if (remaining .size () > 0 ) {
244
235
SeekUtils .seekOrRecover (thrownException , remaining , consumer , container , false ,
245
- getFailureTracker ():: recovered , this .logger , getLogLevel ());
236
+ getFailureTracker (), this .logger , getLogLevel ());
246
237
ConsumerRecord <?, ?> recovered = remaining .get (0 );
247
238
commit (consumer , container ,
248
239
Collections .singletonMap (new TopicPartition (recovered .topic (), recovered .partition ()),
@@ -254,35 +245,33 @@ private <K, V> ConsumerRecords<K, V> seekOrRecover(Exception thrownException, @N
254
245
return ConsumerRecords .empty ();
255
246
}
256
247
else {
257
- if (indexArg == 0 ) {
258
- return (ConsumerRecords <K , V >) data ; // first record just rerun the whole thing
259
- }
260
- else {
261
- try {
262
- if (getFailureTracker ().recovered (remaining .get (0 ), thrownException , container ,
263
- consumer )) {
264
- remaining .remove (0 );
265
- }
266
- }
267
- catch (Exception e ) {
248
+ try {
249
+ if (getFailureTracker ().recovered (remaining .get (0 ), thrownException , container ,
250
+ consumer )) {
251
+ remaining .remove (0 );
268
252
}
269
- Map <TopicPartition , List <ConsumerRecord <K , V >>> remains = new HashMap <>();
270
- remaining .forEach (rec -> remains .computeIfAbsent (new TopicPartition (rec .topic (), rec .partition ()),
271
- tp -> new ArrayList <ConsumerRecord <K , V >>()).add ((ConsumerRecord <K , V >) rec ));
272
- return new ConsumerRecords <>(remains );
273
253
}
254
+ catch (Exception e ) {
255
+ }
256
+ if (remaining .isEmpty ()) {
257
+ return ConsumerRecords .empty ();
258
+ }
259
+ Map <TopicPartition , List <ConsumerRecord <K , V >>> remains = new HashMap <>();
260
+ remaining .forEach (rec -> remains .computeIfAbsent (new TopicPartition (rec .topic (), rec .partition ()),
261
+ tp -> new ArrayList <>()).add ((ConsumerRecord <K , V >) rec ));
262
+ return new ConsumerRecords <>(remains );
274
263
}
275
264
}
276
265
277
- private void commit (Consumer <?, ?> consumer , MessageListenerContainer container , Map <TopicPartition , OffsetAndMetadata > offsets ) {
266
+ private void commit (Consumer <?, ?> consumer , MessageListenerContainer container ,
267
+ Map <TopicPartition , OffsetAndMetadata > offsets ) {
278
268
279
- boolean syncCommits = container .getContainerProperties ().isSyncCommits ();
280
- Duration timeout = container .getContainerProperties ().getSyncCommitTimeout ();
281
- if (syncCommits ) {
282
- consumer .commitSync (offsets , timeout );
269
+ ContainerProperties properties = container .getContainerProperties ();
270
+ if (properties .isSyncCommits ()) {
271
+ consumer .commitSync (offsets , properties .getSyncCommitTimeout ());
283
272
}
284
273
else {
285
- OffsetCommitCallback commitCallback = container . getContainerProperties () .getCommitCallback ();
274
+ OffsetCommitCallback commitCallback = properties .getCommitCallback ();
286
275
if (commitCallback == null ) {
287
276
commitCallback = LOGGING_COMMIT_CALLBACK ;
288
277
}
@@ -304,8 +293,8 @@ private BatchListenerFailedException getBatchListenerFailedException(Throwable t
304
293
throwable = throwable .getCause ();
305
294
checked .add (throwable );
306
295
307
- if (throwable instanceof BatchListenerFailedException ) {
308
- target = ( BatchListenerFailedException ) throwable ;
296
+ if (throwable instanceof BatchListenerFailedException batchListenerFailedException ) {
297
+ target = batchListenerFailedException ;
309
298
break ;
310
299
}
311
300
}
0 commit comments