Our IT group wondered if our Eclipse RCP based application could handle a linux system shutdown. They kept catching hell for rebooting computers at night to keep the batch processing system running. Our users would lose unsaved work and get grumpy. Grumpy seismic processors are not a pretty sight.
The trick is to teach the Eclipse RCP application to listen for a SIGTERM by adding a shutdown hook.
Please note that this does not work on Windows. More about this in a future blog.
public class IPEApplication implements IApplication { public Object start(IApplicationContext context) throws Exception { final Display display = PlatformUI.createDisplay(); Runtime.getRuntime().addShutdownHook(new ShutdownHook()); } // start workbench... } }
The shutdown code must be run in the UI thread and should not be run if the workbench is being closed by other means. All dirty editors are automatically saved. This avoids prompting the user who is probably at home sleeping when their computer is shutdown. Finally the workbench is closed.
private class ShutdownHook extends Thread { @Override public void run() { try { final IWorkbench workbench = PlatformUI.getWorkbench(); final Display display = PlatformUI.getWorkbench() .getDisplay(); if (workbench != null && !workbench.isClosing()) { display.syncExec(new Runnable() { public void run() { IWorkbenchWindow [] workbenchWindows = workbench.getWorkbenchWindows(); for(int i = 0;i < workbenchWindows.length;i++) { IWorkbenchWindow workbenchWindow = workbenchWindows[i]; if (workbenchWindow == null) { // SIGTERM shutdown code must access // workbench using UI thread!! } else { IWorkbenchPage[] pages = workbenchWindow .getPages(); for (int j = 0; j < pages.length; j++) { IEditorPart[] dirtyEditors = pages[j] .getDirtyEditors(); for (int k = 0; k < dirtyEditors.length; k++) { dirtyEditors[k] .doSave(new NullProgressMonitor()); } } } } } }); display.syncExec(new Runnable() { public void run() { workbench.close(); } }); } } catch (IllegalStateException e) { // ignore } } }
2 comments:
Very Useful Blog.Keep up the good work.
Is there a way to accomplish the same on windows? You alluded to a future entry on this :-)
Post a Comment