001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010:
011: /**
012: * Waits for a set of results to become true with timeout
013: * functionality
014: *
015: * @version $Revision: 1.10 $, $Date: 2006/01/18 14:40:06 $
016: * @author bill
017: * @author $Author: billhorsman $ (current maintainer)
018: * @since Proxool 0.8
019: */
020: public abstract class ResultMonitor {
021:
022: private static final Log LOG = LogFactory
023: .getLog(ResultMonitor.class);
024:
025: /**
026: * This monitor is still waiting for the result to come true
027: */
028: public static final int WAITING = 0;
029:
030: /**
031: * The result has happened
032: */
033: public static final int SUCCESS = 1;
034:
035: /**
036: * There was a timeout waiting for the result to happen
037: * @see #setTimeout
038: */
039: public static final int TIMEOUT = 3;
040:
041: /**
042: * Seems awfully long, but it seems to need it. Sometimes.
043: */
044: private long timeout = 60000;
045:
046: private int result = WAITING;
047:
048: private int delay = 500;
049:
050: /**
051: * Override this with your specific check
052: * @return true if the result has happened, else false
053: * @throws Exception if anything goes wrong
054: */
055: public abstract boolean check() throws Exception;
056:
057: /**
058: * Wait for the result to happen, or for a timeout
059: * @return {@link #SUCCESS} or {@link #TIMEOUT}
060: * @throws ProxoolException if the {@link #check} threw an exception
061: * @see #setTimeout
062: */
063: public int getResult() throws ProxoolException {
064:
065: try {
066: long startTime = System.currentTimeMillis();
067: if (check()) {
068: result = SUCCESS;
069: }
070: while (true) {
071: if (System.currentTimeMillis() - startTime > timeout) {
072: result = TIMEOUT;
073: LOG.debug("Timeout");
074: break;
075: }
076: try {
077: Thread.sleep(delay);
078: } catch (InterruptedException e) {
079: LOG.error("Awoken", e);
080: }
081: if (check()) {
082: result = SUCCESS;
083: LOG.debug("Success");
084: break;
085: }
086: }
087: return result;
088: } catch (Exception e) {
089: throw new ProxoolException("Problem monitoring result", e);
090: }
091: }
092:
093: /**
094: * Set the timeout
095: * @param timeout milliseconds
096: */
097: public void setTimeout(long timeout) {
098: this .timeout = timeout;
099: }
100:
101: public void setDelay(int delay) {
102: this .delay = delay;
103: }
104: }
105:
106: /*
107: Revision history:
108: $Log: ResultMonitor.java,v $
109: Revision 1.10 2006/01/18 14:40:06 billhorsman
110: Unbundled Jakarta's Commons Logging.
111:
112: Revision 1.9 2003/03/05 18:44:10 billhorsman
113: fix delay and timeout
114:
115: Revision 1.8 2003/03/04 10:24:40 billhorsman
116: removed try blocks around each test
117:
118: Revision 1.7 2003/03/03 11:12:05 billhorsman
119: fixed licence
120:
121: Revision 1.6 2003/03/02 00:53:38 billhorsman
122: increased timeout to 60 sec!
123:
124: Revision 1.5 2003/03/01 18:17:51 billhorsman
125: arrffgh. fix,
126:
127: Revision 1.4 2003/03/01 16:54:20 billhorsman
128: fix
129:
130: Revision 1.3 2003/03/01 15:27:24 billhorsman
131: checkstyle
132:
133: Revision 1.2 2003/03/01 15:22:50 billhorsman
134: doc
135:
136: Revision 1.1 2003/03/01 15:14:15 billhorsman
137: new ResultMonitor to help cope with test threads
138:
139: */
|