1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.processor; |
18 |
|
|
19 |
|
import java.io.Serializable; |
20 |
|
import java.util.Random; |
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
public class RedeliveryPolicy implements Cloneable, Serializable { |
33 |
|
protected static transient Random randomNumberGenerator; |
34 |
327 |
protected int maximumRedeliveries = 6; |
35 |
327 |
protected long initialRedeliveryDelay = 1000L; |
36 |
327 |
protected double backOffMultiplier = 2; |
37 |
|
protected boolean useExponentialBackOff; |
38 |
|
|
39 |
327 |
protected double collisionAvoidanceFactor = 0.15d; |
40 |
|
protected boolean useCollisionAvoidance; |
41 |
|
|
42 |
327 |
public RedeliveryPolicy() { |
43 |
327 |
} |
44 |
|
|
45 |
|
@Override |
46 |
|
public String toString() { |
47 |
297 |
return "RedeliveryPolicy[maximumRedeliveries=" + maximumRedeliveries + "]"; |
48 |
|
} |
49 |
|
|
50 |
|
public RedeliveryPolicy copy() { |
51 |
|
try { |
52 |
12 |
return (RedeliveryPolicy)clone(); |
53 |
0 |
} catch (CloneNotSupportedException e) { |
54 |
0 |
throw new RuntimeException("Could not clone: " + e, e); |
55 |
|
} |
56 |
|
} |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
public boolean shouldRedeliver(int redeliveryCounter) { |
63 |
21 |
return redeliveryCounter < getMaximumRedeliveries(); |
64 |
|
} |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
public RedeliveryPolicy maximumRedeliveries(int maximumRedeliveries) { |
73 |
6 |
setMaximumRedeliveries(maximumRedeliveries); |
74 |
6 |
return this; |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
public RedeliveryPolicy initialRedeliveryDelay(long initialRedeliveryDelay) { |
81 |
6 |
setInitialRedeliveryDelay(initialRedeliveryDelay); |
82 |
6 |
return this; |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
public RedeliveryPolicy useCollisionAvoidance() { |
90 |
0 |
setUseCollisionAvoidance(true); |
91 |
0 |
return this; |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
public RedeliveryPolicy useExponentialBackOff() { |
99 |
0 |
setUseExponentialBackOff(true); |
100 |
0 |
return this; |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
public RedeliveryPolicy backOffMultiplier(double multiplier) { |
108 |
0 |
useExponentialBackOff(); |
109 |
0 |
setBackOffMultiplier(multiplier); |
110 |
0 |
return this; |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
public RedeliveryPolicy collisionAvoidancePercent(double collisionAvoidancePercent) { |
117 |
0 |
useCollisionAvoidance(); |
118 |
0 |
setCollisionAvoidancePercent(collisionAvoidancePercent); |
119 |
0 |
return this; |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
public double getBackOffMultiplier() { |
125 |
0 |
return backOffMultiplier; |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
public void setBackOffMultiplier(double backOffMultiplier) { |
133 |
0 |
this.backOffMultiplier = backOffMultiplier; |
134 |
0 |
} |
135 |
|
|
136 |
|
public short getCollisionAvoidancePercent() { |
137 |
0 |
return (short)Math.round(collisionAvoidanceFactor * 100); |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
public void setCollisionAvoidancePercent(double collisionAvoidancePercent) { |
145 |
0 |
this.collisionAvoidanceFactor = collisionAvoidancePercent * 0.01d; |
146 |
0 |
} |
147 |
|
|
148 |
|
public double getCollisionAvoidanceFactor() { |
149 |
0 |
return collisionAvoidanceFactor; |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
public void setCollisionAvoidanceFactor(double collisionAvoidanceFactor) { |
157 |
0 |
this.collisionAvoidanceFactor = collisionAvoidanceFactor; |
158 |
0 |
} |
159 |
|
|
160 |
|
public long getInitialRedeliveryDelay() { |
161 |
0 |
return initialRedeliveryDelay; |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
public void setInitialRedeliveryDelay(long initialRedeliveryDelay) { |
168 |
6 |
this.initialRedeliveryDelay = initialRedeliveryDelay; |
169 |
6 |
} |
170 |
|
|
171 |
|
public int getMaximumRedeliveries() { |
172 |
21 |
return maximumRedeliveries; |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
public void setMaximumRedeliveries(int maximumRedeliveries) { |
179 |
18 |
this.maximumRedeliveries = maximumRedeliveries; |
180 |
18 |
} |
181 |
|
|
182 |
|
public long getRedeliveryDelay(long previousDelay) { |
183 |
|
long redeliveryDelay; |
184 |
|
|
185 |
9 |
if (previousDelay == 0) { |
186 |
9 |
redeliveryDelay = initialRedeliveryDelay; |
187 |
9 |
} else if (useExponentialBackOff && backOffMultiplier > 1) { |
188 |
0 |
redeliveryDelay = Math.round(backOffMultiplier * previousDelay); |
189 |
0 |
} else { |
190 |
0 |
redeliveryDelay = previousDelay; |
191 |
|
} |
192 |
|
|
193 |
9 |
if (useCollisionAvoidance) { |
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
0 |
Random random = getRandomNumberGenerator(); |
200 |
0 |
double variance = (random.nextBoolean() ? collisionAvoidanceFactor : -collisionAvoidanceFactor) |
201 |
|
* random.nextDouble(); |
202 |
0 |
redeliveryDelay += redeliveryDelay * variance; |
203 |
|
} |
204 |
|
|
205 |
9 |
return redeliveryDelay; |
206 |
|
} |
207 |
|
|
208 |
|
public boolean isUseCollisionAvoidance() { |
209 |
0 |
return useCollisionAvoidance; |
210 |
|
} |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
public void setUseCollisionAvoidance(boolean useCollisionAvoidance) { |
217 |
0 |
this.useCollisionAvoidance = useCollisionAvoidance; |
218 |
0 |
} |
219 |
|
|
220 |
|
public boolean isUseExponentialBackOff() { |
221 |
0 |
return useExponentialBackOff; |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
public void setUseExponentialBackOff(boolean useExponentialBackOff) { |
229 |
0 |
this.useExponentialBackOff = useExponentialBackOff; |
230 |
0 |
} |
231 |
|
|
232 |
|
protected static synchronized Random getRandomNumberGenerator() { |
233 |
0 |
if (randomNumberGenerator == null) { |
234 |
0 |
randomNumberGenerator = new Random(); |
235 |
|
} |
236 |
0 |
return randomNumberGenerator; |
237 |
|
} |
238 |
|
} |