-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
FaultTolerantStepBuilder.java
780 lines (667 loc) · 27.9 KB
/
FaultTolerantStepBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/*
* Copyright 2006-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.step.builder;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.batch.operations.BatchRuntimeException;
import org.springframework.batch.core.ChunkListener;
import org.springframework.batch.core.JobInterruptedException;
import org.springframework.batch.core.SkipListener;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.StepListener;
import org.springframework.batch.core.annotation.OnSkipInProcess;
import org.springframework.batch.core.annotation.OnSkipInRead;
import org.springframework.batch.core.annotation.OnSkipInWrite;
import org.springframework.batch.core.listener.StepListenerFactoryBean;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.FatalStepExecutionException;
import org.springframework.batch.core.step.item.BatchRetryTemplate;
import org.springframework.batch.core.step.item.ChunkMonitor;
import org.springframework.batch.core.step.item.ChunkOrientedTasklet;
import org.springframework.batch.core.step.item.ChunkProcessor;
import org.springframework.batch.core.step.item.ChunkProvider;
import org.springframework.batch.core.step.item.FaultTolerantChunkProcessor;
import org.springframework.batch.core.step.item.FaultTolerantChunkProvider;
import org.springframework.batch.core.step.item.ForceRollbackForWriteSkipException;
import org.springframework.batch.core.step.item.KeyGenerator;
import org.springframework.batch.core.step.item.SimpleRetryExceptionHandler;
import org.springframework.batch.core.step.skip.CompositeSkipPolicy;
import org.springframework.batch.core.step.skip.ExceptionClassifierSkipPolicy;
import org.springframework.batch.core.step.skip.LimitCheckingItemSkipPolicy;
import org.springframework.batch.core.step.skip.NeverSkipItemSkipPolicy;
import org.springframework.batch.core.step.skip.NonSkippableReadException;
import org.springframework.batch.core.step.skip.SkipLimitExceededException;
import org.springframework.batch.core.step.skip.SkipListenerFailedException;
import org.springframework.batch.core.step.skip.SkipPolicy;
import org.springframework.batch.core.step.skip.SkipPolicyFailedException;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.repeat.RepeatOperations;
import org.springframework.batch.repeat.support.RepeatTemplate;
import org.springframework.batch.support.ReflectionUtils;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.classify.BinaryExceptionClassifier;
import org.springframework.classify.Classifier;
import org.springframework.classify.SubclassClassifier;
import org.springframework.retry.ExhaustedRetryException;
import org.springframework.retry.RetryException;
import org.springframework.retry.RetryListener;
import org.springframework.retry.RetryPolicy;
import org.springframework.retry.backoff.BackOffPolicy;
import org.springframework.retry.policy.CompositeRetryPolicy;
import org.springframework.retry.policy.ExceptionClassifierRetryPolicy;
import org.springframework.retry.policy.NeverRetryPolicy;
import org.springframework.retry.policy.RetryContextCache;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.util.Assert;
/**
* A step builder for fully fault tolerant chunk-oriented item processing steps. Extends {@link SimpleStepBuilder} with
* additional properties for retry and skip of failed items.
*
* @author Dave Syer
* @author Chris Schaefer
* @author Michael Minella
* @author Mahmoud Ben Hassine
*
* @since 2.2
*/
public class FaultTolerantStepBuilder<I, O> extends SimpleStepBuilder<I, O> {
private ChunkMonitor chunkMonitor = new ChunkMonitor();
private boolean streamIsReader;
private int retryLimit = 0;
private BackOffPolicy backOffPolicy;
private Set<RetryListener> retryListeners = new LinkedHashSet<>();
private RetryPolicy retryPolicy;
private RetryContextCache retryContextCache;
private KeyGenerator keyGenerator;
private Collection<Class<? extends Throwable>> noRollbackExceptionClasses = new LinkedHashSet<>();
private Map<Class<? extends Throwable>, Boolean> skippableExceptionClasses = new HashMap<>();
private Collection<Class<? extends Throwable>> nonSkippableExceptionClasses = new HashSet<>();
private Map<Class<? extends Throwable>, Boolean> retryableExceptionClasses = new HashMap<>();
private Collection<Class<? extends Throwable>> nonRetryableExceptionClasses = new HashSet<>();
private Set<SkipListener<? super I, ? super O>> skipListeners = new LinkedHashSet<>();
private Set<org.springframework.batch.core.jsr.RetryListener> jsrRetryListeners = new LinkedHashSet<>();
private int skipLimit = 0;
private SkipPolicy skipPolicy;
private boolean processorTransactional = true;
/**
* Create a new builder initialized with any properties in the parent. The parent is copied, so it can be re-used.
*
* @param parent a parent helper containing common step properties
*/
public FaultTolerantStepBuilder(StepBuilderHelper<?> parent) {
super(parent);
}
/**
* Create a new builder initialized with any properties in the parent. The parent is copied, so it can be re-used.
*
* @param parent a parent helper containing common step properties
*/
protected FaultTolerantStepBuilder(SimpleStepBuilder<I, O> parent) {
super(parent);
}
@Override
public TaskletStep build() {
registerStepListenerAsSkipListener();
return super.build();
}
@SuppressWarnings("unchecked")
protected void registerStepListenerAsSkipListener() {
for (StepExecutionListener stepExecutionListener: properties.getStepExecutionListeners()){
if (stepExecutionListener instanceof SkipListener){
listener((SkipListener<I,O>)stepExecutionListener);
}
}
for (ChunkListener chunkListener: this.chunkListeners){
if (chunkListener instanceof SkipListener){
listener((SkipListener<I,O>)chunkListener);
}
}
}
/**
* Create a new chunk oriented tasklet with reader, writer and processor as provided.
*
* @see org.springframework.batch.core.step.builder.SimpleStepBuilder#createTasklet()
*/
@Override
protected Tasklet createTasklet() {
Assert.state(getReader() != null, "ItemReader must be provided");
Assert.state(getWriter() != null, "ItemWriter must be provided");
addSpecialExceptions();
registerSkipListeners();
ChunkProvider<I> chunkProvider = createChunkProvider();
ChunkProcessor<I> chunkProcessor = createChunkProcessor();
ChunkOrientedTasklet<I> tasklet = new ChunkOrientedTasklet<>(chunkProvider, chunkProcessor);
tasklet.setBuffering(!isReaderTransactionalQueue());
return tasklet;
}
/**
* Registers objects using the annotation based listener configuration.
*
* @param listener the object that has a method configured with listener annotation
* @return this for fluent chaining
*/
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public SimpleStepBuilder<I, O> listener(Object listener) {
super.listener(listener);
Set<Method> skipListenerMethods = new HashSet<>();
skipListenerMethods.addAll(ReflectionUtils.findMethod(listener.getClass(), OnSkipInRead.class));
skipListenerMethods.addAll(ReflectionUtils.findMethod(listener.getClass(), OnSkipInProcess.class));
skipListenerMethods.addAll(ReflectionUtils.findMethod(listener.getClass(), OnSkipInWrite.class));
if(skipListenerMethods.size() > 0) {
StepListenerFactoryBean factory = new StepListenerFactoryBean();
factory.setDelegate(listener);
skipListeners.add((SkipListener) factory.getObject());
}
@SuppressWarnings("unchecked")
SimpleStepBuilder<I, O> result = this;
return result;
}
/**
* Register a skip listener.
*
* @param listener the listener to register
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> listener(SkipListener<? super I, ? super O> listener) {
skipListeners.add(listener);
return this;
}
public FaultTolerantStepBuilder<I, O> listener(org.springframework.batch.core.jsr.RetryListener listener) {
jsrRetryListeners.add(listener);
return this;
}
@Override
public FaultTolerantStepBuilder<I, O> listener(ChunkListener listener) {
super.listener(new TerminateOnExceptionChunkListenerDelegate(listener));
return this;
}
@Override
public AbstractTaskletStepBuilder<SimpleStepBuilder<I, O>> transactionAttribute(
TransactionAttribute transactionAttribute) {
return super.transactionAttribute(getTransactionAttribute(transactionAttribute));
}
/**
* Register a retry listener.
*
* @param listener the listener to register
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> listener(RetryListener listener) {
retryListeners.add(listener);
return this;
}
/**
* Sets the key generator for identifying retried items. Retry across transaction boundaries requires items to be
* identified when they are encountered again. The default strategy is to use the items themselves, relying on their
* own implementation to ensure that they can be identified. Often a key generator is not necessary as long as the
* items have reliable hash code and equals implementations, or the reader is not transactional (the default) and
* the item processor either is itself not transactional (not the default) or does not create new items.
*
* @param keyGenerator a key generator for the stateful retry
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> keyGenerator(KeyGenerator keyGenerator) {
this.keyGenerator = keyGenerator;
return this;
}
/**
* The maximum number of times to try a failed item. Zero and one both translate to try only once and do not retry.
* Ignored if an explicit {@link #retryPolicy} is set.
*
* @param retryLimit the retry limit (default 0)
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> retryLimit(int retryLimit) {
this.retryLimit = retryLimit;
return this;
}
/**
* Provide an explicit retry policy instead of using the {@link #retryLimit(int)} and retryable exceptions provided
* elsewhere. Can be used to retry different exceptions a different number of times, for instance.
*
* @param retryPolicy a retry policy
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> retryPolicy(RetryPolicy retryPolicy) {
this.retryPolicy = retryPolicy;
return this;
}
/**
* Provide a backoff policy to prevent items being retried immediately (e.g. in case the failure was caused by a
* remote resource failure that might take some time to be resolved). Ignored if an explicit {@link #retryPolicy} is
* set.
*
* @param backOffPolicy the back off policy to use (default no backoff)
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> backOffPolicy(BackOffPolicy backOffPolicy) {
this.backOffPolicy = backOffPolicy;
return this;
}
/**
* Provide an explicit retry context cache. Retry is stateful across transactions in the case of failures in item
* processing or writing, so some information about the context for subsequent retries has to be stored.
*
* @param retryContextCache cache for retry contexts in between transactions (default to standard in-memory
* implementation)
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> retryContextCache(RetryContextCache retryContextCache) {
this.retryContextCache = retryContextCache;
return this;
}
/**
* Sets the maximum number of failed items to skip before the step fails. Ignored if an explicit
* {@link #skipPolicy(SkipPolicy)} is provided.
*
* @param skipLimit the skip limit to set
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> skipLimit(int skipLimit) {
this.skipLimit = skipLimit;
return this;
}
/**
* Explicitly prevent certain exceptions (and subclasses) from being skipped.
*
* @param type the non-skippable exception
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> noSkip(Class<? extends Throwable> type) {
skippableExceptionClasses.put(type, false);
return this;
}
/**
* Explicitly request certain exceptions (and subclasses) to be skipped. These
* exceptions (and their subclasses) might be thrown during any phase of the chunk
* processing (read, process, write) but separate counts are made of skips on
* read, process and write inside the step execution.
*
* @param type the exception type.
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> skip(Class<? extends Throwable> type) {
skippableExceptionClasses.put(type, true);
return this;
}
/**
* Provide an explicit policy for managing skips. A skip policy determines which exceptions are skippable and how
* many times.
*
* @param skipPolicy the skip policy
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> skipPolicy(SkipPolicy skipPolicy) {
this.skipPolicy = skipPolicy;
return this;
}
/**
* Mark this exception as ignorable during item read or processing operations. Processing continues with no
* additional callbacks (use skips instead if you need to be notified). Ignored during write because there is no
* guarantee of skip and retry without rollback.
*
* @param type the exception to mark as no rollback
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> noRollback(Class<? extends Throwable> type) {
noRollbackExceptionClasses.add(type);
return this;
}
/**
* Explicitly ask for an exception (and subclasses) to be excluded from retry.
*
* @param type the exception to exclude from retry
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> noRetry(Class<? extends Throwable> type) {
retryableExceptionClasses.put(type, false);
return this;
}
/**
* Explicitly ask for an exception (and subclasses) to be retried.
*
* @param type the exception to retry
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> retry(Class<? extends Throwable> type) {
retryableExceptionClasses.put(type, true);
return this;
}
/**
* Mark the item processor as non-transactional (default is the opposite). If this flag is set the results of item
* processing are cached across transactions in between retries and during skip processing, otherwise the processor
* will be called in every transaction.
*
* @return this for fluent chaining
*/
public FaultTolerantStepBuilder<I, O> processorNonTransactional() {
this.processorTransactional = false;
return this;
}
@Override
public AbstractTaskletStepBuilder<SimpleStepBuilder<I, O>> stream(ItemStream stream) {
if (stream instanceof ItemReader<?>) {
if (!streamIsReader) {
streamIsReader = true;
super.stream(chunkMonitor);
}
// In cases where multiple nested item readers are registered,
// they all want to get the open() and close() callbacks.
chunkMonitor.registerItemStream(stream);
}
else {
super.stream(stream);
}
return this;
}
protected ChunkProvider<I> createChunkProvider() {
SkipPolicy readSkipPolicy = createSkipPolicy();
readSkipPolicy = getFatalExceptionAwareProxy(readSkipPolicy);
FaultTolerantChunkProvider<I> chunkProvider = new FaultTolerantChunkProvider<>(getReader(),
createChunkOperations());
chunkProvider.setMaxSkipsOnRead(Math.max(getChunkSize(), FaultTolerantChunkProvider.DEFAULT_MAX_SKIPS_ON_READ));
chunkProvider.setSkipPolicy(readSkipPolicy);
chunkProvider.setRollbackClassifier(getRollbackClassifier());
ArrayList<StepListener> listeners = new ArrayList<>(getItemListeners());
listeners.addAll(skipListeners);
chunkProvider.setListeners(listeners);
return chunkProvider;
}
protected ChunkProcessor<I> createChunkProcessor() {
BatchRetryTemplate batchRetryTemplate = createRetryOperations();
FaultTolerantChunkProcessor<I, O> chunkProcessor = new FaultTolerantChunkProcessor<>(getProcessor(),
getWriter(), batchRetryTemplate);
chunkProcessor.setBuffering(!isReaderTransactionalQueue());
chunkProcessor.setProcessorTransactional(processorTransactional);
SkipPolicy writeSkipPolicy = createSkipPolicy();
writeSkipPolicy = getFatalExceptionAwareProxy(writeSkipPolicy);
chunkProcessor.setWriteSkipPolicy(writeSkipPolicy);
chunkProcessor.setProcessSkipPolicy(writeSkipPolicy);
chunkProcessor.setRollbackClassifier(getRollbackClassifier());
chunkProcessor.setKeyGenerator(keyGenerator);
detectStreamInReader();
ArrayList<StepListener> listeners = new ArrayList<>(getItemListeners());
listeners.addAll(skipListeners);
chunkProcessor.setListeners(listeners);
chunkProcessor.setChunkMonitor(chunkMonitor);
return chunkProcessor;
}
@SuppressWarnings("unchecked")
private void addSpecialExceptions() {
addNonSkippableExceptionIfMissing(SkipLimitExceededException.class, NonSkippableReadException.class,
SkipListenerFailedException.class, SkipPolicyFailedException.class, RetryException.class,
JobInterruptedException.class, Error.class, BeanCreationException.class);
addNonRetryableExceptionIfMissing(SkipLimitExceededException.class, NonSkippableReadException.class,
TransactionException.class, FatalStepExecutionException.class, SkipListenerFailedException.class,
SkipPolicyFailedException.class, RetryException.class, JobInterruptedException.class, Error.class,
BatchRuntimeException.class, BeanCreationException.class);
}
protected void detectStreamInReader() {
if (streamIsReader) {
if (!concurrent()) {
chunkMonitor.setItemReader(getReader());
}
else {
logger.warn("Asynchronous TaskExecutor detected with ItemStream reader. This is probably an error, "
+ "and may lead to incorrect restart data being stored.");
}
}
}
/**
* Register explicitly set item listeners and auto-register reader, processor and writer if applicable
*/
private void registerSkipListeners() {
// auto-register reader, processor and writer
for (Object itemHandler : new Object[] { getReader(), getWriter(), getProcessor() }) {
if (StepListenerFactoryBean.isListener(itemHandler)) {
StepListener listener = StepListenerFactoryBean.getListener(itemHandler);
if (listener instanceof SkipListener<?, ?>) {
@SuppressWarnings("unchecked")
SkipListener<? super I, ? super O> skipListener = (SkipListener<? super I, ? super O>) listener;
skipListeners.add(skipListener);
}
}
}
}
/**
* Convenience method to get an exception classifier based on the provided transaction attributes.
*
* @return an exception classifier: maps to true if an exception should cause rollback
*/
protected Classifier<Throwable, Boolean> getRollbackClassifier() {
Classifier<Throwable, Boolean> classifier = new BinaryExceptionClassifier(noRollbackExceptionClasses, false);
// Try to avoid pathological cases where we cannot force a rollback
// (should be pretty uncommon):
if (!classifier.classify(new ForceRollbackForWriteSkipException("test", new RuntimeException()))
|| !classifier.classify(new ExhaustedRetryException("test"))) {
final Classifier<Throwable, Boolean> binary = classifier;
Collection<Class<? extends Throwable>> types = new HashSet<>();
types.add(ForceRollbackForWriteSkipException.class);
types.add(ExhaustedRetryException.class);
final Classifier<Throwable, Boolean> panic = new BinaryExceptionClassifier(types, true);
classifier = (Classifier<Throwable, Boolean>) classifiable -> {
// Rollback if either the user's list or our own applies
return panic.classify(classifiable) || binary.classify(classifiable);
};
}
return classifier;
}
@SuppressWarnings("serial")
private TransactionAttribute getTransactionAttribute(TransactionAttribute attribute) {
final Classifier<Throwable, Boolean> classifier = getRollbackClassifier();
return new DefaultTransactionAttribute(attribute) {
@Override
public boolean rollbackOn(Throwable ex) {
return classifier.classify(ex);
}
};
}
protected SkipPolicy createSkipPolicy() {
SkipPolicy skipPolicy = this.skipPolicy;
Map<Class<? extends Throwable>, Boolean> map = new HashMap<>(
skippableExceptionClasses);
map.put(ForceRollbackForWriteSkipException.class, true);
LimitCheckingItemSkipPolicy limitCheckingItemSkipPolicy = new LimitCheckingItemSkipPolicy(skipLimit, map);
if (skipPolicy == null) {
Assert.state(!(skippableExceptionClasses.isEmpty() && skipLimit > 0),
"If a skip limit is provided then skippable exceptions must also be specified");
skipPolicy = limitCheckingItemSkipPolicy;
}
else if (limitCheckingItemSkipPolicy != null) {
skipPolicy = new CompositeSkipPolicy(new SkipPolicy[] { skipPolicy, limitCheckingItemSkipPolicy });
}
return skipPolicy;
}
/**
* @return fully configured retry template for item processing phase.
*/
protected BatchRetryTemplate createRetryOperations() {
RetryPolicy retryPolicy = this.retryPolicy;
SimpleRetryPolicy simpleRetryPolicy = null;
Map<Class<? extends Throwable>, Boolean> map = new HashMap<>(
retryableExceptionClasses);
map.put(ForceRollbackForWriteSkipException.class, true);
simpleRetryPolicy = new SimpleRetryPolicy(retryLimit, map);
if (retryPolicy == null) {
Assert.state(!(retryableExceptionClasses.isEmpty() && retryLimit > 0),
"If a retry limit is provided then retryable exceptions must also be specified");
retryPolicy = simpleRetryPolicy;
}
else if ((!retryableExceptionClasses.isEmpty() && retryLimit > 0)) {
CompositeRetryPolicy compositeRetryPolicy = new CompositeRetryPolicy();
compositeRetryPolicy.setPolicies(new RetryPolicy[] { retryPolicy, simpleRetryPolicy });
retryPolicy = compositeRetryPolicy;
}
RetryPolicy retryPolicyWrapper = getFatalExceptionAwareProxy(retryPolicy);
BatchRetryTemplate batchRetryTemplate = new BatchRetryTemplate();
if (backOffPolicy != null) {
batchRetryTemplate.setBackOffPolicy(backOffPolicy);
}
batchRetryTemplate.setRetryPolicy(retryPolicyWrapper);
// Coordinate the retry policy with the exception handler:
RepeatOperations stepOperations = getStepOperations();
if (stepOperations instanceof RepeatTemplate) {
SimpleRetryExceptionHandler exceptionHandler = new SimpleRetryExceptionHandler(retryPolicyWrapper,
getExceptionHandler(), nonRetryableExceptionClasses);
((RepeatTemplate) stepOperations).setExceptionHandler(exceptionHandler);
}
if (retryContextCache != null) {
batchRetryTemplate.setRetryContextCache(retryContextCache);
}
if (retryListeners != null) {
batchRetryTemplate.setListeners(retryListeners.toArray(new RetryListener[0]));
}
return batchRetryTemplate;
}
protected ChunkMonitor getChunkMonitor() {
return this.chunkMonitor;
}
protected Set<SkipListener<? super I, ? super O>> getSkipListeners() {
return skipListeners;
}
protected Set<org.springframework.batch.core.jsr.RetryListener> getJsrRetryListeners() {
return jsrRetryListeners;
}
/**
* Wrap the provided {@link org.springframework.retry.RetryPolicy} so that it never retries explicitly non-retryable
* exceptions.
*/
private RetryPolicy getFatalExceptionAwareProxy(RetryPolicy retryPolicy) {
NeverRetryPolicy neverRetryPolicy = new NeverRetryPolicy();
Map<Class<? extends Throwable>, RetryPolicy> map = new HashMap<>();
for (Class<? extends Throwable> fatal : nonRetryableExceptionClasses) {
map.put(fatal, neverRetryPolicy);
}
SubclassClassifier<Throwable, RetryPolicy> classifier = new SubclassClassifier<>(
retryPolicy);
classifier.setTypeMap(map);
ExceptionClassifierRetryPolicy retryPolicyWrapper = new ExceptionClassifierRetryPolicy();
retryPolicyWrapper.setExceptionClassifier(classifier);
return retryPolicyWrapper;
}
/**
* Wrap a {@link SkipPolicy} and make it consistent with known fatal exceptions.
*
* @param skipPolicy an existing skip policy
* @return a skip policy that will not skip fatal exceptions
*/
protected SkipPolicy getFatalExceptionAwareProxy(SkipPolicy skipPolicy) {
NeverSkipItemSkipPolicy neverSkipPolicy = new NeverSkipItemSkipPolicy();
Map<Class<? extends Throwable>, SkipPolicy> map = new HashMap<>();
for (Class<? extends Throwable> fatal : nonSkippableExceptionClasses) {
map.put(fatal, neverSkipPolicy);
}
SubclassClassifier<Throwable, SkipPolicy> classifier = new SubclassClassifier<>(skipPolicy);
classifier.setTypeMap(map);
ExceptionClassifierSkipPolicy skipPolicyWrapper = new ExceptionClassifierSkipPolicy();
skipPolicyWrapper.setExceptionClassifier(classifier);
return skipPolicyWrapper;
}
@SuppressWarnings("unchecked")
private void addNonSkippableExceptionIfMissing(Class<? extends Throwable>... cls) {
List<Class<? extends Throwable>> exceptions = new ArrayList<>();
for (Class<? extends Throwable> exceptionClass : nonSkippableExceptionClasses) {
exceptions.add(exceptionClass);
}
for (Class<? extends Throwable> fatal : cls) {
if (!exceptions.contains(fatal)) {
exceptions.add(fatal);
}
}
nonSkippableExceptionClasses = exceptions;
}
@SuppressWarnings("unchecked")
private void addNonRetryableExceptionIfMissing(Class<? extends Throwable>... cls) {
List<Class<? extends Throwable>> exceptions = new ArrayList<>();
for (Class<? extends Throwable> exceptionClass : nonRetryableExceptionClasses) {
exceptions.add(exceptionClass);
}
for (Class<? extends Throwable> fatal : cls) {
if (!exceptions.contains(fatal)) {
exceptions.add(fatal);
}
}
nonRetryableExceptionClasses = exceptions;
}
/**
* ChunkListener that wraps exceptions thrown from the ChunkListener in {@link FatalStepExecutionException} to force
* termination of StepExecution
*
* ChunkListeners shoulnd't throw exceptions and expect continued processing, they must be handled in the
* implementation or the step will terminate
*
*/
private class TerminateOnExceptionChunkListenerDelegate implements ChunkListener {
private ChunkListener chunkListener;
TerminateOnExceptionChunkListenerDelegate(ChunkListener chunkListener) {
this.chunkListener = chunkListener;
}
@Override
public void beforeChunk(ChunkContext context) {
try {
chunkListener.beforeChunk(context);
}
catch (Throwable t) {
throw new FatalStepExecutionException("ChunkListener threw exception, rethrowing as fatal", t);
}
}
@Override
public void afterChunk(ChunkContext context) {
try {
chunkListener.afterChunk(context);
}
catch (Throwable t) {
throw new FatalStepExecutionException("ChunkListener threw exception, rethrowing as fatal", t);
}
}
@Override
public void afterChunkError(ChunkContext context) {
try {
chunkListener.afterChunkError(context);
}
catch (Throwable t) {
throw new FatalStepExecutionException("ChunkListener threw exception, rethrowing as fatal", t);
}
}
@Override
public int hashCode() {
return chunkListener.hashCode();
}
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object obj) {
if (obj instanceof FaultTolerantStepBuilder.TerminateOnExceptionChunkListenerDelegate){
// unwrap the ChunkListener
obj = ((TerminateOnExceptionChunkListenerDelegate)obj).chunkListener;
}
return chunkListener.equals(obj);
}
}
}