FileDocCategorySizeDatePackage
WaitFor.javaAPI DocApache Ant 1.707814Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.taskdefs

WaitFor

public class WaitFor extends org.apache.tools.ant.taskdefs.condition.ConditionBase
Wait for an external event to occur. Wait for an external process to start or to complete some task. This is useful with the parallel task to synchronize the execution of tests with server startup. The following attributes can be specified on a waitfor task:
  • maxwait - maximum length of time to wait before giving up
  • maxwaitunit - The unit to be used to interpret maxwait attribute
  • checkevery - amount of time to sleep between each check
  • checkeveryunit - The unit to be used to interpret checkevery attribute
  • timeoutproperty - name of a property to set if maxwait has been exceeded.
The maxwaitunit and checkeveryunit are allowed to have the following values: millisecond, second, minute, hour, day and week. The default is millisecond. For programmatic use/subclassing, there are two methods that may be overrridden, processSuccess and processTimeout
since
Ant 1.5
ant.task
category="control"

Fields Summary
private long
maxWaitMillis
default max wait time
private long
maxWaitMultiplier
private long
checkEveryMillis
private long
checkEveryMultiplier
private String
timeoutProperty
Constructors Summary
public WaitFor()
Constructor, names this task "waitfor".


              
      
        super("waitfor");
    
Methods Summary
public voidexecute()
Check repeatedly for the specified conditions until they become true or the timeout expires.

throws
BuildException on error

        if (countConditions() > 1) {
            throw new BuildException("You must not nest more than one "
                                     + "condition into "
                                     + getTaskName());
        }
        if (countConditions() < 1) {
            throw new BuildException("You must nest a condition into "
                                     + getTaskName());
        }
        Condition c = (Condition) getConditions().nextElement();

        long savedMaxWaitMillis = maxWaitMillis;
        long savedCheckEveryMillis = checkEveryMillis;
        try {
            maxWaitMillis *= maxWaitMultiplier;
            checkEveryMillis *= checkEveryMultiplier;
            long start = System.currentTimeMillis();
            long end = start + maxWaitMillis;

            while (System.currentTimeMillis() < end) {
                if (c.eval()) {
                    processSuccess();
                    return;
                }
                try {
                    Thread.sleep(checkEveryMillis);
                } catch (InterruptedException e) {
                    // ignore
                }
            }
            processTimeout();
        } finally {
            maxWaitMillis = savedMaxWaitMillis;
            checkEveryMillis = savedCheckEveryMillis;
        }
    
protected voidprocessSuccess()
Actions to be taken on a successful waitfor. This is an override point. The base implementation does nothing.

since
Ant1.7

        log(getTaskName() + ": condition was met", Project.MSG_VERBOSE);
    
protected voidprocessTimeout()
Actions to be taken on an unsuccessful wait. This is an override point. It is where the timeout processing takes place. The base implementation sets the timeoutproperty if there was a timeout and the property was defined.

since
Ant1.7

        log(getTaskName() + ": timeout", Project.MSG_VERBOSE);
        if (timeoutProperty != null) {
            getProject().setNewProperty(timeoutProperty, "true");
        }
    
public voidsetCheckEvery(long time)
Set the time between each check

param
time a long value

        checkEveryMillis = time;
    
public voidsetCheckEveryUnit(org.apache.tools.ant.taskdefs.WaitFor$Unit unit)
Set the check every time unit

param
unit an enumerated Unit value

        checkEveryMultiplier = unit.getMultiplier();
    
public voidsetMaxWait(long time)
Set the maximum length of time to wait.

param
time a long value

        maxWaitMillis = time;
    
public voidsetMaxWaitUnit(org.apache.tools.ant.taskdefs.WaitFor$Unit unit)
Set the max wait time unit

param
unit an enumerated Unit value

        maxWaitMultiplier = unit.getMultiplier();
    
public voidsetTimeoutProperty(java.lang.String p)
Name the property to set after a timeout.

param
p the property name

        timeoutProperty = p;