1 | /* |
2 | * Copyright 2006-2007 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | package org.springframework.batch.core.step.item; |
18 | |
19 | import org.springframework.batch.classify.BinaryExceptionClassifier; |
20 | import org.springframework.batch.classify.Classifier; |
21 | import org.springframework.batch.core.StepContribution; |
22 | import org.springframework.batch.core.step.skip.LimitCheckingItemSkipPolicy; |
23 | import org.springframework.batch.core.step.skip.NonSkippableReadException; |
24 | import org.springframework.batch.core.step.skip.SkipListenerFailedException; |
25 | import org.springframework.batch.core.step.skip.SkipPolicy; |
26 | import org.springframework.batch.item.ItemReader; |
27 | import org.springframework.batch.repeat.RepeatOperations; |
28 | |
29 | /** |
30 | * FaultTolerant implementation of the {@link ChunkProcessor} interface, that |
31 | * allows for skipping or retry of items that cause exceptions during reading or |
32 | * processing. |
33 | * |
34 | */ |
35 | public class FaultTolerantChunkProvider<I> extends SimpleChunkProvider<I> { |
36 | |
37 | private SkipPolicy skipPolicy = new LimitCheckingItemSkipPolicy(0); |
38 | |
39 | private Classifier<Throwable, Boolean> rollbackClassifier = new BinaryExceptionClassifier(true); |
40 | |
41 | public FaultTolerantChunkProvider(ItemReader<? extends I> itemReader, RepeatOperations repeatOperations) { |
42 | super(itemReader, repeatOperations); |
43 | } |
44 | |
45 | /** |
46 | * The policy that determines whether exceptions can be skipped on read. |
47 | * @param SkipPolicy |
48 | */ |
49 | public void setSkipPolicy(SkipPolicy SkipPolicy) { |
50 | this.skipPolicy = SkipPolicy; |
51 | } |
52 | |
53 | /** |
54 | * Classifier to determine whether exceptions have been marked as |
55 | * no-rollback (as opposed to skippable). If ecnounterd they are simply |
56 | * ignored, unless also skippable. |
57 | * |
58 | * @param rollbackClassifier the rollback classifier to set |
59 | */ |
60 | public void setRollbackClassifier(Classifier<Throwable, Boolean> rollbackClassifier) { |
61 | this.rollbackClassifier = rollbackClassifier; |
62 | } |
63 | |
64 | @Override |
65 | protected I read(StepContribution contribution, Chunk<I> chunk) throws Exception { |
66 | while (true) { |
67 | try { |
68 | return doRead(); |
69 | } |
70 | catch (Exception e) { |
71 | |
72 | if (skipPolicy.shouldSkip(e, contribution.getStepSkipCount())) { |
73 | // increment skip count and try again |
74 | contribution.incrementReadSkipCount(); |
75 | chunk.skip(e); |
76 | |
77 | logger.debug("Skipping failed input", e); |
78 | } |
79 | else { |
80 | if (rollbackClassifier.classify(e)) { |
81 | throw new NonSkippableReadException("Non-skippable exception during read", e); |
82 | } |
83 | logger.debug("No-rollback for non-skippable exception (ignored)", e); |
84 | } |
85 | |
86 | } |
87 | } |
88 | } |
89 | |
90 | @Override |
91 | public void postProcess(StepContribution contribution, Chunk<I> chunk) { |
92 | for (Exception e : chunk.getErrors()) { |
93 | try { |
94 | getListener().onSkipInRead(e); |
95 | } |
96 | catch (RuntimeException ex) { |
97 | throw new SkipListenerFailedException("Fatal exception in SkipListener.", ex, e); |
98 | } |
99 | } |
100 | } |
101 | |
102 | } |