Added reset() and setTimeout().

git-svn-id: https://samskivert.googlecode.com/svn/trunk@1194 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert@gmail.com
2003-08-13 01:55:05 +00:00
parent 70ad6989fd
commit 7861bc8507
@@ -1,5 +1,5 @@
// //
// $Id: ServiceWaiter.java,v 1.1 2003/08/12 23:44:33 ray Exp $ // $Id: ServiceWaiter.java,v 1.2 2003/08/13 01:55:05 ray Exp $
package com.samskivert.servlet.util; package com.samskivert.servlet.util;
@@ -33,6 +33,10 @@ import com.samskivert.util.ResultListener;
public class ServiceWaiter public class ServiceWaiter
implements ResultListener implements ResultListener
{ {
/** Timeout to specify when you don't want a timeout. Use at your own
* risk. */
public static final int NO_TIMEOUT = -1;
/** /**
* Construct a ServiceWaiter with the default (30 second) timeout. * Construct a ServiceWaiter with the default (30 second) timeout.
*/ */
@@ -47,10 +51,28 @@ public class ServiceWaiter
* @param timeout the timeout, in seconds. * @param timeout the timeout, in seconds.
*/ */
public ServiceWaiter (int timeout) public ServiceWaiter (int timeout)
{
setTimeout(timeout);
}
/**
* Change the timeout being used for this ServiceWaiter after it
* has been constructed.
*/
public void setTimeout (int timeout)
{ {
_timeout = timeout; _timeout = timeout;
} }
/**
* Reset the service waiter so that it can be used again.
*/
public void reset ()
{
_success = 0;
_argument = null;
}
/** /**
* Marks the request as successful and posts the supplied response * Marks the request as successful and posts the supplied response
* argument for perusal by the caller. * argument for perusal by the caller.
@@ -94,7 +116,12 @@ public class ServiceWaiter
while (_success == 0) { while (_success == 0) {
try { try {
// wait for the response, timing out after a while // wait for the response, timing out after a while
wait(1000L * _timeout); if (_timeout == NO_TIMEOUT) {
wait();
} else {
wait(1000L * _timeout);
}
// if we get here without some sort of response, then // if we get here without some sort of response, then
// we've timed out and we should freak out // we've timed out and we should freak out