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

独自空忆成欢 提交于 2019-12-17 19:03:14

问题


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?


回答1:


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



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/515993/what-is-the-correct-way-to-add-a-shutdown-hook-for-an-eclipse-rcp-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!