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 a retry only if it hasn't timed out. The |
27 | * clock is started on a call to {@link #open(RetryCallback, RetryContext)}. |
28 | * |
29 | * @author Dave Syer |
30 | * |
31 | */ |
32 | public class TimeoutRetryPolicy extends AbstractStatelessRetryPolicy { |
33 | |
34 | /** |
35 | * Default value for timeout (milliseconds). |
36 | */ |
37 | public static final long DEFAULT_TIMEOUT = 1000; |
38 | |
39 | private long timeout = DEFAULT_TIMEOUT; |
40 | |
41 | /** |
42 | * Setter for timeout. Default is {@link #DEFAULT_TIMEOUT}. |
43 | * @param timeout |
44 | */ |
45 | public void setTimeout(long timeout) { |
46 | this.timeout = timeout; |
47 | } |
48 | |
49 | /** |
50 | * Only permits a retry if the timeout has not expired. Does not check the |
51 | * exception at all. |
52 | * |
53 | * @see org.springframework.batch.retry.RetryPolicy#canRetry(org.springframework.batch.retry.RetryContext) |
54 | */ |
55 | public boolean canRetry(RetryContext context) { |
56 | return ((TimeoutRetryContext) context).isAlive(); |
57 | } |
58 | |
59 | public void close(RetryContext context) { |
60 | } |
61 | |
62 | public RetryContext open(RetryCallback callback, RetryContext parent) { |
63 | return new TimeoutRetryContext(parent, timeout); |
64 | } |
65 | |
66 | public void registerThrowable(RetryContext context, Throwable throwable) throws TerminatedRetryException { |
67 | ((RetryContextSupport) context).registerThrowable(throwable); |
68 | // otherwise no-op - we only time out, otherwise retry everything... |
69 | } |
70 | |
71 | private static class TimeoutRetryContext extends RetryContextSupport { |
72 | private long timeout; |
73 | |
74 | private long start; |
75 | |
76 | public TimeoutRetryContext(RetryContext parent, long timeout) { |
77 | super(parent); |
78 | this.start = System.currentTimeMillis(); |
79 | this.timeout = timeout; |
80 | } |
81 | |
82 | public boolean isAlive() { |
83 | return (System.currentTimeMillis() - start) <= timeout; |
84 | } |
85 | } |
86 | |
87 | } |