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 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 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 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 ie) { 62 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 |