Coverage Report - org.apache.camel.processor.RedeliveryPolicy
 
Classes in this File Line Coverage Branch Coverage Complexity
RedeliveryPolicy
38% 
67% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.processor;
 18  
 
 19  
 import java.io.Serializable;
 20  
 import java.util.Random;
 21  
 
 22  
 // Code taken from the ActiveMQ codebase
 23  
 
 24  
 /**
 25  
  * The policy used to decide how many times to redeliver and the time between
 26  
  * the redeliveries before being sent to a <a
 27  
  * href="http://activemq.apache.org/camel/dead-letter-channel.html">Dead Letter
 28  
  * Channel</a>
 29  
  * 
 30  
  * @version $Revision: 565361 $
 31  
  */
 32  
 public class RedeliveryPolicy implements Cloneable, Serializable {
 33  
     protected static transient Random randomNumberGenerator;
 34  327
     protected int maximumRedeliveries = 6;
 35  327
     protected long initialRedeliveryDelay = 1000L;
 36  327
     protected double backOffMultiplier = 2;
 37  
     protected boolean useExponentialBackOff;
 38  
     // +/-15% for a 30% spread -cgs
 39  327
     protected double collisionAvoidanceFactor = 0.15d;
 40  
     protected boolean useCollisionAvoidance;
 41  
 
 42  327
     public RedeliveryPolicy() {
 43  327
     }
 44  
 
 45  
     @Override
 46  
     public String toString() {
 47  297
         return "RedeliveryPolicy[maximumRedeliveries=" + maximumRedeliveries + "]";
 48  
     }
 49  
 
 50  
     public RedeliveryPolicy copy() {
 51  
         try {
 52  12
             return (RedeliveryPolicy)clone();
 53  0
         } catch (CloneNotSupportedException e) {
 54  0
             throw new RuntimeException("Could not clone: " + e, e);
 55  
         }
 56  
     }
 57  
 
 58  
     /**
 59  
      * Returns true if the policy decides that the message exchange should be
 60  
      * redelivered
 61  
      */
 62  
     public boolean shouldRedeliver(int redeliveryCounter) {
 63  21
         return redeliveryCounter < getMaximumRedeliveries();
 64  
     }
 65  
 
 66  
     // Builder methods
 67  
     // -------------------------------------------------------------------------
 68  
 
 69  
     /**
 70  
      * Sets the maximum number of times a message exchange will be redelivered
 71  
      */
 72  
     public RedeliveryPolicy maximumRedeliveries(int maximumRedeliveries) {
 73  6
         setMaximumRedeliveries(maximumRedeliveries);
 74  6
         return this;
 75  
     }
 76  
 
 77  
     /**
 78  
      * Sets the initial redelivery delay in milliseconds on the first redelivery
 79  
      */
 80  
     public RedeliveryPolicy initialRedeliveryDelay(long initialRedeliveryDelay) {
 81  6
         setInitialRedeliveryDelay(initialRedeliveryDelay);
 82  6
         return this;
 83  
     }
 84  
 
 85  
     /**
 86  
      * Enables collision avoidence which adds some randomization to the backoff
 87  
      * timings to reduce contention probability
 88  
      */
 89  
     public RedeliveryPolicy useCollisionAvoidance() {
 90  0
         setUseCollisionAvoidance(true);
 91  0
         return this;
 92  
     }
 93  
 
 94  
     /**
 95  
      * Enables exponential backof using the {@link #getBackOffMultiplier()} to
 96  
      * increase the time between retries
 97  
      */
 98  
     public RedeliveryPolicy useExponentialBackOff() {
 99  0
         setUseExponentialBackOff(true);
 100  0
         return this;
 101  
     }
 102  
 
 103  
     /**
 104  
      * Enables exponential backoff and sets the multiplier used to increase the
 105  
      * delay between redeliveries
 106  
      */
 107  
     public RedeliveryPolicy backOffMultiplier(double multiplier) {
 108  0
         useExponentialBackOff();
 109  0
         setBackOffMultiplier(multiplier);
 110  0
         return this;
 111  
     }
 112  
 
 113  
     /**
 114  
      * Enables collision avoidence and sets the percentage used
 115  
      */
 116  
     public RedeliveryPolicy collisionAvoidancePercent(double collisionAvoidancePercent) {
 117  0
         useCollisionAvoidance();
 118  0
         setCollisionAvoidancePercent(collisionAvoidancePercent);
 119  0
         return this;
 120  
     }
 121  
 
 122  
     // Properties
 123  
     // -------------------------------------------------------------------------
 124  
     public double getBackOffMultiplier() {
 125  0
         return backOffMultiplier;
 126  
     }
 127  
 
 128  
     /**
 129  
      * Sets the multiplier used to increase the delay between redeliveries if
 130  
      * {@link #setUseExponentialBackOff(boolean)} is enabled
 131  
      */
 132  
     public void setBackOffMultiplier(double backOffMultiplier) {
 133  0
         this.backOffMultiplier = backOffMultiplier;
 134  0
     }
 135  
 
 136  
     public short getCollisionAvoidancePercent() {
 137  0
         return (short)Math.round(collisionAvoidanceFactor * 100);
 138  
     }
 139  
 
 140  
     /**
 141  
      * Sets the percentage used for collision avoidence if enabled via
 142  
      * {@link #setUseCollisionAvoidance(boolean)}
 143  
      */
 144  
     public void setCollisionAvoidancePercent(double collisionAvoidancePercent) {
 145  0
         this.collisionAvoidanceFactor = collisionAvoidancePercent * 0.01d;
 146  0
     }
 147  
 
 148  
     public double getCollisionAvoidanceFactor() {
 149  0
         return collisionAvoidanceFactor;
 150  
     }
 151  
 
 152  
     /**
 153  
      * Sets the factor used for collision avoidence if enabled via
 154  
      * {@link #setUseCollisionAvoidance(boolean)}
 155  
      */
 156  
     public void setCollisionAvoidanceFactor(double collisionAvoidanceFactor) {
 157  0
         this.collisionAvoidanceFactor = collisionAvoidanceFactor;
 158  0
     }
 159  
 
 160  
     public long getInitialRedeliveryDelay() {
 161  0
         return initialRedeliveryDelay;
 162  
     }
 163  
 
 164  
     /**
 165  
      * Sets the initial redelivery delay in milliseconds on the first redelivery
 166  
      */
 167  
     public void setInitialRedeliveryDelay(long initialRedeliveryDelay) {
 168  6
         this.initialRedeliveryDelay = initialRedeliveryDelay;
 169  6
     }
 170  
 
 171  
     public int getMaximumRedeliveries() {
 172  21
         return maximumRedeliveries;
 173  
     }
 174  
 
 175  
     /**
 176  
      * Sets the maximum number of times a message exchange will be redelivered
 177  
      */
 178  
     public void setMaximumRedeliveries(int maximumRedeliveries) {
 179  18
         this.maximumRedeliveries = maximumRedeliveries;
 180  18
     }
 181  
 
 182  
     public long getRedeliveryDelay(long previousDelay) {
 183  
         long redeliveryDelay;
 184  
 
 185  9
         if (previousDelay == 0) {
 186  9
             redeliveryDelay = initialRedeliveryDelay;
 187  9
         } else if (useExponentialBackOff && backOffMultiplier > 1) {
 188  0
             redeliveryDelay = Math.round(backOffMultiplier * previousDelay);
 189  0
         } else {
 190  0
             redeliveryDelay = previousDelay;
 191  
         }
 192  
 
 193  9
         if (useCollisionAvoidance) {
 194  
 
 195  
             /*
 196  
              * First random determines +/-, second random determines how far to
 197  
              * go in that direction. -cgs
 198  
              */
 199  0
             Random random = getRandomNumberGenerator();
 200  0
             double variance = (random.nextBoolean() ? collisionAvoidanceFactor : -collisionAvoidanceFactor)
 201  
                               * random.nextDouble();
 202  0
             redeliveryDelay += redeliveryDelay * variance;
 203  
         }
 204  
 
 205  9
         return redeliveryDelay;
 206  
     }
 207  
 
 208  
     public boolean isUseCollisionAvoidance() {
 209  0
         return useCollisionAvoidance;
 210  
     }
 211  
 
 212  
     /**
 213  
      * Enables/disables collision avoidence which adds some randomization to the
 214  
      * backoff timings to reduce contention probability
 215  
      */
 216  
     public void setUseCollisionAvoidance(boolean useCollisionAvoidance) {
 217  0
         this.useCollisionAvoidance = useCollisionAvoidance;
 218  0
     }
 219  
 
 220  
     public boolean isUseExponentialBackOff() {
 221  0
         return useExponentialBackOff;
 222  
     }
 223  
 
 224  
     /**
 225  
      * Enables/disables exponential backof using the
 226  
      * {@link #getBackOffMultiplier()} to increase the time between retries
 227  
      */
 228  
     public void setUseExponentialBackOff(boolean useExponentialBackOff) {
 229  0
         this.useExponentialBackOff = useExponentialBackOff;
 230  0
     }
 231  
 
 232  
     protected static synchronized Random getRandomNumberGenerator() {
 233  0
         if (randomNumberGenerator == null) {
 234  0
             randomNumberGenerator = new Random();
 235  
         }
 236  0
         return randomNumberGenerator;
 237  
     }
 238  
 }