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 | package org.springframework.batch.retry.support; |
17 | |
18 | import org.springframework.batch.classify.Classifier; |
19 | import org.springframework.batch.retry.RecoveryCallback; |
20 | import org.springframework.batch.retry.RetryCallback; |
21 | import org.springframework.batch.retry.RetryOperations; |
22 | import org.springframework.batch.retry.RetryState; |
23 | |
24 | /** |
25 | * |
26 | * @author Dave Syer |
27 | * |
28 | */ |
29 | public class DefaultRetryState implements RetryState { |
30 | |
31 | final private Object key; |
32 | |
33 | final private boolean forceRefresh; |
34 | |
35 | final private Classifier<? super Throwable, Boolean> rollbackClassifier; |
36 | |
37 | /** |
38 | * Create a {@link DefaultRetryState} representing the state for a new retry |
39 | * attempt. |
40 | * |
41 | * @see RetryOperations#execute(RetryCallback, RetryState) |
42 | * @see RetryOperations#execute(RetryCallback, RecoveryCallback, RetryState) |
43 | * |
44 | * @param key the key for the state to allow this retry attempt to be |
45 | * recognised |
46 | * @param forceRefresh true if the attempt is known to be a brand new state |
47 | * (could not have previously failed) |
48 | * @param rollbackClassifier the rollback classifier to set. The rollback |
49 | * classifier answers true if the exception provided should cause a |
50 | * rollback. |
51 | */ |
52 | public DefaultRetryState(Object key, boolean forceRefresh, Classifier<? super Throwable, Boolean> rollbackClassifier) { |
53 | this.key = key; |
54 | this.forceRefresh = forceRefresh; |
55 | this.rollbackClassifier = rollbackClassifier; |
56 | } |
57 | |
58 | /** |
59 | * Defaults the force refresh flag to false. |
60 | * @see DefaultRetryState#DefaultRetryState(Object, boolean, Classifier) |
61 | */ |
62 | public DefaultRetryState(Object key, Classifier<? super Throwable, Boolean> rollbackClassifier) { |
63 | this(key, false, rollbackClassifier); |
64 | } |
65 | |
66 | /** |
67 | * Defaults the rollback classifier to null. |
68 | * @see DefaultRetryState#DefaultRetryState(Object, boolean, Classifier) |
69 | */ |
70 | public DefaultRetryState(Object key, boolean forceRefresh) { |
71 | this(key, forceRefresh, null); |
72 | } |
73 | |
74 | /** |
75 | * Defaults the force refresh flag (to false) and the rollback classifier |
76 | * (to null). |
77 | * |
78 | * @see DefaultRetryState#DefaultRetryState(Object, boolean, Classifier) |
79 | */ |
80 | public DefaultRetryState(Object key) { |
81 | this(key, false, null); |
82 | } |
83 | |
84 | /* |
85 | * (non-Javadoc) |
86 | * |
87 | * @see org.springframework.batch.retry.IRetryState#getKey() |
88 | */ |
89 | public Object getKey() { |
90 | return key; |
91 | } |
92 | |
93 | /* |
94 | * (non-Javadoc) |
95 | * |
96 | * @see org.springframework.batch.retry.IRetryState#isForceRefresh() |
97 | */ |
98 | public boolean isForceRefresh() { |
99 | return forceRefresh; |
100 | } |
101 | |
102 | /* |
103 | * (non-Javadoc) |
104 | * |
105 | * @see |
106 | * org.springframework.batch.retry.RetryState#rollbackFor(java.lang.Throwable |
107 | * ) |
108 | */ |
109 | public boolean rollbackFor(Throwable exception) { |
110 | if (rollbackClassifier == null) { |
111 | return true; |
112 | } |
113 | return rollbackClassifier.classify(exception); |
114 | } |
115 | |
116 | @Override |
117 | public String toString() { |
118 | return String.format("[%s: key=%s, forceRefresh=%b]", getClass().getSimpleName(), key, forceRefresh); |
119 | } |
120 | } |