VMware {code} Community
daniellynn
Enthusiast
Enthusiast

Accessing Tasks

I'm at a loss on this. I've looked at a few tutorials, other community posts, and it looks like I'm doing everything right. Here is the code I'm using:

TaskFilterSpec tFilter = new TaskFilterSpec();

tFilter.entity = new TaskFilterSpecByEntity();

tFilter.entity.entity = vmRef;

tFilter.entity.recursion = TaskFilterSpecRecursionOption.self;

ManagedObjectReference myTaskManager = sic.taskManager;

ManagedObjectReference taskPages = service.CreateCollectorForTasks(myTaskManager, tFilter);

TaskInfo[] TaskArray = (TaskInfo[])PropertyFunctions.PropertyFunctions.getObjectProperty(taskPages, "latestPage", service);

NumberReturned = TaskArray.Length.ToString();

and the result is I get no tasks returned. I checked and there are definitely tasks. I've also tried to not use a filter spec to get all tasks and that returns nothing. I get no errors, just no tasks. Am I missing a step? I also tried the taskManager's "RecentTasks" property and that comes back empty too.

Reply
0 Kudos
1 Reply
storm27
Enthusiast
Enthusiast

Hi,

tried the same thing with different approach which might work in your case as well.

To get the list of the recent tasks, you need to fetch the recentTasks property of the task manager and for this you would need to create a property filter spec.

Using this approach I am able to see the current tasks list. Please refer the attached code snippet for the same.

Code Snippet:

/* this method creates the spec for the task

@param taskmor Managed objject reference to task manager

@return PropertyFilterSpec array

*/

public PropertyFilterSpec[] createSpec(ManagedObjectReference taskmor) {

PropertySpec pspec = new PropertySpec();

pspec.setAll(Boolean.FALSE);

pspec.setType("Task");

pspec.setPathSet(new String[]

{"info.entity", "info.name"});

ObjectSpec ospec = new ObjectSpec();

ospec.setObj(taskmor);

ospec.setSkip(Boolean.FALSE);

TraversalSpec tspec = new TraversalSpec();

tspec.setType("TaskManager");

tspec.setPath("recentTask");

tspec.setSkip(Boolean.FALSE);

ospec.setSelectSet(new SelectionSpec[]);

PropertyFilterSpec pfspec = new PropertyFilterSpec();

pfspec.setPropSet(new PropertySpec[]);

pfspec.setObjectSet(new ObjectSpec[]);

return new PropertyFilterSpec[];

}

/*to display the recent tasks on the ESX host or vCenter

*

*@param ocs ObjectContent

**/

public void displayTasks(ObjectContent[] ocs) {

String op = "";

String type = "";

for(int oci=0;oci<ocs.length;++oci) {

DynamicProperty[] dps = ocs[oci].getPropSet();

if(dps!=null) {

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.name".equals(dp.getName())) {

op = ((String)dp.getVal());

}

}

System.out.println("Operation :" + op);

System.out.println("Type :" + type);

}

}

}

pfs = objtask.createSpec(taskmor);

ObjectContent[] ocs = _service.retrieveProperties(propcoll, pfs);

if(ocs!=null) {

objtask.displayTasks(ocs);

}

else {

System.out.println("Task list is null");

}

Hope this solves your issue!

Thanks.

Angela

Reply
0 Kudos