Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to add a Shutdown Hook for an Eclipse RCP application?

I have an RCP application that uses a connection to a in-memory database. There is one circumstance that, when shutting down windows, the application is killed without giving it a chance to close the connection to the database.

I researched a little and it seems that adding a Shutdown hook is the best way to detect this event and do cleanup in a Java application. However, what is the correct way to do process this if you have an RCP application, possibly with multiple editors open?

like image 381
Mario Ortegón Avatar asked Feb 05 '09 13:02

Mario Ortegón


2 Answers

Note: this blog entry suggests the following implementation for the shutdown hook:

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.

(so not exactly your scenario, but the implementation is still interesting in that it shows how to run it within the UI thread)

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
    }
  }
}

It is set, as you said, in the IApplication:

public class IPEApplication implements IApplication {
  public Object start(IApplicationContext context) throws Exception {
    final Display display = PlatformUI.createDisplay();
    Runtime.getRuntime().addShutdownHook(new ShutdownHook());  }
    // start workbench...
  }
}
like image 51
VonC Avatar answered Sep 27 '22 22:09

VonC


You should override the preShutdown method on your class that extends WorkbenachAdvisor. Return false to halt the shutdown process or true to continue.

like image 24
mxc Avatar answered Sep 28 '22 00:09

mxc