001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.model;
018
019 import javax.xml.bind.annotation.XmlAccessType;
020 import javax.xml.bind.annotation.XmlAccessorType;
021 import javax.xml.bind.annotation.XmlAttribute;
022 import javax.xml.bind.annotation.XmlRootElement;
023
024 import org.apache.camel.CamelContext;
025 import org.apache.camel.LoggingLevel;
026 import org.apache.camel.processor.RedeliveryPolicy;
027 import org.apache.camel.util.CamelContextHelper;
028
029 /**
030 * Represents an XML <redeliveryPolicy/> element
031 *
032 * @version $Revision: 751447 $
033 */
034 @XmlRootElement(name = "redeliveryPolicy")
035 @XmlAccessorType(XmlAccessType.FIELD)
036 public class RedeliveryPolicyDefinition {
037 @XmlAttribute()
038 private String ref;
039 @XmlAttribute
040 private Integer maximumRedeliveries;
041 @XmlAttribute
042 private Long redeliveryDelay;
043 @XmlAttribute
044 private Double backOffMultiplier;
045 @XmlAttribute
046 private Boolean useExponentialBackOff;
047 @XmlAttribute
048 private Double collisionAvoidanceFactor;
049 @XmlAttribute
050 private Boolean useCollisionAvoidance;
051 @XmlAttribute
052 private Long maximumRedeliveryDelay;
053 @XmlAttribute
054 private LoggingLevel retriesExhaustedLogLevel;
055 @XmlAttribute
056 private LoggingLevel retryAttemptedLogLevel;
057 @XmlAttribute
058 private Boolean logStackTrace;
059
060 public RedeliveryPolicy createRedeliveryPolicy(CamelContext context, RedeliveryPolicy parentPolicy) {
061 if (ref != null) {
062 // lookup in registry if ref provided
063 return CamelContextHelper.mandatoryLookup(context, ref, RedeliveryPolicy.class);
064 }
065
066 RedeliveryPolicy answer = parentPolicy.copy();
067
068 // copy across the properties - if they are set
069 if (maximumRedeliveries != null) {
070 answer.setMaximumRedeliveries(maximumRedeliveries);
071 }
072 if (redeliveryDelay != null) {
073 answer.setDelay(redeliveryDelay);
074 }
075 if (retriesExhaustedLogLevel != null) {
076 answer.setRetriesExhaustedLogLevel(retriesExhaustedLogLevel);
077 }
078 if (retryAttemptedLogLevel != null) {
079 answer.setRetryAttemptedLogLevel(retryAttemptedLogLevel);
080 }
081 if (backOffMultiplier != null) {
082 answer.setBackOffMultiplier(backOffMultiplier);
083 }
084 if (useExponentialBackOff != null) {
085 answer.setUseExponentialBackOff(useExponentialBackOff);
086 }
087 if (collisionAvoidanceFactor != null) {
088 answer.setCollisionAvoidanceFactor(collisionAvoidanceFactor);
089 }
090 if (useCollisionAvoidance != null) {
091 answer.setUseCollisionAvoidance(useCollisionAvoidance);
092 }
093 if (maximumRedeliveryDelay != null) {
094 answer.setMaximumRedeliveryDelay(maximumRedeliveryDelay);
095 }
096 if (logStackTrace != null) {
097 answer.setLogStackTrace(logStackTrace);
098 }
099 return answer;
100 }
101
102 public String toString() {
103 return "RedeliveryPolicy[maximumRedeliveries: " + maximumRedeliveries + "]";
104 }
105
106 // Fluent API
107 //-------------------------------------------------------------------------
108 /**
109 * Sets the back off multiplier
110 *
111 * @param backOffMultiplier the back off multiplier
112 * @return the builder
113 */
114 public RedeliveryPolicyDefinition backOffMultiplier(double backOffMultiplier) {
115 setBackOffMultiplier(backOffMultiplier);
116 return this;
117 }
118
119 /**
120 * Sets the collision avoidance percentage
121 *
122 * @param collisionAvoidancePercent the percentage
123 * @return the builder
124 */
125 public RedeliveryPolicyDefinition collisionAvoidancePercent(double collisionAvoidancePercent) {
126 setCollisionAvoidanceFactor(collisionAvoidancePercent * 0.01d);
127 return this;
128 }
129
130 /**
131 * Sets the collision avoidance factor
132 *
133 * @param collisionAvoidanceFactor the factor
134 * @return the builder
135 */
136 public RedeliveryPolicyDefinition collisionAvoidanceFactor(double collisionAvoidanceFactor) {
137 setCollisionAvoidanceFactor(collisionAvoidanceFactor);
138 return this;
139 }
140
141 /**
142 * Sets the fixed delay between redeliveries
143 *
144 * @param delay delay in millis
145 * @return the builder
146 */
147 public RedeliveryPolicyDefinition redeliveryDelay(long delay) {
148 setRedeliveryDelay(delay);
149 return this;
150 }
151
152 /**
153 * Sets the logging level to use when retries has exhausted
154 *
155 * @param retriesExhaustedLogLevel the logging level
156 * @return the builder
157 */
158 public RedeliveryPolicyDefinition retriesExhaustedLogLevel(LoggingLevel retriesExhaustedLogLevel) {
159 setRetriesExhaustedLogLevel(retriesExhaustedLogLevel);
160 return this;
161 }
162
163 /**
164 * Sets the logging level to use for logging retry attempts
165 *
166 * @param retryAttemptedLogLevel the logging level
167 * @return the builder
168 */
169 public RedeliveryPolicyDefinition retryAttemptedLogLevel(LoggingLevel retryAttemptedLogLevel) {
170 setRetryAttemptedLogLevel(retryAttemptedLogLevel);
171 return this;
172 }
173
174 /**
175 * Sets wheter stack traces should be logged, can be used to reduce verbose.
176 *
177 * @param logStackTrace wheter stack traces should be logged or not
178 * @return the builder
179 */
180 public RedeliveryPolicyDefinition logStackTrace(boolean logStackTrace) {
181 setLogStackTrace(logStackTrace);
182 return this;
183 }
184
185
186
187 /**
188 * Sets the maximum redeliveries
189 * <ul>
190 * <li>5 = default value</li>
191 * <li>0 = no redeliveries</li>
192 * <li>-1 = redeliver forever</li>
193 * </ul>
194 *
195 * @param maximumRedeliveries the value
196 * @return the builder
197 */
198 public RedeliveryPolicyDefinition maximumRedeliveries(int maximumRedeliveries) {
199 setMaximumRedeliveries(maximumRedeliveries);
200 return this;
201 }
202
203 /**
204 * Turn on collision avoidance.
205 *
206 * @return the builder
207 */
208 public RedeliveryPolicyDefinition useCollisionAvoidance() {
209 setUseCollisionAvoidance(Boolean.TRUE);
210 return this;
211 }
212
213 /**
214 * Turn on exponential backk off
215 *
216 * @return the builder
217 */
218 public RedeliveryPolicyDefinition useExponentialBackOff() {
219 setUseExponentialBackOff(Boolean.TRUE);
220 return this;
221 }
222
223 /**
224 * Sets the maximum delay between redelivery
225 *
226 * @param maximumRedeliveryDelay the delay in millis
227 * @return the builder
228 */
229 public RedeliveryPolicyDefinition maximumRedeliveryDelay(long maximumRedeliveryDelay) {
230 setMaximumRedeliveryDelay(maximumRedeliveryDelay);
231 return this;
232 }
233
234 /**
235 * Use redelivery policy looked up in the registry
236 *
237 * @param ref reference to the redelivery policy to lookup and use
238 * @return the builder
239 */
240 public RedeliveryPolicyDefinition ref(String ref) {
241 setRef(ref);
242 return this;
243 }
244
245 // Properties
246 //-------------------------------------------------------------------------
247
248 public Double getBackOffMultiplier() {
249 return backOffMultiplier;
250 }
251
252 public void setBackOffMultiplier(Double backOffMultiplier) {
253 this.backOffMultiplier = backOffMultiplier;
254 }
255
256 public Double getCollisionAvoidanceFactor() {
257 return collisionAvoidanceFactor;
258 }
259
260 public void setCollisionAvoidanceFactor(Double collisionAvoidanceFactor) {
261 this.collisionAvoidanceFactor = collisionAvoidanceFactor;
262 }
263
264 public Long getRedeliveryDelay() {
265 return redeliveryDelay;
266 }
267
268 public void setRedeliveryDelay(Long delay) {
269 this.redeliveryDelay = delay;
270 }
271
272 public Integer getMaximumRedeliveries() {
273 return maximumRedeliveries;
274 }
275
276 public void setMaximumRedeliveries(Integer maximumRedeliveries) {
277 this.maximumRedeliveries = maximumRedeliveries;
278 }
279
280 public Boolean getUseCollisionAvoidance() {
281 return useCollisionAvoidance;
282 }
283
284 public void setUseCollisionAvoidance(Boolean useCollisionAvoidance) {
285 this.useCollisionAvoidance = useCollisionAvoidance;
286 }
287
288 public Boolean getUseExponentialBackOff() {
289 return useExponentialBackOff;
290 }
291
292 public void setUseExponentialBackOff(Boolean useExponentialBackOff) {
293 this.useExponentialBackOff = useExponentialBackOff;
294 }
295
296 public Long getMaximumRedeliveryDelay() {
297 return maximumRedeliveryDelay;
298 }
299
300 public void setMaximumRedeliveryDelay(Long maximumRedeliveryDelay) {
301 this.maximumRedeliveryDelay = maximumRedeliveryDelay;
302 }
303
304 public void setRetriesExhaustedLogLevel(LoggingLevel retriesExhaustedLogLevel) {
305 this.retriesExhaustedLogLevel = retriesExhaustedLogLevel;
306 }
307
308 public LoggingLevel getRetriesExhaustedLogLevel() {
309 return retriesExhaustedLogLevel;
310 }
311
312 public void setRetryAttemptedLogLevel(LoggingLevel retryAttemptedLogLevel) {
313 this.retryAttemptedLogLevel = retryAttemptedLogLevel;
314 }
315
316 public LoggingLevel getRetryAttemptedLogLevel() {
317 return retryAttemptedLogLevel;
318 }
319
320 public String getRef() {
321 return ref;
322 }
323
324 public void setRef(String ref) {
325 this.ref = ref;
326 }
327
328 public Boolean getLogStackTrace() {
329 return logStackTrace;
330 }
331
332 public void setLogStackTrace(Boolean logStackTrace) {
333 this.logStackTrace = logStackTrace;
334 }
335 }