Monitoring TaskInfo Properties

To monitor the state of a Task, use the PropertyCollector.WaitForUpdatesEx method.

You can monitor the values of TaskInfo properties, which change as the Task runs to completion. For example, you can check the values of startTime, queueTime, completeTime, progress, result, and state as the operation progresses. Monitor these properties in your code in a separate thread until the Task completes, while the main line of your code continues with other activities.

For more information about monitoring properties, see Client Data Synchronization (WaitForUpdatesEx).

Your code must handle the datatype returned when the Task completes (managed object reference, data object, and so on). In addition to success, queued, and running, an operation can enter an error state, which your code must handle.

A Task object has a lifecycle that is independent of the TaskManager that creates it and independent of the entity with which it is associated. It exists to convey status about an operation. You can discard the reference to it when your application no longer needs the information.

The following example shows a code fragment that obtains values for the info property from each Task object in the array.

Displaying TaskInfoState Values for Tasks in recentTask Array

...
private void displayTasks(ObjectContent[] oContents) {
      for(int oci=0; oci<oContents.length; ++oci) {
         System.out.println("Task");
         DynamicProperty[] dps = oContents[oci].getPropSet();
         if(dps!=null) {
            String op="", name="", type="", state="", error="";
            for(int dpi=0; dpi<dps.length; ++dpi) {
               DynamicProperty dp = dps[dpi];
               if("info.entity".equals(dp.getName())) {
                  type = ((ManagedObjectReference)dp.getVal()).getType();
               } else if ("info.entityName".equals(dp.getName())) {
                  name = ((String)dp.getVal());
               } else if ("info.name".equals(dp.getName())) {
                  op = ((String)dp.getVal());
               } else if ("info.state".equals(dp.getName())) {
                  TaskInfoState tis = (TaskInfoState)dp.getVal();
                  if(TaskInfoState.error.equals(tis)) {
                     state = "-Error";
                  } else if(TaskInfoState.queued.equals(tis)) {
                     state = "-Queued";
                  } else if(TaskInfoState.running.equals(tis)) {
                     state = "-Running";
                  } else if(TaskInfoState.success.equals(tis)) {
                     state = "-Success";
                  }
               } else if ("info.cancelled".equals(dp.getName())) {
                  Boolean b = (Boolean)dp.getVal();
                  if(b != null && b.booleanValue()) {
                     state += "-Cancelled";
                  }
              }...

Sample Run of the TaskList Java Application shows output from a run of the program. See the source code listing for TaskList.java or for TaskList.cs in the vSphere Web Services SDK package for details.

Sample Run of the TaskList Java Application

java com.vmware.samples.general.TaskList --url https://srv/sdk --username root --password *******

Started
Task
Operation AcquireCimServicesTicket
Name srv
Type HostSystem
State -Success
Error
======================
Ended TaskList