*
*/
public boolean waitForTask(ManagedObjectReference task, int runningDelayInMillSecond, int queuedDelayInMillSecond) throws RuntimeFaultFaultMsg, RemoteException,
InterruptedException {
boolean retVal = false;
TaskInfoState tState = null;
int tries = 0;
int maxTries = 3;
Exception getInfoException = null;
while ((tState == null) || tState.equals(TaskInfoState.RUNNING) || tState.equals(TaskInfoState.QUEUED)) {
tState = null;
getInfoException = null;
tries = 0;
// under load getTaskInfo may return null when there really is valid task info, so we try 3 times to get it.
while (tState == null) {
tries++;
if (tries > maxTries) {
if (getInfoException == null) {
throw new RuntimeException("VCenter failed to return task info after 3 tries.");
} else if (getInfoException instanceof RuntimeFaultFaultMsg) {
throw (RuntimeFaultFaultMsg) getInfoException;
} else if (getInfoException instanceof RemoteException) {
throw (RemoteException) getInfoException;
} else {
throw new RuntimeException(getInfoException);
}
}
try {
TaskInfo tInfo = (TaskInfo) getDynamicProperty(task, "info");
if (tInfo != null) {
tState = tInfo.getState();
}
} catch (Exception e) {
// silently catch 3 exceptions
getInfoException = e;
}
}
// sleep for a specified time based on task state.
if (tState.equals(TaskInfoState.RUNNING)) {
Thread.sleep(runningDelayInMillSecond);
} else {
Thread.sleep(queuedDelayInMillSecond);
}
}
if (tState.equals(TaskInfoState.SUCCESS)) {
retVal = true;
}
return retVal;
}