EMMA Coverage Report (generated Tue May 06 07:28:24 PDT 2008)
[all classes][org.springframework.batch.retry.policy]

COVERAGE SUMMARY FOR SOURCE FILE [TimeoutRetryPolicy.java]

nameclass, %method, %block, %line, %
TimeoutRetryPolicy.java100% (2/2)100% (8/8)100% (49/49)100% (15/15)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class TimeoutRetryPolicy100% (1/1)100% (6/6)100% (27/27)100% (10/10)
TimeoutRetryPolicy (): void 100% (1/1)100% (6/6)100% (3/3)
canRetry (RetryContext): boolean 100% (1/1)100% (4/4)100% (1/1)
close (RetryContext): void 100% (1/1)100% (1/1)100% (1/1)
open (RetryCallback, RetryContext): RetryContext 100% (1/1)100% (7/7)100% (1/1)
registerThrowable (RetryContext, Throwable): void 100% (1/1)100% (5/5)100% (2/2)
setTimeout (long): void 100% (1/1)100% (4/4)100% (2/2)
     
class TimeoutRetryPolicy$TimeoutRetryContext100% (1/1)100% (2/2)100% (22/22)100% (5/5)
TimeoutRetryPolicy$TimeoutRetryContext (RetryContext, long): void 100% (1/1)100% (10/10)100% (4/4)
isAlive (): boolean 100% (1/1)100% (12/12)100% (1/1)

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 
17package org.springframework.batch.retry.policy;
18 
19import org.springframework.batch.retry.RetryCallback;
20import org.springframework.batch.retry.RetryContext;
21import org.springframework.batch.retry.RetryPolicy;
22import org.springframework.batch.retry.TerminatedRetryException;
23import 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 */
32public 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}

[all classes][org.springframework.batch.retry.policy]
EMMA 2.0.5312 (C) Vladimir Roubtsov