protected TaskInfo getTaskInfo(VMwareClient vmw, VMPropertyHandler paramHandler) throws Exception { String instanceId = paramHandler.getInstanceName(); String taskKey = paramHandler .getServiceSetting(VMPropertyHandler.TASK_KEY); logger.debug("VM: " + instanceId + " taskId: " + taskKey); if (taskKey == null || "".equals(taskKey)) { return null; } VimPortType service = vmw.getConnection().getService(); ManagedObjectReference taskManager = vmw.getConnection() .getServiceContent().getTaskManager(); ManagedObjectReference taskHistoryCollector = service .createCollectorForTasks(taskManager, new TaskFilterSpec()); try { service.resetCollector(taskHistoryCollector); service.readNextTasks(taskHistoryCollector, 100); List<TaskInfo> taskList = vmw.getConnection().getService() .readPreviousTasks(taskHistoryCollector, 100); if (taskList != null) { for (TaskInfo taskInfo : taskList) { if (taskInfo != null && taskKey.equals(taskInfo.getKey())) { logTaskInfo(taskInfo); return taskInfo; } } } logger.error("Task not found. VM: " + instanceId + " taskId: " + taskKey); return null; } finally { service.destroyCollector(taskHistoryCollector); } }
static TaskFilterSpec createTaskFilterSpec(ManagedEntity ent) { TaskFilterSpec tfs = new TaskFilterSpec(); // only the root initiated tasks TaskFilterSpecByUsername nameFilter = new TaskFilterSpecByUsername(); nameFilter.setUserList(new String[] {"Administrator"}); // include tasks initiated by non-users, // for example, by ScheduledTaskManager. nameFilter.setSystemUser(true); tfs.setUserName(nameFilter); // only the tasks with one entity itself TaskFilterSpecByEntity entFilter = new TaskFilterSpecByEntity(); entFilter.setEntity(ent.getMOR()); entFilter.setRecursion(TaskFilterSpecRecursionOption.all); tfs.setEntity(entFilter); // only successfully finished tasks tfs.setState(new TaskInfoState[]{TaskInfoState.success }); // only tasks started within last one month // strictly speaking, time should be retrieved from server TaskFilterSpecByTime tFilter =new TaskFilterSpecByTime(); Calendar cal = Calendar.getInstance(); cal.roll(Calendar.MONTH, -1); tFilter.setBeginTime(cal); //we ignore the end time here so it gets the latest. tFilter.setTimeType(TaskFilterSpecTimeOption.startedTime); tfs.setTime(tFilter); // Optionally, you limits tasks initiated by scheduled task // with the setScheduledTask() method. return tfs; }
public static void main(String[] args) throws Exception { if(args.length != 3) { System.out.println("Usage: java TaskHistoryMonitor " + "<url> <username> <password>"); return; } ServiceInstance si = new ServiceInstance( new URL(args[0]), args[1], args[2], true); TaskManager taskMgr = si.getTaskManager(); if(taskMgr!=null) { Folder root = si.getRootFolder(); TaskFilterSpec tfs = createTaskFilterSpec(root); TaskHistoryCollector thc = taskMgr.createCollectorForTasks(tfs); // Note: 10 <= pagesize <= 62 thc.setCollectorPageSize(15); System.out.println("Tasks in latestPage:"); TaskInfo[] tis = thc.getLatestPage(); printTaskInfos(tis); System.out.println("\nAll tasks:"); int total = 0; while(true) { tis= thc.readNextTasks(50); if(tis==null) { break; } total += tis.length; printTaskInfos(tis); } System.out.println("\nTotal number " + "of tasks retrieved:" + total); thc.destroyCollector(); } si.getServerConnection().logout(); }
TaskHistoryCollector createCollectorForTasks(TaskFilterSpec filter) throws InvalidState, RuntimeFault, RemoteException;