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