SimpleTask
From Java CoG Kit
Contents |
Introduction
The SimpleTask package is essentially an abstraction above the abstraction package in the Java CoG Kit. Presently, SimpleTask is used by the Directory Browser, Grid Shell and Image Viewer. Originally developed specifically for the Grid Shell, SimpleTask was transformed and renamed to meet the needs of other Gridfaces. SimpleTask was developed in parallel with the directory browser, so many of the new features grew out of some practical need that arose while developing a client to the API.
The SimpleTask package is aimed at simplifying file operations (i.e. ls, cd, mkdir, etc), and also contains tasks for the remote execution of jobs and file transfers (including third party transfers). Additionally, the SimpleTask package contains it's own simplified version of task graphs, enabling end users to create large units of work from smaller, inter-dependent tasks.
Using Simple Tasks
Task Submission
There are three different ways to submit a task in the Simple Task package.
submitTaskAsync()
This method returns immediately, ensuring that task submission and execution occurs in a separate thread, thus leaving the current thread to continue its work.
task.initTask(); task.submitTaskAsync(); //Immediately continue with work.
submitTaskAndWait()
This method causes the thread to block until the submission AND execution of the task has completed.
task.initTask(); task.submitTaskAndWait(); //output now available: Object output = task.getOutput();
submitTask()
task.initTask(); task.submitTask();
Status Listeners
In order to monitor a task's status when it is submitted asynchronously through submitTaskAsync() or perhaps submitTask(), it is important to assign the task a status listener. A class that is to be a status listener should implement the interface org.cogkit.abstraction.interfaces.StatusListener. The listener will be notified whenever the status of a task changes. These statuses include, complete, failed, canceled and more.
Inside the status listener you can obtain a reference to the original task or retreive its output. Also, using attributes you can assign objects to a task that may be accessed later in the status listener (see example below).
Example
StatusListener listener = new myStatusListener();
PwdTask pwdTask= new PwdTask(connectionTask);
pwdTask.addStatusListener(listener);
pwdTask.initTask();
pwdTask.submitTaskAsync();
SimpleTask lsTask = new LsTask(connectionTask);
lsTask.addStatusListener(listener);
lsTask.initTask();
lsTask.submitTaskAsync();
...
private class myStatusListener implements StatusListener {
public void statusChanged(StatusEvent event) {
int statusCode = event.getStatus().getStatusCode();
SimpleTask task = (SimpleTask) event.getSource();
if(statusCode == Status.COMPLETED) {
System.out.println(task.getName() + " complete. Result is: ");
System.out.println(task.getResult());
} else {
System.out.println(task.getName() +" has status " + event.getStatus().getStatusString());
}
}
}
Exception Handling
More Information
For some more sophisticated examples of the using the Simple Task package, it may be useful to look at [the code for the directory browser]. In particular, the [TaskDispatcher class] may be of interest.
More information may be found in the Java CoG Kit Abstraction Guide.
File Operation Tasks
File operation tasks are tasks executed on your local machine or a remote server that create, delete, copy, move, rename or obtain information about files. A list of existing file operation tasks follows:
Example
Object result = null;
PostStartAbstractFileOpTask operation;
StartTask connection = new StartTask(null,"local","localhost",0);
connection.initTask();
result = connection.submitTaskAndWait();
logger.debug("Result: " + result);
operation = new LsTask(connection);
operation.initTask();
result = operation.submitTaskAndWait();
logger.debug("Result: " + result);
operation = new PwdTask(connection);
operation.initTask();
result = operation.submitTaskAndWait();
logger.debug("Result: " + result);
File Transfer Task
The File Transfer Task allow you to transfer files between machines. The transfer may be between your local machine and remote server. Also, the transfer my be between two remote machines, in which case a third party transfer will be initiated if the protocol supports it (i.e. gridftp). The File Transfer task will also work from your local machine to your local machine, in which case a simple copy will be performed.
Example
FileTransferTask transfer = new FileTransferTask("file://localhost/my_file.txt", "ftp://my.ftp.site.com/some/directory/");
transfer.initTask();
transfer.submitTask();
Remote Execution Task
The remote execution task allows you to run an arbitrary command on a remote machine.
Example
AbstractTask exec = new ExecTask("gt2","wiggum.mcs.anl.gov",-1,"/bin/ls","-l");
exec.initTask();
exec.submitTask();
exec.submitAndWait();
System.out.println(exec.getResult());
Extending Tasks
Simple Task Graphs
The SimpleTask package implements a simpler version of the task graphs outlined by the CoG Kit abstraction package. Relying the TaskGraph implemenation inside the abstractions package, but hiding most of the details behind an interface, SimpleTask graphs are quite easy to use. All you do is create some tasks and a graph, and add the tasks to the graph.
SimpleTask task = new LsTask(connection); SimpleTaskGraph graph = new SimpleTaskGraph(); graph.add(task);
To add dependent tasks:
graph.add(task1); graph.add(task2); graph.add(task3); graph.addDependency(task1, task2); graph.addDependency(task2, task3);
Or, if you just have a simple ordering of dependencies(i.e. task3 depends on task2 depends on task1) you can simply:
graph.addDependentTasks(task1, task2, task3); //tasks added to graph AND dependencies created
