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.policy; |
18 | |
19 | import org.springframework.batch.retry.RetryCallback; |
20 | import org.springframework.batch.retry.RetryContext; |
21 | import org.springframework.batch.retry.RetryPolicy; |
22 | import org.springframework.batch.retry.TerminatedRetryException; |
23 | import org.springframework.batch.retry.context.RetryContextSupport; |
24 | |
25 | /** |
26 | * A {@link RetryPolicy} that allows the first attempt but never permits a |
27 | * retry. Also be used as a base class for other policies, e.g. for test |
28 | * purposes as a stub. |
29 | * |
30 | * @author Dave Syer |
31 | * |
32 | */ |
33 | public class NeverRetryPolicy extends AbstractStatelessRetryPolicy { |
34 | |
35 | /** |
36 | * Returns false after the first exception. So there is always one try, and |
37 | * then the retry is prevented. |
38 | * |
39 | * @see org.springframework.batch.retry.RetryPolicy#canRetry(org.springframework.batch.retry.RetryContext) |
40 | */ |
41 | public boolean canRetry(RetryContext context) { |
42 | return !((NeverRetryContext) context).isFinished(); |
43 | } |
44 | |
45 | /** |
46 | * Do nothing. |
47 | * |
48 | * @see org.springframework.batch.retry.RetryPolicy#close(org.springframework.batch.retry.RetryContext) |
49 | */ |
50 | public void close(RetryContext context) { |
51 | // no-op |
52 | } |
53 | |
54 | /** |
55 | * Return a context that can respond to early termination requests, but does |
56 | * nothing else. |
57 | * |
58 | * @see org.springframework.batch.retry.RetryPolicy#open(org.springframework.batch.retry.RetryCallback, RetryContext) |
59 | */ |
60 | public RetryContext open(RetryCallback callback, RetryContext parent) { |
61 | return new NeverRetryContext(parent); |
62 | } |
63 | |
64 | /** |
65 | * Do nothing. |
66 | * @see org.springframework.batch.retry.RetryPolicy#registerThrowable(org.springframework.batch.retry.RetryContext, |
67 | * java.lang.Throwable) |
68 | */ |
69 | public void registerThrowable(RetryContext context, Throwable throwable) throws TerminatedRetryException { |
70 | ((NeverRetryContext) context).setFinished(); |
71 | ((RetryContextSupport) context).registerThrowable(throwable); |
72 | } |
73 | |
74 | /** |
75 | * Special context object for {@link NeverRetryPolicy}. Implements a flag |
76 | * with a similar function to {@link RetryContext#isExhaustedOnly()}, but |
77 | * kept separate so that if subclasses of {@link NeverRetryPolicy} need to |
78 | * they can modify the behaviour of |
79 | * {@link NeverRetryPolicy#canRetry(RetryContext)} without affecting |
80 | * {@link RetryContext#isExhaustedOnly()}. |
81 | * |
82 | * @author Dave Syer |
83 | * |
84 | */ |
85 | private static class NeverRetryContext extends RetryContextSupport { |
86 | private boolean finished = false; |
87 | |
88 | public NeverRetryContext(RetryContext parent) { |
89 | super(parent); |
90 | } |
91 | |
92 | public boolean isFinished() { |
93 | return finished; |
94 | } |
95 | |
96 | public void setFinished() { |
97 | this.finished = true; |
98 | } |
99 | } |
100 | |
101 | } |