Using Eclipse Jobs API

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:

Jobs API View

3 comments

  1. I have waited so long for an update to this wonderful blog… but after 4 months of waiting, I can honestly say it was worth the effort. Radek, I wanna give birth to your children:)

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s