KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > ResultMonitor


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 /**
12  * Waits for a set of results to become true with timeout
13  * functionality
14  *
15  * @version $Revision: 1.10 $, $Date: 2006/01/18 14:40:06 $
16  * @author bill
17  * @author $Author: billhorsman $ (current maintainer)
18  * @since Proxool 0.8
19  */

20 public abstract class ResultMonitor {
21
22     private static final Log LOG = LogFactory.getLog(ResultMonitor.class);
23
24     /**
25      * This monitor is still waiting for the result to come true
26      */

27     public static final int WAITING = 0;
28
29     /**
30      * The result has happened
31      */

32     public static final int SUCCESS = 1;
33
34     /**
35      * There was a timeout waiting for the result to happen
36      * @see #setTimeout
37      */

38     public static final int TIMEOUT = 3;
39
40     /**
41      * Seems awfully long, but it seems to need it. Sometimes.
42      */

43     private long timeout = 60000;
44
45     private int result = WAITING;
46
47     private int delay = 500;
48
49     /**
50      * Override this with your specific check
51      * @return true if the result has happened, else false
52      * @throws Exception if anything goes wrong
53      */

54     public abstract boolean check() throws Exception JavaDoc;
55
56     /**
57      * Wait for the result to happen, or for a timeout
58      * @return {@link #SUCCESS} or {@link #TIMEOUT}
59      * @throws ProxoolException if the {@link #check} threw an exception
60      * @see #setTimeout
61      */

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 JavaDoc 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 JavaDoc e) {
88             throw new ProxoolException("Problem monitoring result", e);
89         }
90     }
91
92     /**
93      * Set the timeout
94      * @param timeout milliseconds
95      */

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 /*
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  */
Popular Tags