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 */
017package org.apache.camel.model;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023
024import org.apache.camel.LoggingLevel;
025import org.apache.camel.spi.Metadata;
026
027/**
028 * To configure re-delivery for error handling
029 */
030@Metadata(label = "configuration")
031@XmlRootElement(name = "redeliveryPolicy")
032@XmlAccessorType(XmlAccessType.FIELD)
033public class RedeliveryPolicyDefinition {
034    @XmlAttribute
035    private String maximumRedeliveries;
036    @XmlAttribute
037    private String redeliveryDelay;
038    @XmlAttribute
039    private String asyncDelayedRedelivery;
040    @XmlAttribute
041    private String backOffMultiplier;
042    @XmlAttribute
043    private String useExponentialBackOff;
044    @XmlAttribute
045    private String collisionAvoidanceFactor;
046    @XmlAttribute
047    private String useCollisionAvoidance;
048    @XmlAttribute
049    private String maximumRedeliveryDelay;
050    @XmlAttribute
051    private LoggingLevel retriesExhaustedLogLevel;
052    @XmlAttribute
053    private LoggingLevel retryAttemptedLogLevel;
054    @XmlAttribute
055    private String retryAttemptedLogInterval;
056    @XmlAttribute
057    private String logRetryAttempted;
058    @XmlAttribute
059    private String logStackTrace;
060    @XmlAttribute
061    private String logRetryStackTrace;
062    @XmlAttribute
063    private String logHandled;
064    @XmlAttribute
065    private String logNewException;
066    @XmlAttribute
067    private String logContinued;
068    @XmlAttribute
069    private String logExhausted;
070    @XmlAttribute
071    private String logExhaustedMessageHistory;
072    @XmlAttribute
073    private String logExhaustedMessageBody;
074    @XmlAttribute
075    private String disableRedelivery;
076    @XmlAttribute
077    private String delayPattern;
078    @XmlAttribute
079    private String allowRedeliveryWhileStopping;
080    @XmlAttribute
081    private String exchangeFormatterRef;
082
083    @Override
084    public String toString() {
085        return "RedeliveryPolicy[maximumRedeliveries: " + maximumRedeliveries + "]";
086    }
087
088    // Fluent API
089    // -------------------------------------------------------------------------
090
091    /**
092     * Allow synchronous delayed redelivery. The route, in particular the
093     * consumer's component, must support the Asynchronous Routing Engine (e.g.
094     * seda).
095     *
096     * @return the builder
097     */
098    public RedeliveryPolicyDefinition asyncDelayedRedelivery() {
099        setAsyncDelayedRedelivery("true");
100        return this;
101    }
102
103    /**
104     * Controls whether to allow redelivery while stopping/shutting down a route
105     * that uses error handling.
106     *
107     * @param allowRedeliveryWhileStopping <tt>true</tt> to allow redelivery,
108     *            <tt>false</tt> to reject redeliveries
109     * @return the builder
110     */
111    public RedeliveryPolicyDefinition allowRedeliveryWhileStopping(boolean allowRedeliveryWhileStopping) {
112        return allowRedeliveryWhileStopping(Boolean.toString(allowRedeliveryWhileStopping));
113    }
114
115    /**
116     * Controls whether to allow redelivery while stopping/shutting down a route
117     * that uses error handling.
118     *
119     * @param allowRedeliveryWhileStopping <tt>true</tt> to allow redelivery,
120     *            <tt>false</tt> to reject redeliveries
121     * @return the builder
122     */
123    public RedeliveryPolicyDefinition allowRedeliveryWhileStopping(String allowRedeliveryWhileStopping) {
124        setAllowRedeliveryWhileStopping(allowRedeliveryWhileStopping);
125        return this;
126    }
127
128    /**
129     * Sets the back off multiplier
130     *
131     * @param backOffMultiplier the back off multiplier
132     * @return the builder
133     */
134    public RedeliveryPolicyDefinition backOffMultiplier(double backOffMultiplier) {
135        return backOffMultiplier(Double.toString(backOffMultiplier));
136    }
137
138    /**
139     * Sets the back off multiplier (supports property placeholders)
140     *
141     * @param backOffMultiplier the back off multiplier
142     * @return the builder
143     */
144    public RedeliveryPolicyDefinition backOffMultiplier(String backOffMultiplier) {
145        setBackOffMultiplier(backOffMultiplier);
146        return this;
147    }
148
149    /**
150     * Sets the collision avoidance percentage
151     *
152     * @param collisionAvoidancePercent the percentage
153     * @return the builder
154     */
155    public RedeliveryPolicyDefinition collisionAvoidancePercent(double collisionAvoidancePercent) {
156        setCollisionAvoidanceFactor(Double.toString(collisionAvoidancePercent * 0.01d));
157        return this;
158    }
159
160    /**
161     * Sets the collision avoidance factor
162     *
163     * @param collisionAvoidanceFactor the factor
164     * @return the builder
165     */
166    public RedeliveryPolicyDefinition collisionAvoidanceFactor(double collisionAvoidanceFactor) {
167        return collisionAvoidanceFactor(Double.toString(collisionAvoidanceFactor));
168    }
169
170    /**
171     * Sets the collision avoidance factor (supports property placeholders)
172     *
173     * @param collisionAvoidanceFactor the factor
174     * @return the builder
175     */
176    public RedeliveryPolicyDefinition collisionAvoidanceFactor(String collisionAvoidanceFactor) {
177        setCollisionAvoidanceFactor(collisionAvoidanceFactor);
178        return this;
179    }
180
181    /**
182     * Sets the initial redelivery delay
183     *
184     * @param delay delay in millis
185     * @return the builder
186     */
187    public RedeliveryPolicyDefinition redeliveryDelay(long delay) {
188        return redeliveryDelay(Long.toString(delay));
189    }
190
191    /**
192     * Sets the initial redelivery delay (supports property placeholders)
193     *
194     * @param delay delay in millis
195     * @return the builder
196     */
197    public RedeliveryPolicyDefinition redeliveryDelay(String delay) {
198        setRedeliveryDelay(delay);
199        return this;
200    }
201
202    /**
203     * Sets the logging level to use when retries has exhausted
204     *
205     * @param retriesExhaustedLogLevel the logging level
206     * @return the builder
207     */
208    public RedeliveryPolicyDefinition retriesExhaustedLogLevel(LoggingLevel retriesExhaustedLogLevel) {
209        setRetriesExhaustedLogLevel(retriesExhaustedLogLevel);
210        return this;
211    }
212
213    /**
214     * Sets the logging level to use for logging retry attempts
215     *
216     * @param retryAttemptedLogLevel the logging level
217     * @return the builder
218     */
219    public RedeliveryPolicyDefinition retryAttemptedLogLevel(LoggingLevel retryAttemptedLogLevel) {
220        setRetryAttemptedLogLevel(retryAttemptedLogLevel);
221        return this;
222    }
223
224    /**
225     * Sets the interval to use for logging retry attempts
226     *
227     * @param retryAttemptedLogInterval the retry logging interval
228     * @return the builder
229     */
230    public RedeliveryPolicyDefinition retryAttemptedLogInterval(String retryAttemptedLogInterval) {
231        setRetryAttemptedLogInterval(retryAttemptedLogInterval);
232        return this;
233    }
234
235    /**
236     * Sets whether stack traces should be logged. Can be used to include or
237     * reduce verbose.
238     *
239     * @param logStackTrace whether stack traces should be logged or not
240     * @return the builder
241     */
242    public RedeliveryPolicyDefinition logStackTrace(boolean logStackTrace) {
243        return logStackTrace(Boolean.toString(logStackTrace));
244    }
245
246    /**
247     * Sets whether stack traces should be logged (supports property
248     * placeholders) Can be used to include or reduce verbose.
249     *
250     * @param logStackTrace whether stack traces should be logged or not
251     * @return the builder
252     */
253    public RedeliveryPolicyDefinition logStackTrace(String logStackTrace) {
254        setLogStackTrace(logStackTrace);
255        return this;
256    }
257
258    /**
259     * Sets whether stack traces should be logged when an retry attempt failed.
260     * Can be used to include or reduce verbose.
261     *
262     * @param logRetryStackTrace whether stack traces should be logged or not
263     * @return the builder
264     */
265    public RedeliveryPolicyDefinition logRetryStackTrace(boolean logRetryStackTrace) {
266        return logRetryStackTrace(Boolean.toString(logRetryStackTrace));
267    }
268
269    /**
270     * Sets whether stack traces should be logged when an retry attempt failed
271     * (supports property placeholders). Can be used to include or reduce
272     * verbose.
273     *
274     * @param logRetryStackTrace whether stack traces should be logged or not
275     * @return the builder
276     */
277    public RedeliveryPolicyDefinition logRetryStackTrace(String logRetryStackTrace) {
278        setLogRetryStackTrace(logRetryStackTrace);
279        return this;
280    }
281
282    /**
283     * Sets whether retry attempts should be logged or not. Can be used to
284     * include or reduce verbose.
285     *
286     * @param logRetryAttempted whether retry attempts should be logged or not
287     * @return the builder
288     */
289    public RedeliveryPolicyDefinition logRetryAttempted(boolean logRetryAttempted) {
290        return logRetryAttempted(Boolean.toString(logRetryAttempted));
291    }
292
293    /**
294     * Sets whether retry attempts should be logged or not (supports property
295     * placeholders). Can be used to include or reduce verbose.
296     *
297     * @param logRetryAttempted whether retry attempts should be logged or not
298     * @return the builder
299     */
300    public RedeliveryPolicyDefinition logRetryAttempted(String logRetryAttempted) {
301        setLogRetryAttempted(logRetryAttempted);
302        return this;
303    }
304
305    /**
306     * Sets whether handled exceptions should be logged or not. Can be used to
307     * include or reduce verbose.
308     *
309     * @param logHandled whether handled exceptions should be logged or not
310     * @return the builder
311     */
312    public RedeliveryPolicyDefinition logHandled(boolean logHandled) {
313        return logHandled(Boolean.toString(logHandled));
314    }
315
316    /**
317     * Sets whether handled exceptions should be logged or not (supports
318     * property placeholders). Can be used to include or reduce verbose.
319     *
320     * @param logHandled whether handled exceptions should be logged or not
321     * @return the builder
322     */
323    public RedeliveryPolicyDefinition logHandled(String logHandled) {
324        setLogHandled(logHandled);
325        return this;
326    }
327
328    /**
329     * Sets whether new exceptions should be logged or not. Can be used to
330     * include or reduce verbose.
331     * <p/>
332     * A new exception is an exception that was thrown while handling a previous
333     * exception.
334     *
335     * @param logNewException whether new exceptions should be logged or not
336     * @return the builder
337     */
338    public RedeliveryPolicyDefinition logNewException(boolean logNewException) {
339        return logNewException(Boolean.toString(logNewException));
340    }
341
342    /**
343     * Sets whether new exceptions should be logged or not (supports property
344     * placeholders). Can be used to include or reduce verbose.
345     * <p/>
346     * A new exception is an exception that was thrown while handling a previous
347     * exception.
348     *
349     * @param logNewException whether new exceptions should be logged or not
350     * @return the builder
351     */
352    public RedeliveryPolicyDefinition logNewException(String logNewException) {
353        setLogNewException(logNewException);
354        return this;
355    }
356
357    /**
358     * Sets whether continued exceptions should be logged or not. Can be used to
359     * include or reduce verbose.
360     *
361     * @param logContinued whether continued exceptions should be logged or not
362     * @return the builder
363     */
364    public RedeliveryPolicyDefinition logContinued(boolean logContinued) {
365        return logContinued(Boolean.toString(logContinued));
366    }
367
368    /**
369     * Sets whether continued exceptions should be logged or not (supports
370     * property placeholders). Can be used to include or reduce verbose.
371     *
372     * @param logContinued whether continued exceptions should be logged or not
373     * @return the builder
374     */
375    public RedeliveryPolicyDefinition logContinued(String logContinued) {
376        setLogContinued(logContinued);
377        return this;
378    }
379
380    /**
381     * Sets whether exhausted exceptions should be logged or not. Can be used to
382     * include or reduce verbose.
383     *
384     * @param logExhausted whether exhausted exceptions should be logged or not
385     * @return the builder
386     */
387    public RedeliveryPolicyDefinition logExhausted(boolean logExhausted) {
388        return logExhausted(Boolean.toString(logExhausted));
389    }
390
391    /**
392     * Sets whether exhausted exceptions should be logged or not (supports
393     * property placeholders). Can be used to include or reduce verbose.
394     *
395     * @param logExhausted whether exhausted exceptions should be logged or not
396     * @return the builder
397     */
398    public RedeliveryPolicyDefinition logExhausted(String logExhausted) {
399        setLogExhausted(logExhausted);
400        return this;
401    }
402
403    /**
404     * Sets whether exhausted exceptions should be logged including message
405     * history or not (supports property placeholders). Can be used to include
406     * or reduce verbose.
407     *
408     * @param logExhaustedMessageHistory whether exhausted exceptions should be
409     *            logged with message history
410     * @return the builder
411     */
412    public RedeliveryPolicyDefinition logExhaustedMessageHistory(boolean logExhaustedMessageHistory) {
413        setLogExhaustedMessageHistory(Boolean.toString(logExhaustedMessageHistory));
414        return this;
415    }
416
417    /**
418     * Sets whether exhausted exceptions should be logged including message
419     * history or not (supports property placeholders). Can be used to include
420     * or reduce verbose.
421     *
422     * @param logExhaustedMessageHistory whether exhausted exceptions should be
423     *            logged with message history
424     * @return the builder
425     */
426    public RedeliveryPolicyDefinition logExhaustedMessageHistory(String logExhaustedMessageHistory) {
427        setLogExhaustedMessageHistory(logExhaustedMessageHistory);
428        return this;
429    }
430
431    /**
432     * Sets whether exhausted message body should be logged including message
433     * history or not (supports property placeholders). Can be used to include
434     * or reduce verbose. Requires <tt>logExhaustedMessageHistory</tt> to be
435     * enabled.
436     *
437     * @param logExhaustedMessageBody whether exhausted message body should be
438     *            logged with message history
439     * @return the builder
440     */
441    public RedeliveryPolicyDefinition logExhaustedMessageBody(boolean logExhaustedMessageBody) {
442        setLogExhaustedMessageBody(Boolean.toString(logExhaustedMessageBody));
443        return this;
444    }
445
446    /**
447     * Sets whether exhausted message body should be logged including message
448     * history or not (supports property placeholders). Can be used to include
449     * or reduce verbose. Requires <tt>logExhaustedMessageHistory</tt> to be
450     * enabled.
451     *
452     * @param logExhaustedMessageBody whether exhausted message body should be
453     *            logged with message history
454     * @return the builder
455     */
456    public RedeliveryPolicyDefinition logExhaustedMessageBody(String logExhaustedMessageBody) {
457        setLogExhaustedMessageBody(logExhaustedMessageBody);
458        return this;
459    }
460
461    /**
462     * Sets the maximum redeliveries
463     * <ul>
464     * <li>x = redeliver at most x times</li>
465     * <li>0 = no redeliveries</li>
466     * <li>-1 = redeliver forever</li>
467     * </ul>
468     *
469     * @param maximumRedeliveries the value
470     * @return the builder
471     */
472    public RedeliveryPolicyDefinition maximumRedeliveries(int maximumRedeliveries) {
473        return maximumRedeliveries(Integer.toString(maximumRedeliveries));
474    }
475
476    /**
477     * Sets the maximum redeliveries (supports property placeholders)
478     * <ul>
479     * <li>x = redeliver at most x times</li>
480     * <li>0 = no redeliveries</li>
481     * <li>-1 = redeliver forever</li>
482     * </ul>
483     *
484     * @param maximumRedeliveries the value
485     * @return the builder
486     */
487    public RedeliveryPolicyDefinition maximumRedeliveries(String maximumRedeliveries) {
488        setMaximumRedeliveries(maximumRedeliveries);
489        return this;
490    }
491
492    /**
493     * Turn on collision avoidance.
494     *
495     * @return the builder
496     */
497    public RedeliveryPolicyDefinition useCollisionAvoidance() {
498        setUseCollisionAvoidance("true");
499        return this;
500    }
501
502    /**
503     * Turn on exponential backk off
504     *
505     * @return the builder
506     */
507    public RedeliveryPolicyDefinition useExponentialBackOff() {
508        setUseExponentialBackOff("true");
509        return this;
510    }
511
512    /**
513     * Sets the maximum delay between redelivery
514     *
515     * @param maximumRedeliveryDelay the delay in millis
516     * @return the builder
517     */
518    public RedeliveryPolicyDefinition maximumRedeliveryDelay(long maximumRedeliveryDelay) {
519        return maximumRedeliveryDelay(Long.toString(maximumRedeliveryDelay));
520    }
521
522    /**
523     * Sets the maximum delay between redelivery (supports property
524     * placeholders)
525     *
526     * @param maximumRedeliveryDelay the delay in millis
527     * @return the builder
528     */
529    public RedeliveryPolicyDefinition maximumRedeliveryDelay(String maximumRedeliveryDelay) {
530        setMaximumRedeliveryDelay(maximumRedeliveryDelay);
531        return this;
532    }
533
534    /**
535     * Sets the delay pattern with delay intervals.
536     *
537     * @param delayPattern the delay pattern
538     * @return the builder
539     */
540    public RedeliveryPolicyDefinition delayPattern(String delayPattern) {
541        setDelayPattern(delayPattern);
542        return this;
543    }
544
545    /**
546     * Sets the reference of the instance of
547     * {@link org.apache.camel.spi.ExchangeFormatter} to generate the log
548     * message from exchange.
549     *
550     * @param exchangeFormatterRef name of the instance of
551     *            {@link org.apache.camel.spi.ExchangeFormatter}
552     * @return the builder
553     */
554    public RedeliveryPolicyDefinition exchangeFormatterRef(String exchangeFormatterRef) {
555        setExchangeFormatterRef(exchangeFormatterRef);
556        return this;
557    }
558
559    // Properties
560    // -------------------------------------------------------------------------
561
562    public String getMaximumRedeliveries() {
563        return maximumRedeliveries;
564    }
565
566    public void setMaximumRedeliveries(String maximumRedeliveries) {
567        this.maximumRedeliveries = maximumRedeliveries;
568    }
569
570    public String getRedeliveryDelay() {
571        return redeliveryDelay;
572    }
573
574    public void setRedeliveryDelay(String redeliveryDelay) {
575        this.redeliveryDelay = redeliveryDelay;
576    }
577
578    public String getAsyncDelayedRedelivery() {
579        return asyncDelayedRedelivery;
580    }
581
582    public void setAsyncDelayedRedelivery(String asyncDelayedRedelivery) {
583        this.asyncDelayedRedelivery = asyncDelayedRedelivery;
584    }
585
586    public String getBackOffMultiplier() {
587        return backOffMultiplier;
588    }
589
590    public void setBackOffMultiplier(String backOffMultiplier) {
591        this.backOffMultiplier = backOffMultiplier;
592    }
593
594    public String getUseExponentialBackOff() {
595        return useExponentialBackOff;
596    }
597
598    public void setUseExponentialBackOff(String useExponentialBackOff) {
599        this.useExponentialBackOff = useExponentialBackOff;
600    }
601
602    public String getCollisionAvoidanceFactor() {
603        return collisionAvoidanceFactor;
604    }
605
606    public void setCollisionAvoidanceFactor(String collisionAvoidanceFactor) {
607        this.collisionAvoidanceFactor = collisionAvoidanceFactor;
608    }
609
610    public String getUseCollisionAvoidance() {
611        return useCollisionAvoidance;
612    }
613
614    public void setUseCollisionAvoidance(String useCollisionAvoidance) {
615        this.useCollisionAvoidance = useCollisionAvoidance;
616    }
617
618    public String getMaximumRedeliveryDelay() {
619        return maximumRedeliveryDelay;
620    }
621
622    public void setMaximumRedeliveryDelay(String maximumRedeliveryDelay) {
623        this.maximumRedeliveryDelay = maximumRedeliveryDelay;
624    }
625
626    public LoggingLevel getRetriesExhaustedLogLevel() {
627        return retriesExhaustedLogLevel;
628    }
629
630    public void setRetriesExhaustedLogLevel(LoggingLevel retriesExhaustedLogLevel) {
631        this.retriesExhaustedLogLevel = retriesExhaustedLogLevel;
632    }
633
634    public LoggingLevel getRetryAttemptedLogLevel() {
635        return retryAttemptedLogLevel;
636    }
637
638    public void setRetryAttemptedLogLevel(LoggingLevel retryAttemptedLogLevel) {
639        this.retryAttemptedLogLevel = retryAttemptedLogLevel;
640    }
641
642    public String getRetryAttemptedLogInterval() {
643        return retryAttemptedLogInterval;
644    }
645
646    public void setRetryAttemptedLogInterval(String retryAttemptedLogInterval) {
647        this.retryAttemptedLogInterval = retryAttemptedLogInterval;
648    }
649
650    public String getLogRetryAttempted() {
651        return logRetryAttempted;
652    }
653
654    public void setLogRetryAttempted(String logRetryAttempted) {
655        this.logRetryAttempted = logRetryAttempted;
656    }
657
658    public String getLogStackTrace() {
659        return logStackTrace;
660    }
661
662    public void setLogStackTrace(String logStackTrace) {
663        this.logStackTrace = logStackTrace;
664    }
665
666    public String getLogRetryStackTrace() {
667        return logRetryStackTrace;
668    }
669
670    public void setLogRetryStackTrace(String logRetryStackTrace) {
671        this.logRetryStackTrace = logRetryStackTrace;
672    }
673
674    public String getLogHandled() {
675        return logHandled;
676    }
677
678    public void setLogHandled(String logHandled) {
679        this.logHandled = logHandled;
680    }
681
682    public String getLogNewException() {
683        return logNewException;
684    }
685
686    public void setLogNewException(String logNewException) {
687        this.logNewException = logNewException;
688    }
689
690    public String getLogContinued() {
691        return logContinued;
692    }
693
694    public void setLogContinued(String logContinued) {
695        this.logContinued = logContinued;
696    }
697
698    public String getLogExhausted() {
699        return logExhausted;
700    }
701
702    public void setLogExhausted(String logExhausted) {
703        this.logExhausted = logExhausted;
704    }
705
706    public String getLogExhaustedMessageHistory() {
707        return logExhaustedMessageHistory;
708    }
709
710    public void setLogExhaustedMessageHistory(String logExhaustedMessageHistory) {
711        this.logExhaustedMessageHistory = logExhaustedMessageHistory;
712    }
713
714    public String getLogExhaustedMessageBody() {
715        return logExhaustedMessageBody;
716    }
717
718    public void setLogExhaustedMessageBody(String logExhaustedMessageBody) {
719        this.logExhaustedMessageBody = logExhaustedMessageBody;
720    }
721
722    public String getDisableRedelivery() {
723        return disableRedelivery;
724    }
725
726    /**
727     * Disables redelivery (same as setting maximum redeliveries to 0)
728     */
729    public void setDisableRedelivery(String disableRedelivery) {
730        this.disableRedelivery = disableRedelivery;
731    }
732
733    public String getDelayPattern() {
734        return delayPattern;
735    }
736
737    public void setDelayPattern(String delayPattern) {
738        this.delayPattern = delayPattern;
739    }
740
741    public String getAllowRedeliveryWhileStopping() {
742        return allowRedeliveryWhileStopping;
743    }
744
745    public void setAllowRedeliveryWhileStopping(String allowRedeliveryWhileStopping) {
746        this.allowRedeliveryWhileStopping = allowRedeliveryWhileStopping;
747    }
748
749    public String getExchangeFormatterRef() {
750        return exchangeFormatterRef;
751    }
752
753    public void setExchangeFormatterRef(String exchangeFormatterRef) {
754        this.exchangeFormatterRef = exchangeFormatterRef;
755    }
756
757}