1 6 package org.logicalcobwebs.proxool; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 20 public abstract class ResultMonitor { 21 22 private static final Log LOG = LogFactory.getLog(ResultMonitor.class); 23 24 27 public static final int WAITING = 0; 28 29 32 public static final int SUCCESS = 1; 33 34 38 public static final int TIMEOUT = 3; 39 40 43 private long timeout = 60000; 44 45 private int result = WAITING; 46 47 private int delay = 500; 48 49 54 public abstract boolean check() throws Exception ; 55 56 62 public int getResult() throws ProxoolException { 63 64 try { 65 long startTime = System.currentTimeMillis(); 66 if (check()) { 67 result = SUCCESS; 68 } 69 while (true) { 70 if (System.currentTimeMillis() - startTime > timeout) { 71 result = TIMEOUT; 72 LOG.debug("Timeout"); 73 break; 74 } 75 try { 76 Thread.sleep(delay); 77 } catch (InterruptedException e) { 78 LOG.error("Awoken", e); 79 } 80 if (check()) { 81 result = SUCCESS; 82 LOG.debug("Success"); 83 break; 84 } 85 } 86 return result; 87 } catch (Exception e) { 88 throw new ProxoolException("Problem monitoring result", e); 89 } 90 } 91 92 96 public void setTimeout(long timeout) { 97 this.timeout = timeout; 98 } 99 100 public void setDelay(int delay) { 101 this.delay = delay; 102 } 103 } 104 105 106 | Popular Tags |