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