vogella.de

Follow me on twitter
About Lars Vogel
Flattr this

Eclipse Job API in RCP - Tutorial

Lars Vogel

Version 0.3

16.08.2009

Revision History
Revision 0.112.07.2009Lars Vogel
Created
Revision 0.215.07.2009Lars Vogel
Updated resources
Revision 0.316.08.2009Lars Vogel
Progress Report

Eclipse Jobs

This article describes how to use the Job API in Eclipse RCP for asynchronous tasks.


Table of Contents

1. Overview
2. Example
3. Progress report
4. Thank you
5. Questions and Discussion
6. Links and Literature
6.1. Eclipse Jobs resources
6.2. Source Code
6.3. vogella Resources

1. Overview

The Eclipse Jobs API provides background capabilities to Eclipse with can also be used in Eclipse RCP.

The important parts of the Job API are:

  • JobManager - Schedules the jobs

  • Job - The individual task to perform

  • IProgresMonitor - UI for Job information

Using the Jobs API in Eclipse RCP is the same as in Eclipse plug-in development therefore I just give a small example. Check the appendix for a general introduction to the Eclipse Jobs API.

2. Example

The following assumes that you are familiar with Eclipse RCP development .

Create a new Eclipse RCP project "de.vogella.jobs.first" with the "Hello RCP" example. Create one command "de.vogella.jobs.first.runJob1" with the following default handler.

			
package de.vogella.jobs.first.handler;


import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.handlers.HandlerUtil;

public class RunJob1 extends AbstractHandler {

	@Override
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		Job job = new Job("First Job"){
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				
				for (int i=0; i<10; i++){
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("Doing something");
				}
				HandlerUtil.getActiveShell(event).getDisplay().getDefault().asyncExec(new Runnable() {
		               public void run() {
		            	   MessageDialog.openInformation(HandlerUtil.getActiveShell(event), "Info", "Info for you");
		               }
				});
				

				

				return Status.OK_STATUS;
			}
			
		}; 
		
		job.schedule();
		return null;
	}

}

		

Add the command to the menu.

Run your application to see the Job API in action.

3. Progress report

If you perform long running operations you should provide the user some information about the long running job. A good way of doing this is to show a progress indicator or a progress dialog.

Create a new project "de.vogella.rcp.intro.progress" with "Hello RCP" as a template. Create the command "de.vogella.rcp.intro.progress.showDialog" with the default handler "de.vogella.rcp.intro.progress.handler.ShowDialog". Create the handler class.

			
package de.vogella.rcp.intro.progress.handler;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.handlers.HandlerUtil;

public class ShowDialog extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		ProgressMonitorDialog dialog = new ProgressMonitorDialog(HandlerUtil
				.getActiveShell(event).getShell());
		try {
			dialog.run(true, true, new IRunnableWithProgress() {
				@Override
				public void run(IProgressMonitor monitor) {
					monitor
							.beginTask("Doing something timeconsuming here",
									100);
					for (int i = 0; i < 10; i++) {
						if (monitor.isCanceled())
							return;
						monitor.subTask("I'm doing something here " + i);
						sleep(1000);
						monitor.worked(i);
					}
					monitor.done();
				}
			});
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		return null;
	}

	private void sleep(Integer waitTime) {
		try {
			Thread.sleep(waitTime);
		} catch (Throwable t) {
			System.out.println("Wait time interrupted");
		}
	}

}

		

Add this command to the menu.

Run the application. Your command should open a dialog which show the process.

4. Thank you

Thank you for practicing with this tutorial.

I maintain this tutorial in my private time. If you like the information please help me by using flattr or donating or by recommending this tutorial to other people.

Flattr this

5. Questions and Discussion

Before posting questions, please see the vogella FAQ . If you have questions or find an error in this article please use the www.vogella.de Google Group . I have created a short list how to create good questions which might also help you. .

6. Links and Literature

6.1. Eclipse Jobs resources

http://www.eclipse.org/articles/Article-Concurrency/jobs-api.html Eclipse Jobs API

6.2. Source Code

Source Code of Examples