KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > kernel > overflow > ExponentialBackoff


1 package com.ubermq.kernel.overflow;
2
3 import com.ubermq.kernel.IOverflowHandler;
4 import com.ubermq.kernel.IDatagram;
5 import java.nio.*;
6
7 /**
8  * A simple exponential backoff overflow handler. This
9  * implementation treats all overflow signals that
10  * arrive within a configurable timeframe as part of the
11  * same overflow, and increment the backoff accordingly.<P>
12  *
13  * The backoff is implemented as a Thread.sleep(). This will
14  * impact servicing of other input buffers even if they
15  * are not related to this outgoing channel.
16  */

17 public class ExponentialBackoff
18     extends AbstractOverflowHandler
19 {
20     private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(ExponentialBackoff.class);
21     
22     final protected long timeout;
23     final protected long maximumTimeout;
24     final protected int factor;
25     final protected boolean shouldFailIfMaximumReached;
26
27     /**
28      * the initial timeout, exponential factor,
29      * timeframe to reset the backoff timeout and
30      * the maximum timeout.
31      * After the maximum timeout, we fail and drop
32      * the message if drop is true. Otherwise
33      * we will block forever.
34      */

35     public ExponentialBackoff(long timeout,
36                   int factor,
37                   long maximumTimeout,
38                   boolean shouldFailIfMaximumReached)
39     {
40         this.timeout = timeout;
41         this.factor = factor;
42         this.maximumTimeout = maximumTimeout;
43         this.shouldFailIfMaximumReached = shouldFailIfMaximumReached;
44     }
45     
46     /**
47      * An exponential backoff with reasonable defaults (for government work)
48      * start with an optimistic wait time and degrade to a reasonably acceptable
49      * 6.4 seconds (backoff factor is 2). we fail when we get there.
50      */

51     public ExponentialBackoff()
52     {
53         this(50, 2, 6400, true);
54     }
55     
56     protected int doOverflow()
57     {
58         try {
59             log.debug("overflow condition - waiting " + timeout + " ms");
60             Thread.sleep(timeout);
61         } catch(InterruptedException JavaDoc ie) {
62             // if we got interrupted,
63
// let's go ahead and retry
64
return IOverflowHandler.ACTION_RETRY;
65         }
66     
67         if (timeout >= maximumTimeout)
68             return shouldFailIfMaximumReached ? IOverflowHandler.ACTION_FAIL : IOverflowHandler.ACTION_RETRY;
69         else return IOverflowHandler.ACTION_RETRY;
70     }
71
72     public IOverflowHandler getRetryHandler()
73     {
74         return new ExponentialBackoff(Math.min(maximumTimeout, timeout * factor),
75                         factor,
76                         maximumTimeout,
77                         shouldFailIfMaximumReached);
78     }
79 }
80
Popular Tags