KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > RedeliveryPolicy


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.activemq;
19
20 import java.io.Serializable JavaDoc;
21 import java.util.Random JavaDoc;
22
23 /**
24  * Configuration options used to control how messages are re-delivered when they
25  * are rolled back.
26  *
27  * @org.apache.xbean.XBean element="redeliveryPolicy"
28  *
29  * @version $Revision: 1.11 $
30  */

31 public class RedeliveryPolicy implements Cloneable JavaDoc, Serializable JavaDoc {
32
33     public static final int NO_MAXIMUM_REDELIVERIES = -1;
34     
35     // +/-15% for a 30% spread -cgs
36
protected double collisionAvoidanceFactor = 0.15d;
37     protected int maximumRedeliveries = 6;
38     protected long initialRedeliveryDelay = 1000L;
39     protected static Random JavaDoc 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 JavaDoc e) {
52             throw new RuntimeException JavaDoc("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             /*
101              * First random determines +/-, second random determines how far to
102              * go in that direction. -cgs
103              */

104             Random JavaDoc 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 JavaDoc getRandomNumberGenerator() {
129         if (randomNumberGenerator == null) {
130             randomNumberGenerator = new Random JavaDoc();
131         }
132         return randomNumberGenerator;
133     }
134
135 }
136
Popular Tags