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.retry.context; |
18 | |
19 | import org.springframework.batch.retry.RetryContext; |
20 | import org.springframework.batch.retry.RetryPolicy; |
21 | import org.springframework.core.AttributeAccessorSupport; |
22 | |
23 | public class RetryContextSupport extends AttributeAccessorSupport implements RetryContext { |
24 | |
25 | private boolean terminate = false; |
26 | |
27 | private int count; |
28 | |
29 | private Throwable lastException; |
30 | |
31 | private RetryContext parent; |
32 | |
33 | public RetryContextSupport(RetryContext parent) { |
34 | super(); |
35 | this.parent = parent; |
36 | } |
37 | |
38 | public RetryContext getParent() { |
39 | return this.parent; |
40 | } |
41 | |
42 | public boolean isExhaustedOnly() { |
43 | return terminate; |
44 | } |
45 | |
46 | public void setExhaustedOnly() { |
47 | terminate = true; |
48 | } |
49 | |
50 | public int getRetryCount() { |
51 | return count; |
52 | } |
53 | |
54 | public Throwable getLastThrowable() { |
55 | return lastException; |
56 | } |
57 | |
58 | /** |
59 | * Set the exception for the public interface {@link RetryContext}, and |
60 | * also increment the retry count if the throwable is non-null.<br/> |
61 | * |
62 | * All {@link RetryPolicy} implementations should use this method when they |
63 | * register the throwable. It should only be called once per retry attempt |
64 | * because it increments the conter.<br/> |
65 | * |
66 | * Use of this method is not enforced by the framework - it is a service |
67 | * provider contract for authors of policies. |
68 | * |
69 | * @param throwable the exception that caused the current retry attempt to |
70 | * fail. |
71 | */ |
72 | public void registerThrowable(Throwable throwable) { |
73 | this.lastException = throwable; |
74 | if (throwable != null) |
75 | count++; |
76 | } |
77 | |
78 | } |