Eclipse platform offers great API for managing long running operations in the background. Besides the API for starting, stopping and monitoring progress of the Job – there is also standard UI for managing all those operations. I’m going to present sample usage of Jobs API in you own plug-in/application.
Creating and scheduling Job may look like this:
TrainJob job = new TrainJob(TRAIN_JOB_NAME + file.getName(), classifier);
job.setRule(file);
job.setUser(true);
job.setPriority(Job.SHORT);
job.schedule(); // start as soon as possible
Sample of custom Job:
package pl.edu.agh.caracal.classifier.popup.jobs;
import static org.eclipse.core.runtime.Status.OK_STATUS;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.Job;
import pl.edu.agh.caracal.classifier.ext.classifiers.AbstractClassifier;
/**
* Class for execution of training of a classifier
*
* @since 1.0
*/
public class TrainJob extends Job {
private AbstractClassifier classifier;
/**
* Public constructor TrainJob
*
* @param name Train job name
* @param classifier Classifier to be trained
*/
public TrainJob(String name, AbstractClassifier classifier) {
super(name);
this.classifier = classifier;
}
@Override
protected IStatus run(IProgressMonitor monitor) {
// Long running operation - in this case classifier training
// ...
return OK_STATUS;
}
}
Standard GUI for presenting Job progress:
