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