Skip to content

Commit 6a04831

Browse files
committed
Configurable back off for listener recovery
Prior to this commit, DefaultMessageListenerContainer was recovering on failure using a fixed time interval, potentially in an infinite way. This commit adds an extra "backoff" property to the container that permits to fine tune the recovery interval using a BackOff instance. FixedBackOff provides a fixed interval between two attempts and a maximum number of retries. ExponentialBackOff increases an initial interval until a maximum interval has been reached. A BackOff instance can return a special "STOP" time value that indicates that no further attemps should be made. DefaultMessageListenerContainer uses this value to stop the container. protected method "sleepInbetweenRecoveryAttempts" has been renamed to "applyBackOff" and now returns a boolean that indicate if the back off has been applied and a new attempt should now be made. Issue: SPR-11746
1 parent 97fb308 commit 6a04831

File tree

11 files changed

+804
-18
lines changed

11 files changed

+804
-18
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2002-2014 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.util;
18+
19+
/**
20+
* Indicate the rate at which an operation should be retried.
21+
*
22+
* <p>Users of this interface are expected to use it like this:
23+
*
24+
* <pre class="code">
25+
* {@code
26+
*
27+
* long waitInterval = backOff.nextBackOffMillis();
28+
* if (waitInterval == BackOff.STOP) {
29+
* backOff.reset();
30+
* // do not retry operation
31+
* }
32+
* else {
33+
* // sleep, e.g. Thread.sleep(waitInterval)
34+
* // retry operation
35+
* }
36+
* }</pre>
37+
*
38+
* Once the underlying operation has completed successfully, the instance
39+
* <b>must</b> be {@link #reset()} before further use. Due to how back off
40+
* should be used, implementations do not need to be thread-safe.
41+
*
42+
* @author Stephane Nicoll
43+
* @since 4.1
44+
*/
45+
public interface BackOff {
46+
47+
/**
48+
* Return value of {@link #nextBackOff()} that indicates that the operation
49+
* should not be retried.
50+
*/
51+
long STOP = -1;
52+
53+
/**
54+
* Return the number of milliseconds to wait before retrying the operation
55+
* or {@link #STOP} ({@value #STOP}) to indicate that no further attempt
56+
* should be made for the operation.
57+
*/
58+
long nextBackOff();
59+
60+
/**
61+
* Reset this instance to its original state.
62+
*/
63+
void reset();
64+
65+
}
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
* Copyright 2002-2014 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.util;
18+
19+
/**
20+
* Implementation of {@link BackOff} that increases the back off period for each
21+
* retry attempt. When the interval has reached the {@link #setMaxInterval(long)
22+
* max interval}, it is no longer increased. Stops retrying once the
23+
* {@link #setMaxElapsedTime(long) max elapsed time} has been reached.
24+
*
25+
* <p>Example: The default interval is {@value #DEFAULT_INITIAL_INTERVAL}ms, default
26+
* multiplier is {@value #DEFAULT_MULTIPLIER} and the default max interval is
27+
* {@value #DEFAULT_MAX_INTERVAL}. For 10 attempts the sequence will be
28+
* as follows:
29+
*
30+
* <pre>
31+
* request# back off
32+
*
33+
* 1 2000
34+
* 2 3000
35+
* 3 4500
36+
* 4 6750
37+
* 5 10125
38+
* 6 15187
39+
* 7 22780
40+
* 8 30000
41+
* 9 30000
42+
* 10 30000
43+
* </pre>
44+
*
45+
* Note that the default max elapsed time is {@link Long#MAX_VALUE}. Use
46+
* {@link #setMaxElapsedTime(long)} to limit the maximum number of time
47+
* that an instance should accumulate before returning {@link BackOff#STOP}.
48+
*
49+
* @author Stephane Nicoll
50+
* @since 4.1
51+
*/
52+
public class ExponentialBackOff implements BackOff {
53+
54+
/**
55+
* The default initial interval.
56+
*/
57+
public static final long DEFAULT_INITIAL_INTERVAL = 2000L;
58+
59+
/**
60+
* The default multiplier (increases the interval by 50%).
61+
*/
62+
public static final double DEFAULT_MULTIPLIER = 1.5;
63+
64+
/**
65+
* The default maximum back off time.
66+
*/
67+
public static final long DEFAULT_MAX_INTERVAL = 30000L;
68+
69+
/**
70+
* The default maximum elapsed time.
71+
*/
72+
public static final long DEFAULT_MAX_ELAPSED_TIME = Long.MAX_VALUE;
73+
74+
75+
private long initialInterval = DEFAULT_INITIAL_INTERVAL;
76+
77+
private double multiplier = DEFAULT_MULTIPLIER;
78+
79+
private long maxInterval = DEFAULT_MAX_INTERVAL;
80+
81+
private long maxElapsedTime = DEFAULT_MAX_ELAPSED_TIME;
82+
83+
private long currentInterval = -1;
84+
85+
private long currentElapsedTime = 0;
86+
87+
/**
88+
* Create an instance with the default settings.
89+
* @see #DEFAULT_INITIAL_INTERVAL
90+
* @see #DEFAULT_MULTIPLIER
91+
* @see #DEFAULT_MAX_INTERVAL
92+
* @see #DEFAULT_MAX_ELAPSED_TIME
93+
*/
94+
public ExponentialBackOff() {
95+
}
96+
97+
/**
98+
* Create an instance.
99+
* @param initialInterval the initial interval in milliseconds
100+
* @param multiplier the multiplier (should be equal or higher to 1)
101+
*/
102+
public ExponentialBackOff(long initialInterval, double multiplier) {
103+
checkMultiplier(multiplier);
104+
this.initialInterval = initialInterval;
105+
this.multiplier = multiplier;
106+
}
107+
108+
/**
109+
* The initial interval in milliseconds.
110+
*/
111+
public void setInitialInterval(long initialInterval) {
112+
this.initialInterval = initialInterval;
113+
}
114+
115+
/**
116+
* The value to multiply the current interval with for each retry attempt.
117+
*/
118+
public void setMultiplier(double multiplier) {
119+
checkMultiplier(multiplier);
120+
this.multiplier = multiplier;
121+
}
122+
123+
/**
124+
* The maximum back off time.
125+
*/
126+
public void setMaxInterval(long maxInterval) {
127+
this.maxInterval = maxInterval;
128+
}
129+
130+
/**
131+
* The maximum elapsed time in milliseconds after which a call to
132+
* {@link #nextBackOff()} returns {@link BackOff#STOP}.
133+
*/
134+
public void setMaxElapsedTime(long maxElapsedTime) {
135+
this.maxElapsedTime = maxElapsedTime;
136+
}
137+
138+
@Override
139+
public long nextBackOff() {
140+
if (currentElapsedTime >= maxElapsedTime) {
141+
return BackOff.STOP;
142+
}
143+
144+
long nextInterval = computeNextInterval();
145+
currentElapsedTime += nextInterval;
146+
return nextInterval;
147+
148+
}
149+
150+
@Override
151+
public void reset() {
152+
this.currentInterval = -1;
153+
this.currentElapsedTime = 0;
154+
}
155+
156+
private long computeNextInterval() {
157+
if (this.currentInterval >= this.maxInterval) {
158+
return this.maxInterval;
159+
}
160+
else if (this.currentInterval < 0) {
161+
this.currentInterval = (this.initialInterval < this.maxInterval
162+
? this.initialInterval : this.maxInterval);
163+
}
164+
else {
165+
this.currentInterval = multiplyInterval();
166+
}
167+
return currentInterval;
168+
}
169+
170+
private long multiplyInterval() {
171+
long i = this.currentInterval;
172+
i *= this.multiplier;
173+
return (i > this.maxInterval ? this.maxInterval :i);
174+
}
175+
176+
private void checkMultiplier(double multiplier) {
177+
if (multiplier < 1) {
178+
throw new IllegalArgumentException("Invalid multiplier '" + multiplier + "'. Should be equal" +
179+
"or higher than 1. A multiplier of 1 is equivalent to a fixed interval");
180+
}
181+
}
182+
183+
@Override
184+
public String toString() {
185+
String i = (this.currentInterval < 0 ? "n/a" : this.currentInterval + "ms");
186+
final StringBuilder sb = new StringBuilder("ExponentialBackOff{");
187+
sb.append("currentInterval=").append(i);
188+
sb.append(", multiplier=").append(this.multiplier);
189+
sb.append('}');
190+
return sb.toString();
191+
}
192+
193+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2002-2014 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.util;
18+
19+
/**
20+
* A simple {@link BackOff} implementation that provides a fixed interval
21+
* between two attempts and a maximum number of retries.
22+
*
23+
* @author Stephane Nicoll
24+
* @since 4.1
25+
*/
26+
public class FixedBackOff implements BackOff {
27+
28+
/**
29+
* The default recovery interval: 5000 ms = 5 seconds.
30+
*/
31+
public static final long DEFAULT_INTERVAL = 5000;
32+
33+
/**
34+
* Constant value indicating an unlimited number of attempts.
35+
*/
36+
public static final long UNLIMITED_ATTEMPTS = Long.MAX_VALUE;
37+
38+
private long interval = DEFAULT_INTERVAL;
39+
40+
private long maxAttempts = UNLIMITED_ATTEMPTS;
41+
42+
private long currentAttempts = 0;
43+
44+
/**
45+
* Create an instance with an interval of {@value #DEFAULT_INTERVAL}
46+
* ms and an unlimited number of attempts.
47+
*/
48+
public FixedBackOff() {
49+
}
50+
51+
/**
52+
* Create an instance.
53+
* @param interval the interval between two attempts
54+
* @param maxAttempts the maximal number of attempts
55+
*/
56+
public FixedBackOff(long interval, long maxAttempts) {
57+
this.interval = interval;
58+
this.maxAttempts = maxAttempts;
59+
}
60+
61+
/**
62+
* Set the interval between two attempts in milliseconds.
63+
*/
64+
public void setInterval(long interval) {
65+
this.interval = interval;
66+
}
67+
68+
/**
69+
* Return the interval between two attempts in milliseconds.
70+
*/
71+
public long getInterval() {
72+
return interval;
73+
}
74+
75+
/**
76+
* Set the maximal number of attempts in milliseconds.
77+
*/
78+
public void setMaxAttempts(long maxAttempts) {
79+
this.maxAttempts = maxAttempts;
80+
}
81+
82+
/**
83+
* Return the maximal number of attempts in milliseconds.
84+
*/
85+
public long getMaxAttempts() {
86+
return maxAttempts;
87+
}
88+
89+
@Override
90+
public long nextBackOff() {
91+
this.currentAttempts++;
92+
if (this.currentAttempts <= this.maxAttempts) {
93+
return this.interval;
94+
}
95+
else {
96+
return BackOff.STOP;
97+
}
98+
}
99+
100+
@Override
101+
public void reset() {
102+
this.currentAttempts = 0;
103+
}
104+
105+
@Override
106+
public String toString() {
107+
final StringBuilder sb = new StringBuilder("FixedBackOff{");
108+
sb.append("interval=").append(this.interval);
109+
String attemptValue = (this.maxAttempts == Long.MAX_VALUE ? "unlimited"
110+
: String.valueOf(this.maxAttempts));
111+
sb.append(", currentAttempts=").append(this.currentAttempts);
112+
sb.append(", maxAttempts=").append(attemptValue);
113+
sb.append('}');
114+
return sb.toString();
115+
}
116+
117+
}

0 commit comments

Comments
 (0)