Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

3. Jobs Example

Create a new Eclipse RCP project "de.vogella.rcp.background.jobs" with the "Hello RCP" example. Create one command "de.vogella.rcp.background.jobs.runJob1" with the following default handler "de.vogella.rcp.background.jobs.RunJob1". This handler demonstrates the usage of Display.getDefault().asyncExec() which executes the runnable in the UI thread. If the job would try to directly modify the UI you would receive a "org.eclipse.swt.SWTException: Invalid thread access" error.

			
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.swt.widgets.Display;
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 {
						// We simulate a long running operation here
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("Doing something");
				}
				// Use this to open a Shell in the UI thread
				Display.getDefault().asyncExec(new Runnable() {
					public void run() {
						MessageDialog.openInformation(
								HandlerUtil.getActiveShell(event),
								"Your Popup ", "Your job has finished.");
					}
				});
				return Status.OK_STATUS;
			}

		};
		job.setUser(true);
		job.schedule();
		return null;
	}

}

		

In ApplicationWorkbenchWindowAdvisor you can activate the job progress bar via configurer.setShowProgressIndicator(true);

			    
public void preWindowOpen() {
    IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
    configurer.setInitialSize(new Point(400, 300));
    configurer.setShowCoolBar(false);
    configurer.setShowStatusLine(false);
    configurer.setTitle("Hello RCP");
    configurer.setShowProgressIndicator(true);
}

		

Add the command to the menu and run your application to see the Job API in action. A dialog is opened which can be send to the background.

Jobs are executed outside the UI thread so that the UI thread it not blocked. If you want to run a job in the UI thread you can use UIJob.

			
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.jface.dialogs.MessageDialog;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.progress.UIJob;

public class RunUiJob1 extends AbstractHandler {

	@Override
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		UIJob job = new UIJob("First Job") {
			@Override
			public IStatus runInUIThread(IProgressMonitor monitor) {
				for (int i = 0; i < 10; i++) {
					try {
						// We simulate a long running operation here
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("Doing something");
				}
				MessageDialog.openInformation(
						HandlerUtil.getActiveShell(event), "Your Popup ",
						"Your job has finished.");

				return Status.OK_STATUS;
			}

		};
		job.setUser(true);
		job.schedule();
		return null;
	}

}