Skip to main content

Sample Code for a Long-running Operation

This topic reviews the sample code for a long-running operation that is taken from the com.sabre.redapp.example.longrunning sample plug-in. This code is based on the first instance of Example  2 in the Eclipse Jobs API article. (For detailed information, see

The code is commented inline.

When the job has the following states, it has callbacks:

  • The job is scheduled.

  • The job is about to run.

  • The job is running.

  • The job is sleeping.

  • The job is awake.

The following sample code is taken from the com.sabre.redapp.example.longrunning sample plug-in.

package com.sabre.redapp.example.longrunning;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.BundleContext;

import com.sabre.edge.platform.core.common.plugin.base.AbstractEdgeBasePlugin;

 * Activator demonstrating long-running operation.
public class Activator extends AbstractEdgeBasePlugin {

   * {@inheritDoc}

  public void start(BundleContext context) throws Exception {

    /*  The purpose of the following line is to demonstrate the sample long-running operation. The loop is repeated 10 times. It simulates communication with the server by thread slipping. The job is trying to connect with the server 10 times, and each time takes several seconds. */
    final long max = 10;
    /* Create a new job with the name "Long Running Job" */

    final Job job = new Job("Long Running Job") {
    /* IProgressMonitor provides feedback to the end-user on the status of the job and checks for cancellation of the job. */

    /* The lines that begin with for (long i = 1; i < max; i) and end with System.err.println(e.getMessage())are included for the purpose of presenting and running this sample. */

    /* Replace these lines with your own code. */

    protected IStatus run(IProgressMonitor monitor) {

    /* Continue to run the job if i is less than max*/

    for (long i = 1; i < max; i) {
            System.out.println("Step: "  i  "/"  max);
            if (monitor.isCanceled())
              return Status.CANCEL_STATUS;
            try {              // Here we are simulating a long-running operation.
              /* Simulate the code for the long running operation */
              } catch (InterruptedException e) {
            /* Generally, you will implement the run method and return a status. return Status provides feedback to the end user */
        return Status.OK_STATUS;

/* Use IJobChangeListener to receive notification about changes to the job state.*/

    job.addJobChangeListener(new JobChangeAdapter() {
      public void done(IJobChangeEvent event) {
        /* Get the result and display a message that is based on the result. */
          if (event.getResult().isOK()) {
            System.out.println("Job completed successfully");
          } else {
            System.out.println("Job did not complete successfully");
        /* Set this job as a user job. There are 3 categories of jobs: systems, user and default.*/
        job.schedule(); // Start the job as soon as possible.