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

前端 未结 3 1363
执念已碎
执念已碎 2020-12-09 22:08

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 givi

相关标签:
3条回答
  • 2020-12-09 22:43

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

    0 讨论(0)
  • 2020-12-09 22:45

    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...
      }
    }
    
    0 讨论(0)
  • 2020-12-09 23:06

    I tried the following code, that I execute from my IApplication implementor start() method, before the RCP application is actually launched:

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            if (PlatformUI.isWorkbenchRunning()) {
                PlatformUI.getWorkbench().close();
            }
            logger.info("Shutdown request received");
            cleanup();
        }
    });
    

    Where cleanup() closes the connection to the database. Close should ask the users to save if there is any documents open.

    0 讨论(0)
提交回复
热议问题