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 import org.apache.camel.util.ObjectHelper;
029
030 /**
031 * Represents an XML <redeliveryPolicy/> element
032 *
033 * @version
034 */
035 @XmlRootElement(name = "redeliveryPolicy")
036 @XmlAccessorType(XmlAccessType.FIELD)
037 public class RedeliveryPolicyDefinition {
038 @XmlAttribute
039 private String maximumRedeliveries;
040 @XmlAttribute
041 private String redeliveryDelay;
042 @XmlAttribute
043 private String asyncDelayedRedelivery;
044 @XmlAttribute
045 private String backOffMultiplier;
046 @XmlAttribute
047 private String useExponentialBackOff;
048 @XmlAttribute
049 private String collisionAvoidanceFactor;
050 @XmlAttribute
051 private String useCollisionAvoidance;
052 @XmlAttribute
053 private String maximumRedeliveryDelay;
054 @XmlAttribute
055 private LoggingLevel retriesExhaustedLogLevel;
056 @XmlAttribute
057 private LoggingLevel retryAttemptedLogLevel;
058 @XmlAttribute
059 private String logRetryAttempted;
060 @XmlAttribute
061 private String logStackTrace;
062 @XmlAttribute
063 private String logRetryStackTrace;
064 @XmlAttribute
065 private String logHandled;
066 @XmlAttribute
067 private String logContinued;
068 @XmlAttribute
069 private String logExhausted;
070 @XmlAttribute
071 private String disableRedelivery;
072 @XmlAttribute
073 private String delayPattern;
074
075 public RedeliveryPolicy createRedeliveryPolicy(CamelContext context, RedeliveryPolicy parentPolicy) {
076
077 RedeliveryPolicy answer;
078 if (parentPolicy != null) {
079 answer = parentPolicy.copy();
080 } else {
081 answer = new RedeliveryPolicy();
082 }
083
084 try {
085
086 // copy across the properties - if they are set
087 if (maximumRedeliveries != null) {
088 answer.setMaximumRedeliveries(CamelContextHelper.parseInteger(context, maximumRedeliveries));
089 }
090 if (redeliveryDelay != null) {
091 answer.setRedeliveryDelay(CamelContextHelper.parseLong(context, redeliveryDelay));
092 }
093 if (asyncDelayedRedelivery != null) {
094 if (CamelContextHelper.parseBoolean(context, asyncDelayedRedelivery)) {
095 answer.asyncDelayedRedelivery();
096 }
097 }
098 if (retriesExhaustedLogLevel != null) {
099 answer.setRetriesExhaustedLogLevel(retriesExhaustedLogLevel);
100 }
101 if (retryAttemptedLogLevel != null) {
102 answer.setRetryAttemptedLogLevel(retryAttemptedLogLevel);
103 }
104 if (backOffMultiplier != null) {
105 answer.setBackOffMultiplier(CamelContextHelper.parseDouble(context, backOffMultiplier));
106 }
107 if (useExponentialBackOff != null) {
108 answer.setUseExponentialBackOff(CamelContextHelper.parseBoolean(context, useExponentialBackOff));
109 }
110 if (collisionAvoidanceFactor != null) {
111 answer.setCollisionAvoidanceFactor(CamelContextHelper.parseDouble(context, collisionAvoidanceFactor));
112 }
113 if (useCollisionAvoidance != null) {
114 answer.setUseCollisionAvoidance(CamelContextHelper.parseBoolean(context, useCollisionAvoidance));
115 }
116 if (maximumRedeliveryDelay != null) {
117 answer.setMaximumRedeliveryDelay(CamelContextHelper.parseLong(context, maximumRedeliveryDelay));
118 }
119 if (logStackTrace != null) {
120 answer.setLogStackTrace(CamelContextHelper.parseBoolean(context, logStackTrace));
121 }
122 if (logRetryStackTrace != null) {
123 answer.setLogRetryStackTrace(CamelContextHelper.parseBoolean(context, logRetryStackTrace));
124 }
125 if (logHandled != null) {
126 answer.setLogHandled(CamelContextHelper.parseBoolean(context, logHandled));
127 }
128 if (logContinued != null) {
129 answer.setLogContinued(CamelContextHelper.parseBoolean(context, logContinued));
130 }
131 if (logRetryAttempted != null) {
132 answer.setLogRetryAttempted(CamelContextHelper.parseBoolean(context, logRetryAttempted));
133 }
134 if (logExhausted != null) {
135 answer.setLogExhausted(CamelContextHelper.parseBoolean(context, logExhausted));
136 }
137 if (disableRedelivery != null) {
138 if (CamelContextHelper.parseBoolean(context, disableRedelivery)) {
139 answer.setMaximumRedeliveries(0);
140 }
141 }
142 if (delayPattern != null) {
143 answer.setDelayPattern(delayPattern);
144 }
145 } catch (Exception e) {
146 throw ObjectHelper.wrapRuntimeCamelException(e);
147 }
148
149 return answer;
150 }
151
152 @Override
153 public String toString() {
154 return "RedeliveryPolicy[maximumRedeliveries: " + maximumRedeliveries + "]";
155 }
156
157 // Fluent API
158 //-------------------------------------------------------------------------
159
160 /**
161 * Allow synchronous delayed redelivery.
162 */
163 public RedeliveryPolicyDefinition asyncDelayedRedelivery() {
164 setAsyncDelayedRedelivery("true");
165 return this;
166 }
167
168 /**
169 * Sets the back off multiplier
170 *
171 * @param backOffMultiplier the back off multiplier
172 * @return the builder
173 */
174 public RedeliveryPolicyDefinition backOffMultiplier(double backOffMultiplier) {
175 return backOffMultiplier(Double.toString(backOffMultiplier));
176 }
177
178 /**
179 * Sets the back off multiplier (supports property placeholders)
180 *
181 * @param backOffMultiplier the back off multiplier
182 * @return the builder
183 */
184 public RedeliveryPolicyDefinition backOffMultiplier(String backOffMultiplier) {
185 setBackOffMultiplier(backOffMultiplier);
186 return this;
187 }
188
189 /**
190 * Sets the collision avoidance percentage
191 *
192 * @param collisionAvoidancePercent the percentage
193 * @return the builder
194 */
195 public RedeliveryPolicyDefinition collisionAvoidancePercent(double collisionAvoidancePercent) {
196 setCollisionAvoidanceFactor(Double.toString(collisionAvoidancePercent * 0.01d));
197 return this;
198 }
199
200 /**
201 * Sets the collision avoidance factor
202 *
203 * @param collisionAvoidanceFactor the factor
204 * @return the builder
205 */
206 public RedeliveryPolicyDefinition collisionAvoidanceFactor(double collisionAvoidanceFactor) {
207 return collisionAvoidanceFactor(Double.toString(collisionAvoidanceFactor));
208 }
209
210 /**
211 * Sets the collision avoidance factor (supports property placeholders)
212 *
213 * @param collisionAvoidanceFactor the factor
214 * @return the builder
215 */
216 public RedeliveryPolicyDefinition collisionAvoidanceFactor(String collisionAvoidanceFactor) {
217 setCollisionAvoidanceFactor(collisionAvoidanceFactor);
218 return this;
219 }
220
221 /**
222 * Sets the initial redelivery delay
223 *
224 * @param delay delay in millis
225 * @return the builder
226 */
227 public RedeliveryPolicyDefinition redeliveryDelay(long delay) {
228 return redeliveryDelay(Long.toString(delay));
229 }
230
231 /**
232 * Sets the initial redelivery delay (supports property placeholders)
233 *
234 * @param delay delay in millis
235 * @return the builder
236 */
237 public RedeliveryPolicyDefinition redeliveryDelay(String delay) {
238 setRedeliveryDelay(delay);
239 return this;
240 }
241
242 /**
243 * Sets the logging level to use when retries has exhausted
244 *
245 * @param retriesExhaustedLogLevel the logging level
246 * @return the builder
247 */
248 public RedeliveryPolicyDefinition retriesExhaustedLogLevel(LoggingLevel retriesExhaustedLogLevel) {
249 setRetriesExhaustedLogLevel(retriesExhaustedLogLevel);
250 return this;
251 }
252
253 /**
254 * Sets the logging level to use for logging retry attempts
255 *
256 * @param retryAttemptedLogLevel the logging level
257 * @return the builder
258 */
259 public RedeliveryPolicyDefinition retryAttemptedLogLevel(LoggingLevel retryAttemptedLogLevel) {
260 setRetryAttemptedLogLevel(retryAttemptedLogLevel);
261 return this;
262 }
263
264 /**
265 * Sets whether stack traces should be logged.
266 * Can be used to include or reduce verbose.
267 *
268 * @param logStackTrace whether stack traces should be logged or not
269 * @return the builder
270 */
271 public RedeliveryPolicyDefinition logStackTrace(boolean logStackTrace) {
272 return logStackTrace(Boolean.toString(logStackTrace));
273 }
274
275 /**
276 * Sets whether stack traces should be logged (supports property placeholders)
277 * Can be used to include or reduce verbose.
278 *
279 * @param logStackTrace whether stack traces should be logged or not
280 * @return the builder
281 */
282 public RedeliveryPolicyDefinition logStackTrace(String logStackTrace) {
283 setLogStackTrace(logStackTrace);
284 return this;
285 }
286
287 /**
288 * Sets whether stack traces should be logged when an retry attempt failed.
289 * Can be used to include or reduce verbose.
290 *
291 * @param logRetryStackTrace whether stack traces should be logged or not
292 * @return the builder
293 */
294 public RedeliveryPolicyDefinition logRetryStackTrace(boolean logRetryStackTrace) {
295 return logRetryStackTrace(Boolean.toString(logRetryStackTrace));
296 }
297
298 /**
299 * Sets whether stack traces should be logged when an retry attempt failed (supports property placeholders).
300 * Can be used to include or reduce verbose.
301 *
302 * @param logRetryStackTrace whether stack traces should be logged or not
303 * @return the builder
304 */
305 public RedeliveryPolicyDefinition logRetryStackTrace(String logRetryStackTrace) {
306 setLogRetryStackTrace(logRetryStackTrace);
307 return this;
308 }
309
310 /**
311 * Sets whether retry attempts should be logged or not.
312 * Can be used to include or reduce verbose.
313 *
314 * @param logRetryAttempted whether retry attempts should be logged or not
315 * @return the builder
316 */
317 public RedeliveryPolicyDefinition logRetryAttempted(boolean logRetryAttempted) {
318 return logRetryAttempted(Boolean.toString(logRetryAttempted));
319 }
320
321 /**
322 * Sets whether retry attempts should be logged or not (supports property placeholders).
323 * Can be used to include or reduce verbose.
324 *
325 * @param logRetryAttempted whether retry attempts should be logged or not
326 * @return the builder
327 */
328 public RedeliveryPolicyDefinition logRetryAttempted(String logRetryAttempted) {
329 setLogRetryAttempted(logRetryAttempted);
330 return this;
331 }
332
333 /**
334 * Sets whether handled exceptions should be logged or not.
335 * Can be used to include or reduce verbose.
336 *
337 * @param logHandled whether handled exceptions should be logged or not
338 * @return the builder
339 */
340 public RedeliveryPolicyDefinition logHandled(boolean logHandled) {
341 return logHandled(Boolean.toString(logHandled));
342 }
343
344 /**
345 * Sets whether handled exceptions should be logged or not (supports property placeholders).
346 * Can be used to include or reduce verbose.
347 *
348 * @param logHandled whether handled exceptions should be logged or not
349 * @return the builder
350 */
351 public RedeliveryPolicyDefinition logHandled(String logHandled) {
352 setLogHandled(logHandled);
353 return this;
354 }
355
356 /**
357 * Sets whether continued exceptions should be logged or not.
358 * Can be used to include or reduce verbose.
359 *
360 * @param logContinued whether continued exceptions should be logged or not
361 * @return the builder
362 */
363 public RedeliveryPolicyDefinition logContinued(boolean logContinued) {
364 return logContinued(Boolean.toString(logContinued));
365 }
366
367 /**
368 * Sets whether continued exceptions should be logged or not (supports property placeholders).
369 * Can be used to include or reduce verbose.
370 *
371 * @param logContinued whether continued exceptions should be logged or not
372 * @return the builder
373 */
374 public RedeliveryPolicyDefinition logContinued(String logContinued) {
375 setLogContinued(logContinued);
376 return this;
377 }
378
379 /**
380 * Sets whether exhausted exceptions should be logged or not.
381 * Can be used to include or reduce verbose.
382 *
383 * @param logExhausted whether exhausted exceptions should be logged or not
384 * @return the builder
385 */
386 public RedeliveryPolicyDefinition logExhausted(boolean logExhausted) {
387 return logExhausted(Boolean.toString(logExhausted));
388 }
389
390 /**
391 * Sets whether exhausted exceptions should be logged or not (supports property placeholders).
392 * Can be used to include or reduce verbose.
393 *
394 * @param logExhausted whether exhausted exceptions should be logged or not
395 * @return the builder
396 */
397 public RedeliveryPolicyDefinition logExhausted(String logExhausted) {
398 setLogExhausted(logExhausted);
399 return this;
400 }
401
402 /**
403 * Sets the maximum redeliveries
404 * <ul>
405 * <li>x = redeliver at most x times</li>
406 * <li>0 = no redeliveries</li>
407 * <li>-1 = redeliver forever</li>
408 * </ul>
409 *
410 * @param maximumRedeliveries the value
411 * @return the builder
412 */
413 public RedeliveryPolicyDefinition maximumRedeliveries(int maximumRedeliveries) {
414 return maximumRedeliveries(Integer.toString(maximumRedeliveries));
415 }
416
417 /**
418 * Sets the maximum redeliveries (supports property placeholders)
419 * <ul>
420 * <li>x = redeliver at most x times</li>
421 * <li>0 = no redeliveries</li>
422 * <li>-1 = redeliver forever</li>
423 * </ul>
424 *
425 * @param maximumRedeliveries the value
426 * @return the builder
427 */
428 public RedeliveryPolicyDefinition maximumRedeliveries(String maximumRedeliveries) {
429 setMaximumRedeliveries(maximumRedeliveries);
430 return this;
431 }
432
433 /**
434 * Turn on collision avoidance.
435 *
436 * @return the builder
437 */
438 public RedeliveryPolicyDefinition useCollisionAvoidance() {
439 setUseCollisionAvoidance("true");
440 return this;
441 }
442
443 /**
444 * Turn on exponential backk off
445 *
446 * @return the builder
447 */
448 public RedeliveryPolicyDefinition useExponentialBackOff() {
449 setUseExponentialBackOff("true");
450 return this;
451 }
452
453 /**
454 * Sets the maximum delay between redelivery
455 *
456 * @param maximumRedeliveryDelay the delay in millis
457 * @return the builder
458 */
459 public RedeliveryPolicyDefinition maximumRedeliveryDelay(long maximumRedeliveryDelay) {
460 return maximumRedeliveryDelay(Long.toString(maximumRedeliveryDelay));
461 }
462
463 /**
464 * Sets the maximum delay between redelivery (supports property placeholders)
465 *
466 * @param maximumRedeliveryDelay the delay in millis
467 * @return the builder
468 */
469 public RedeliveryPolicyDefinition maximumRedeliveryDelay(String maximumRedeliveryDelay) {
470 setMaximumRedeliveryDelay(maximumRedeliveryDelay);
471 return this;
472 }
473
474 /**
475 * Sets the delay pattern with delay intervals.
476 *
477 * @param delayPattern the delay pattern
478 * @return the builder
479 */
480 public RedeliveryPolicyDefinition delayPattern(String delayPattern) {
481 setDelayPattern(delayPattern);
482 return this;
483 }
484
485 // Properties
486 //-------------------------------------------------------------------------
487
488 public String getMaximumRedeliveries() {
489 return maximumRedeliveries;
490 }
491
492 public void setMaximumRedeliveries(String maximumRedeliveries) {
493 this.maximumRedeliveries = maximumRedeliveries;
494 }
495
496 public String getRedeliveryDelay() {
497 return redeliveryDelay;
498 }
499
500 public void setRedeliveryDelay(String redeliveryDelay) {
501 this.redeliveryDelay = redeliveryDelay;
502 }
503
504 public String getAsyncDelayedRedelivery() {
505 return asyncDelayedRedelivery;
506 }
507
508 public boolean isAsyncDelayedRedelivery(CamelContext context) {
509 if (getAsyncDelayedRedelivery() == null) {
510 return false;
511 }
512
513 try {
514 return CamelContextHelper.parseBoolean(context, getAsyncDelayedRedelivery());
515 } catch (Exception e) {
516 throw ObjectHelper.wrapRuntimeCamelException(e);
517 }
518 }
519
520 public void setAsyncDelayedRedelivery(String asyncDelayedRedelivery) {
521 this.asyncDelayedRedelivery = asyncDelayedRedelivery;
522 }
523
524 public String getBackOffMultiplier() {
525 return backOffMultiplier;
526 }
527
528 public void setBackOffMultiplier(String backOffMultiplier) {
529 this.backOffMultiplier = backOffMultiplier;
530 }
531
532 public String getUseExponentialBackOff() {
533 return useExponentialBackOff;
534 }
535
536 public void setUseExponentialBackOff(String useExponentialBackOff) {
537 this.useExponentialBackOff = useExponentialBackOff;
538 }
539
540 public String getCollisionAvoidanceFactor() {
541 return collisionAvoidanceFactor;
542 }
543
544 public void setCollisionAvoidanceFactor(String collisionAvoidanceFactor) {
545 this.collisionAvoidanceFactor = collisionAvoidanceFactor;
546 }
547
548 public String getUseCollisionAvoidance() {
549 return useCollisionAvoidance;
550 }
551
552 public void setUseCollisionAvoidance(String useCollisionAvoidance) {
553 this.useCollisionAvoidance = useCollisionAvoidance;
554 }
555
556 public String getMaximumRedeliveryDelay() {
557 return maximumRedeliveryDelay;
558 }
559
560 public void setMaximumRedeliveryDelay(String maximumRedeliveryDelay) {
561 this.maximumRedeliveryDelay = maximumRedeliveryDelay;
562 }
563
564 public LoggingLevel getRetriesExhaustedLogLevel() {
565 return retriesExhaustedLogLevel;
566 }
567
568 public void setRetriesExhaustedLogLevel(LoggingLevel retriesExhaustedLogLevel) {
569 this.retriesExhaustedLogLevel = retriesExhaustedLogLevel;
570 }
571
572 public LoggingLevel getRetryAttemptedLogLevel() {
573 return retryAttemptedLogLevel;
574 }
575
576 public void setRetryAttemptedLogLevel(LoggingLevel retryAttemptedLogLevel) {
577 this.retryAttemptedLogLevel = retryAttemptedLogLevel;
578 }
579
580 public String getLogRetryAttempted() {
581 return logRetryAttempted;
582 }
583
584 public void setLogRetryAttempted(String logRetryAttempted) {
585 this.logRetryAttempted = logRetryAttempted;
586 }
587
588 public String getLogStackTrace() {
589 return logStackTrace;
590 }
591
592 public void setLogStackTrace(String logStackTrace) {
593 this.logStackTrace = logStackTrace;
594 }
595
596 public String getLogRetryStackTrace() {
597 return logRetryStackTrace;
598 }
599
600 public void setLogRetryStackTrace(String logRetryStackTrace) {
601 this.logRetryStackTrace = logRetryStackTrace;
602 }
603
604 public String getLogHandled() {
605 return logHandled;
606 }
607
608 public void setLogHandled(String logHandled) {
609 this.logHandled = logHandled;
610 }
611
612 public String getLogContinued() {
613 return logContinued;
614 }
615
616 public void setLogContinued(String logContinued) {
617 this.logContinued = logContinued;
618 }
619
620 public String getLogExhausted() {
621 return logExhausted;
622 }
623
624 public void setLogExhausted(String logExhausted) {
625 this.logExhausted = logExhausted;
626 }
627
628 public String getDisableRedelivery() {
629 return disableRedelivery;
630 }
631
632 public void setDisableRedelivery(String disableRedelivery) {
633 this.disableRedelivery = disableRedelivery;
634 }
635
636 public String getDelayPattern() {
637 return delayPattern;
638 }
639
640 public void setDelayPattern(String delayPattern) {
641 this.delayPattern = delayPattern;
642 }
643 }