Examples - Run An Existing Task (Programmatic)

This Java code sample shows how to use the programmatic API to run an existing task and interpret the results.

Setup

To run this example, you will need a task in MOVEit Automation called "Monthly Reports".

Example Run

Assuming you are in the samples directory

java -cp .;..\MICentralAPIJava.jar RunExistingTask

Starting task Monthly Reports
Task Monthly Reports has started
TaskName             = Monthly Reports
TaskID               = 133254349
TaskNominalStartTime = 2006-04-20 16:04:23
TaskTimeEnded        = 2006-04-20 16:04:24
TaskErrorCode        = 0
TaskErrorDescription =

--- Information about the task run as a whole: ---
TotalBytesSent = 36350

--- Information about the individual file transfers: ---
Sent c:\tmp\two\test1.ggg (33408 bytes)
Sent c:\tmp\two\test1.hhh (2942 bytes)

Code Excerpts

The main body of the example highlights how to start a task, given a task name. How to wait for that task, given the task handle from start. And how to use the result of wait to determine if the task has completed.

String taskName = "Monthly Reports";
showMsg("Starting task " + taskName);

// Start the task
String taskHnd = oAPI.startTask(taskName, true, "");
if (taskHnd.equals("")) {
	showMsg("Task " + taskName + " failed to start");
	showMsg(oAPI.getErrorDescription());
	return;
} else {
	showMsg("Task " + taskName + " has started");
}

int waitSecs = 120;
// Wait for the task to complete
WaitResult wresult = oAPI.waitForTasks(taskHnd, waitSecs);

// The task might not have completed within the timeout we selected.
if (wresult.isCompleted()) {
	showMsg("TaskName             = " + wresult.getTaskName());
	showMsg("TaskID               = " + wresult.getTaskID());
	showMsg("TaskNominalStartTime = " + wresult.getTaskNominalStartTime());
	showMsg("TaskTimeEnded        = " + wresult.getTaskTimeEnded());
	showMsg("TaskErrorCode        = " + wresult.getTaskErrorCode());
	showMsg("TaskErrorDescription = " + wresult.getTaskErrorDescription());

	showTaskRunInfo(oAPI, wresult);
	showTaskTransfers(oAPI, wresult);
} else {
	showMsg("The task has not yet completed");
}

Now query to get details about the run of the task. (XML result details)

private static void showTaskRunInfo(MICentralAPI oAPI, WaitResult wresult) {
	// Fetch the information from MOVEit Automation, in XML format.
	String strXML = oAPI.queryTaskRunInfo(wresult);

	Document docXML = parse_xml(strXML);
	showMsg("");
	showMsg("--- Information about the task run as a whole: ---");
	showMsg("TotalBytesSent = " + get_node_value_xpath(docXML.getDocumentElement(),
		"/Records/Record/TotalBytesSent"));
}

Or queryTaskRunXfers to get details about the files transfered. (XML result details)

private static void showTaskTransfers(MICentralAPI oAPI, WaitResult wresult) {
	// Fetch the information from MOVEit Automation, in XML format.
	String strXML = oAPI.queryTaskRunXfers(wresult);

	Document docXML = parse_xml(strXML);
	showMsg("");
	showMsg("--- Information about the individual file transfers: ---");

	// Loop through each file transfer record. Remember that there may be multiple
	// records per transfer, in particular, a "get" and a "send" record for each.
	NodeList nodes = get_nodes_xpath(docXML.getDocumentElement(), "/Records/Record");
	if ((nodes != null) && (nodes.getLength() > 0)) {
		String action = "";
		for (int i=0;i<nodes.getLength();i++) {
			Node node = nodes.item(i);
			action = get_subnode_value((Element)node,"Action");
			// List only send records
			if (action.equals("send")) {
				showMsg("Sent " +
					get_subnode_value((Element)node,"DestFilename") +
					" (" +
					get_subnode_value((Element)node,"NBytes") +
					")");
			}
		}
	}
}