5

I use org.eclipse.core.runtime.jobs.Job to execute stored procedure which deletes data and to update user interface according to the new data. Thus it is important that this job will be completed even if user closes eclipse application.

final Job execStoredProcJob = new Job(taskName) {
    protected IStatus run(IProgressMonitor monitor) {
        monitor.beginTask(taskName, 
// execute stored procedure
// update user interface
        monitor.done();
        return Status.OK_STATUS;
    }
};

execStoredProcJob.schedule();

When I close eclipse app while the Job still running it seems to kill the Job. How to complete the job after user has closed eclipse app? Is it possible?

ka3ak
  • 2,435
  • 2
  • 30
  • 57
  • Possible duplicate of http://stackoverflow.com/questions/515993/what-is-the-correct-way-to-add-a-shutdown-hook-for-an-eclipse-rcp-application – katsharp Jan 25 '12 at 09:04

2 Answers2

2

I think you might want to take a look at scheduling rules http://www.eclipse.org/articles/Article-Concurrency/jobs-api.html

execStoredProcJob.setRule([Workspace root]);
execStoredProcJob.schedule();

[Workspace root] can be attained like project.getWorkspace().getRoot() if you have a reference to your project.

That will block all jobs that require the same rule. The shutdown job is one of them..

It's also possible to:

IWorkspace myWorkspace = org.eclipse.core.resources.ResourcesPlugin.getWorkspace();

Then use:

myWorkspace.getRoot();
Enrico
  • 621
  • 1
  • 4
  • 12
  • A workspace wide scheduling rule might be a bit drastic as it will also block other stuff from running before the shutdown of the workbench. Figure out the smallest possible rule based on resources and use that. Remember all `IResource` are also `ISchedulingRule`. – Tonny Madsen Jan 25 '12 at 10:10
  • I agree. This page http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fruntime_jobs.htm has a header about completing jobs before shutdown. It suggests you make sure all jobs are complete in your plugin's "stop" method, that has to finish before eclipse exists. – Enrico Jan 25 '12 at 10:48
  • Be careful with this method. You might not know which plug-ins that have _already_ stopped when your plug-in is stopped and some of the infra structure you depend on might have disappeared. Also, this might require that a core plug-in needs UI access to properly show the waiting state... – Tonny Madsen Jan 25 '12 at 10:58
  • So how does it work? Should you accept the answer now or something? I'm working on my reputation here ;) – Enrico Jan 25 '12 at 11:58
  • What would be great too if I could display dialog with job progress after user has closed eclipse app. Like after you have closed Eclipse IDE the dialog "Saving workbench state..." is displayed. – ka3ak Jan 25 '12 at 12:07
  • I tried to just close eclipse while I had a job with a workspace root scheduling rule still running and Eclipse did what you mentioned for me. It shutdown the UI but left a progress window with the name of the running task. Do you add tasks to the monitor? – Enrico Jan 25 '12 at 19:11
  • I use the same code as above. I have only added the instruction execStoredProcJob.setRule(org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot()); before execStoredProcJob.schedule(); When I close the app I see that job still executing but I don't see any progress dialog. – ka3ak Jan 26 '12 at 10:14
  • I'm using indigo.. Open the details. I see this: http://i42.tinypic.com/11mgdgy.png – Enrico Jan 26 '12 at 13:13
0

An alternative to scheduling rules is to add code in WorkbenchAdvisor.preShutdown() that will join any outstanding job you have...

Tonny Madsen
  • 12,628
  • 4
  • 31
  • 70