Use the PropertyCollector.WaitForUpdatesEx
method to monitor and report Task
state changes.
This example uses the
waitForUpdatesEx method to look for a change in the
info.state property and the
info.result.property of
cloneTask
and
cloneTask2
. When the state is
“success”
,
info.result is the managed object reference of the clone virtual machine.
Note: The order of property retrieval is not guaranteed, and it may take more than one call to
waitForUpdatesEx to retrieve both properties for a task
.
Prerequisites
For this task you need:
- An authenticated Web Services session with the vSphere server that manages the virtual machine.
- Managed object references for two
Task
objects in the variables cloneTask
and cloneTask2
.
- The ViewManager object is referenced by the variable
viewMgr
.
- The PropertyCollector object is referenced by the variable
pCollector
.
Procedure
- Initialize variables for the wait loop.
String version = "";
Boolean wait = true;
WaitOptions waitOptions = new WaitOptions();
while ( wait ) {
- Call WaitForUpdatesEx.
If waitForUpdatesEx
returns null
, that indicates that the call has timed out.
UpdateSet uSet = methods.waitForUpdatesEx(pCollector,
version,
waitOptions);
if (uSet == null) {
wait = false;
} else {
- Save the
version
for subsequent calls to WaitForUpdatesEx
.
version = uSet.getVersion();
- Get the list of property updates.
List<PropertyFilterUpdate> pfUpdates = uSet.getFilterSet();
for (PropertyFilterUpdate pfu : pfUpdates) {
- Get the list of object updates produced by the filter.
List<ObjectUpdate> oUpdates = pfu.getObjectSet();
for (ObjectUpdate ou : oUpdates) {
- Look for
ObjectUpdate.kind=="MODIFY"
(property modified).
if (ou.getKind() == ObjectUpdateKind.MODIFY) {
String name = "";
TaskInfoState state;
ManagedObjectReference cloneRef = new ManagedObjectReference();
- Get the changed data.
List<PropertyChange> pChanges = ou.getChangeSet();
- Output the property changes.
If the task completes successfully, the result
property contains the MOref of the new clone.
for (PropertyChange pc : pChanges) {
name = pc.getName();
if (name.equals("info.state")) {
state = (TaskInfoState)pc.getVal();
System.out.println("State is "+state.value());
} else if (name.equals("info.result")) {
cloneRef = (ManagedObjectReference)pc.getVal();
System.out.println("Clone reference is "+cloneRef.getValue());
} // end if name.equals
} // end for pc
} // end if ou.getKind
} // end for ou
} // end for pfu
} // end if user
} // end while wait
} // end cloneVM
Results
This code does not set a time-out (WaitOptions.maxWaitSeconds is unset), so after it has retrieved all of the property changes, waitForUpdatesEx will block the thread, waiting for the TCP connection with the vSphere Server to time out.
How a client application handles the session depends on the particular context. For example, the client can call WaitForUpdatesEx from its own thread, look for specific updates and then stop calling the method. For the preceding example, you might choose to watch for the Task.info.state
to transition to a value of success
or error
.
For more information about WaitOptions and the waitForUpdatesEx method, see Client Data Synchronization with WaitForUpdatesEx.