1 18 package org.apache.activemq; 19 20 import java.io.Serializable ; 21 import java.util.Random ; 22 23 31 public class RedeliveryPolicy implements Cloneable , Serializable { 32 33 public static final int NO_MAXIMUM_REDELIVERIES = -1; 34 35 protected double collisionAvoidanceFactor = 0.15d; 37 protected int maximumRedeliveries = 6; 38 protected long initialRedeliveryDelay = 1000L; 39 protected static Random randomNumberGenerator; 40 protected boolean useCollisionAvoidance = false; 41 protected boolean useExponentialBackOff = false; 42 protected short backOffMultiplier = 5; 43 44 public RedeliveryPolicy() { 45 } 46 47 public RedeliveryPolicy copy() { 48 try { 49 return (RedeliveryPolicy) clone(); 50 } 51 catch (CloneNotSupportedException e) { 52 throw new RuntimeException ("Could not clone: " + e, e); 53 } 54 } 55 56 public short getBackOffMultiplier() { 57 return backOffMultiplier; 58 } 59 60 public void setBackOffMultiplier(short backOffMultiplier) { 61 this.backOffMultiplier = backOffMultiplier; 62 } 63 64 public short getCollisionAvoidancePercent() { 65 return (short) Math.round(collisionAvoidanceFactor * 100); 66 } 67 68 public void setCollisionAvoidancePercent(short collisionAvoidancePercent) { 69 this.collisionAvoidanceFactor = collisionAvoidancePercent * 0.01d; 70 } 71 72 public long getInitialRedeliveryDelay() { 73 return initialRedeliveryDelay; 74 } 75 76 public void setInitialRedeliveryDelay(long initialRedeliveryDelay) { 77 this.initialRedeliveryDelay = initialRedeliveryDelay; 78 } 79 80 public int getMaximumRedeliveries() { 81 return maximumRedeliveries; 82 } 83 84 public void setMaximumRedeliveries(int maximumRedeliveries) { 85 this.maximumRedeliveries = maximumRedeliveries; 86 } 87 88 public long getRedeliveryDelay(long previousDelay) { 89 long redeliveryDelay; 90 91 if (previousDelay == 0) { 92 redeliveryDelay = initialRedeliveryDelay; 93 } else if (useExponentialBackOff && backOffMultiplier > 1) { 94 redeliveryDelay = previousDelay * backOffMultiplier; 95 } else { 96 redeliveryDelay = previousDelay; 97 } 98 99 if (useCollisionAvoidance) { 100 104 Random random = getRandomNumberGenerator(); 105 double variance = (random.nextBoolean() ? collisionAvoidanceFactor : -collisionAvoidanceFactor) * random.nextDouble(); 106 redeliveryDelay += redeliveryDelay * variance; 107 } 108 109 return redeliveryDelay; 110 } 111 112 public boolean isUseCollisionAvoidance() { 113 return useCollisionAvoidance; 114 } 115 116 public void setUseCollisionAvoidance(boolean useCollisionAvoidance) { 117 this.useCollisionAvoidance = useCollisionAvoidance; 118 } 119 120 public boolean isUseExponentialBackOff() { 121 return useExponentialBackOff; 122 } 123 124 public void setUseExponentialBackOff(boolean useExponentialBackOff) { 125 this.useExponentialBackOff = useExponentialBackOff; 126 } 127 128 protected static synchronized Random getRandomNumberGenerator() { 129 if (randomNumberGenerator == null) { 130 randomNumberGenerator = new Random (); 131 } 132 return randomNumberGenerator; 133 } 134 135 } 136 | Popular Tags |