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: 784513 $
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        @XmlAttribute
060        private Boolean disableRedelivery;
061    
062        public RedeliveryPolicy createRedeliveryPolicy(CamelContext context, RedeliveryPolicy parentPolicy) {
063            if (ref != null) {
064                // lookup in registry if ref provided
065                return CamelContextHelper.mandatoryLookup(context, ref, RedeliveryPolicy.class);
066            }
067    
068            RedeliveryPolicy answer = parentPolicy.copy();
069    
070            // copy across the properties - if they are set
071            if (maximumRedeliveries != null) {
072                answer.setMaximumRedeliveries(maximumRedeliveries);
073            }
074            if (redeliveryDelay != null) {
075                answer.setRedeliverDelay(redeliveryDelay);
076            }
077            if (retriesExhaustedLogLevel != null) {
078                answer.setRetriesExhaustedLogLevel(retriesExhaustedLogLevel);
079            }
080            if (retryAttemptedLogLevel != null) {
081                answer.setRetryAttemptedLogLevel(retryAttemptedLogLevel);
082            }
083            if (backOffMultiplier != null) {
084                answer.setBackOffMultiplier(backOffMultiplier);
085            }
086            if (useExponentialBackOff != null) {
087                answer.setUseExponentialBackOff(useExponentialBackOff);
088            }
089            if (collisionAvoidanceFactor != null) {
090                answer.setCollisionAvoidanceFactor(collisionAvoidanceFactor);
091            }
092            if (useCollisionAvoidance != null) {
093                answer.setUseCollisionAvoidance(useCollisionAvoidance);
094            }
095            if (maximumRedeliveryDelay != null) {
096                answer.setMaximumRedeliveryDelay(maximumRedeliveryDelay);
097            }
098            if (logStackTrace != null) {
099                answer.setLogStackTrace(logStackTrace);
100            }
101            if (disableRedelivery != null && disableRedelivery) {
102                answer.setMaximumRedeliveries(0);
103            }
104    
105            return answer;
106        }
107    
108        public String toString() {
109            return "RedeliveryPolicy[maximumRedeliveries: " + maximumRedeliveries + "]";
110        }
111    
112        // Fluent API
113        //-------------------------------------------------------------------------
114        /**
115         * Sets the back off multiplier
116         *
117         * @param backOffMultiplier  the back off multiplier
118         * @return the builder
119         */
120        public RedeliveryPolicyDefinition backOffMultiplier(double backOffMultiplier) {
121            setBackOffMultiplier(backOffMultiplier);
122            return this;
123        }
124    
125        /**
126         * Sets the collision avoidance percentage
127         *
128         * @param collisionAvoidancePercent  the percentage
129         * @return the builder
130         */
131        public RedeliveryPolicyDefinition collisionAvoidancePercent(double collisionAvoidancePercent) {
132            setCollisionAvoidanceFactor(collisionAvoidancePercent * 0.01d);
133            return this;
134        }
135    
136        /**
137         * Sets the collision avoidance factor
138         *
139         * @param collisionAvoidanceFactor  the factor
140         * @return the builder
141         */
142        public RedeliveryPolicyDefinition collisionAvoidanceFactor(double collisionAvoidanceFactor) {
143            setCollisionAvoidanceFactor(collisionAvoidanceFactor);
144            return this;
145        }
146    
147        /**
148         * Sets the fixed delay between redeliveries
149         *
150         * @param delay  delay in millis
151         * @return the builder
152         */
153        public RedeliveryPolicyDefinition redeliveryDelay(long delay) {
154            setRedeliveryDelay(delay);
155            return this;
156        }
157    
158        /**
159         * Sets the logging level to use when retries has exhausted
160         *
161         * @param retriesExhaustedLogLevel  the logging level
162         * @return the builder
163         */
164        public RedeliveryPolicyDefinition retriesExhaustedLogLevel(LoggingLevel retriesExhaustedLogLevel) {
165            setRetriesExhaustedLogLevel(retriesExhaustedLogLevel);
166            return this;
167        }    
168        
169        /**
170         * Sets the logging level to use for logging retry attempts
171         *
172         * @param retryAttemptedLogLevel  the logging level
173         * @return the builder
174         */
175        public RedeliveryPolicyDefinition retryAttemptedLogLevel(LoggingLevel retryAttemptedLogLevel) {
176            setRetryAttemptedLogLevel(retryAttemptedLogLevel);
177            return this;
178        }
179    
180        /**
181         * Sets wheter stack traces should be logged, can be used to reduce verbose.
182         *
183         * @param logStackTrace  wheter stack traces should be logged or not
184         * @return the builder
185         */
186        public RedeliveryPolicyDefinition logStackTrace(boolean logStackTrace) {
187            setLogStackTrace(logStackTrace);
188            return this;
189        }
190    
191    
192            
193        /**
194         * Sets the maximum redeliveries
195         * <ul>
196         *   <li>5 = default value</li>
197         *   <li>0 = no redeliveries</li>
198         *   <li>-1 = redeliver forever</li>
199         * </ul>
200         *
201         * @param maximumRedeliveries  the value
202         * @return the builder
203         */
204        public RedeliveryPolicyDefinition maximumRedeliveries(int maximumRedeliveries) {
205            setMaximumRedeliveries(maximumRedeliveries);
206            return this;
207        }
208    
209        /**
210         * Turn on collision avoidance.
211         *
212         * @return the builder
213         */
214        public RedeliveryPolicyDefinition useCollisionAvoidance() {
215            setUseCollisionAvoidance(Boolean.TRUE);
216            return this;
217        }
218    
219        /**
220         * Turn on exponential backk off
221         *
222         * @return the builder
223         */
224        public RedeliveryPolicyDefinition useExponentialBackOff() {
225            setUseExponentialBackOff(Boolean.TRUE);
226            return this;
227        }
228    
229        /**
230         * Sets the maximum delay between redelivery
231         *
232         * @param maximumRedeliveryDelay  the delay in millis
233         * @return the builder
234         */
235        public RedeliveryPolicyDefinition maximumRedeliveryDelay(long maximumRedeliveryDelay) {
236            setMaximumRedeliveryDelay(maximumRedeliveryDelay);
237            return this;
238        }
239    
240        /**
241         * Use redelivery policy looked up in the registry
242         *
243         * @param ref  reference to the redelivery policy to lookup and use
244         * @return the builder
245         */
246        public RedeliveryPolicyDefinition ref(String ref) {
247            setRef(ref);
248            return this;
249        }
250    
251        // Properties
252        //-------------------------------------------------------------------------
253    
254        public Double getBackOffMultiplier() {
255            return backOffMultiplier;
256        }
257    
258        public void setBackOffMultiplier(Double backOffMultiplier) {
259            this.backOffMultiplier = backOffMultiplier;
260        }
261    
262        public Double getCollisionAvoidanceFactor() {
263            return collisionAvoidanceFactor;
264        }
265    
266        public void setCollisionAvoidanceFactor(Double collisionAvoidanceFactor) {
267            this.collisionAvoidanceFactor = collisionAvoidanceFactor;
268        }
269    
270        public Long getRedeliveryDelay() {
271            return redeliveryDelay;
272        }
273    
274        public void setRedeliveryDelay(Long delay) {
275            this.redeliveryDelay = delay;
276        }
277    
278        public Integer getMaximumRedeliveries() {
279            return maximumRedeliveries;
280        }
281    
282        public void setMaximumRedeliveries(Integer maximumRedeliveries) {
283            this.maximumRedeliveries = maximumRedeliveries;
284        }
285    
286        public Boolean getUseCollisionAvoidance() {
287            return useCollisionAvoidance;
288        }
289    
290        public void setUseCollisionAvoidance(Boolean useCollisionAvoidance) {
291            this.useCollisionAvoidance = useCollisionAvoidance;
292        }
293    
294        public Boolean getUseExponentialBackOff() {
295            return useExponentialBackOff;
296        }
297    
298        public void setUseExponentialBackOff(Boolean useExponentialBackOff) {
299            this.useExponentialBackOff = useExponentialBackOff;
300        }
301    
302        public Long getMaximumRedeliveryDelay() {
303            return maximumRedeliveryDelay;
304        }
305    
306        public void setMaximumRedeliveryDelay(Long maximumRedeliveryDelay) {
307            this.maximumRedeliveryDelay = maximumRedeliveryDelay;
308        }
309    
310        public void setRetriesExhaustedLogLevel(LoggingLevel retriesExhaustedLogLevel) {
311            this.retriesExhaustedLogLevel = retriesExhaustedLogLevel;
312        }
313    
314        public LoggingLevel getRetriesExhaustedLogLevel() {
315            return retriesExhaustedLogLevel;
316        } 
317    
318        public void setRetryAttemptedLogLevel(LoggingLevel retryAttemptedLogLevel) {
319            this.retryAttemptedLogLevel = retryAttemptedLogLevel;
320        }
321    
322        public LoggingLevel getRetryAttemptedLogLevel() {
323            return retryAttemptedLogLevel;
324        }
325    
326        public String getRef() {
327            return ref;
328        }
329    
330        public void setRef(String ref) {
331            this.ref = ref;
332        }
333    
334        public Boolean getLogStackTrace() {
335            return logStackTrace;
336        }
337    
338        public void setLogStackTrace(Boolean logStackTrace) {
339            this.logStackTrace = logStackTrace;
340        }
341    
342        public Boolean getDisableRedelivery() {
343            return disableRedelivery;
344        }
345    
346        public void setDisableRedelivery(Boolean disableRedelivery) {
347            this.disableRedelivery = disableRedelivery;
348        }
349    }