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

COVERAGE SUMMARY FOR SOURCE FILE [ExponentialBackOffPolicy.java]

nameclass, %method, %block, %line, %
ExponentialBackOffPolicy.java100% (2/2)100% (10/10)95%  (132/139)94%  (31/33)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ExponentialBackOffPolicy100% (1/1)100% (8/8)93%  (97/104)91%  (20/22)
backOff (BackOffContext): void 100% (1/1)59%  (10/17)67%  (4/6)
ExponentialBackOffPolicy (): void 100% (1/1)100% (17/17)100% (6/6)
setInitialInterval (long): void 100% (1/1)100% (10/10)100% (2/2)
setMaxInterval (long): void 100% (1/1)100% (10/10)100% (2/2)
setMultiplier (double): void 100% (1/1)100% (10/10)100% (2/2)
setSleeper (Sleeper): void 100% (1/1)100% (4/4)100% (2/2)
start (RetryContext): BackOffContext 100% (1/1)100% (10/10)100% (1/1)
toString (): String 100% (1/1)100% (26/26)100% (1/1)
     
class ExponentialBackOffPolicy$ExponentialBackOffContext100% (1/1)100% (2/2)100% (35/35)100% (11/11)
ExponentialBackOffPolicy$ExponentialBackOffContext (long, double, long): void 100% (1/1)100% (12/12)100% (5/5)
getSleepAndIncrement (): long 100% (1/1)100% (23/23)100% (6/6)

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.backoff;
18 
19import org.springframework.batch.retry.RetryContext;
20import org.springframework.util.ClassUtils;
21 
22/**
23 * Implementation of {@link BackOffPolicy} that increases the back off period
24 * for each retry attempt in a given set using the
25 * {@link Math#exp(double) exponential} function. <p/> This implementation is
26 * thread-safe and suitable for concurrent access. Modifications to the
27 * configuration do not affect any retry sets that are already in progress. <p/>
28 * The {@link #setInitialInterval(long)} property controls the initial value passed to
29 * {@link Math#exp(double)} and the {@link #setMultiplier(double)} property controls by
30 * how much this value is increased for each subsequent attempt.
31 * 
32 * @author Rob Harrop
33 * @author Dave Syer
34 */
35public class ExponentialBackOffPolicy implements BackOffPolicy {
36 
37        /**
38         * The default 'initialInterval' value - 100 millisecs. Coupled with the
39         * default 'multiplier' value this gives a useful initial spread of pauses
40         * for 1-5 retries.
41         */
42        public static final long DEFAULT_INITIAL_INTERVAL = 100L;
43 
44        /**
45         * The default maximum backoff time (30 seconds).
46         */
47        public static final long DEFAULT_MAX_INTERVAL = 30000L;
48 
49        /**
50         * The default 'multiplier' value - value 2 (100% increase per backoff).
51         */
52        public static final double DEFAULT_MULTIPLIER = 2;
53 
54        /**
55         * The initial sleep interval.
56         */
57        private volatile long initialInterval = DEFAULT_INITIAL_INTERVAL;
58 
59        /**
60         * The maximum value of the backoff period in milliseconds.
61         */
62        private volatile long maxInterval = DEFAULT_MAX_INTERVAL;
63 
64        /**
65         * The value to increment the exp seed with for each retry attempt.
66         */
67        private volatile double multiplier = DEFAULT_MULTIPLIER;
68 
69        private Sleeper sleeper = new ObjectWaitSleeper();
70 
71        /**
72         * Public setter for the {@link Sleeper} strategy.
73         * @param sleeper the sleeper to set defaults to {@link ObjectWaitSleeper}.
74         */
75        public void setSleeper(Sleeper sleeper) {
76                this.sleeper = sleeper;
77        }
78 
79        /**
80         * Set the initial sleep interval value. Default is <code>1</code>
81         * millisecond. Cannot be set to a value less than one.
82         */
83        public void setInitialInterval(long initialInterval) {
84                this.initialInterval = (initialInterval > 1 ? initialInterval : 1);
85        }
86 
87        /**
88         * Set the multiplier value. Default is '<code>1.0</code>'.
89         */
90        public void setMultiplier(double multiplier) {
91                this.multiplier = (multiplier > 1.0 ? multiplier : 1.0);
92        }
93 
94        /**
95         * Setter for maximum back off period. Default is 30000 (30 seconds). the
96         * value will be reset to 1 if this method is called with a value less than
97         * 1.
98         * 
99         * @param maxInterval in milliseconds.
100         */
101        public void setMaxInterval(long maxInterval) {
102                this.maxInterval = maxInterval > 0 ? maxInterval : 1;
103        }
104 
105        /**
106         * Returns a new instance of {@link BackOffContext} configured
107         * with the 'expSeed' and 'increment' values.
108         */
109        public BackOffContext start(RetryContext context) {
110                return new ExponentialBackOffContext(this.initialInterval, this.multiplier, this.maxInterval);
111        }
112 
113        /**
114         * Pause for a length of time equal to '<code>exp(backOffContext.expSeed)</code>'.
115         */
116        public void backOff(BackOffContext backOffContext) throws BackOffInterruptedException {
117                ExponentialBackOffContext context = (ExponentialBackOffContext) backOffContext;
118                try {
119                        sleeper.sleep(context.getSleepAndIncrement());
120                }
121                catch (InterruptedException e) {
122                        throw new BackOffInterruptedException("Thread interrupted while sleeping", e);
123                }
124        }
125 
126        private static class ExponentialBackOffContext implements BackOffContext {
127 
128                private final double multiplier;
129 
130                private long interval;
131 
132                private long maxInterval;
133 
134                public ExponentialBackOffContext(long expSeed, double multiplier, long maxInterval) {
135                        this.interval = expSeed;
136                        this.multiplier = multiplier;
137                        this.maxInterval = maxInterval;
138                }
139 
140                public synchronized long getSleepAndIncrement() {
141                        long sleep = this.interval;
142                        if (sleep > maxInterval) {
143                                sleep = (long) maxInterval;
144                        }
145                        else {
146                                this.interval *= this.multiplier;
147                        }
148                        return sleep;
149                }
150        }
151 
152        public String toString() {
153                return ClassUtils.getShortName(getClass()) + "[initialInterval=" + initialInterval + ", multiplier="
154                                + multiplier + ", maxInterval=" + maxInterval + "]";
155        }
156 
157}

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