代码之家  ›  专栏  ›  技术社区  ›  Martin

如何在停止OSGi框架之前销毁所有OSGi蓝图组件?

  •  1
  • Martin  · 技术社区  · 6 年前

    如果停止OSGi框架,OSGi蓝图组件不会被销毁(不会调用destroy方法)。为什么会这样,我怎样才能摧毁他们。我有自己的OSGi启动程序实现。我正在使用关闭挂钩,然后停止OSGi框架:

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            Launcher.logService.log(LogService.LOG_WARNING, "HANDLE SHUTDOWN");
            if(Launcher.framework != null) {
                try {
                    Launcher.framework.stop();
                } catch (BundleException e) {
                    Launcher.logService.log(LogService.LOG_ERROR, "Shutdown" + ((Launcher.framework != null) ? " of " + Launcher.framework.getSymbolicName() + " " : " ") + "failed!", e);
                    System.exit(-1);
                }
            }
        }
    });
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Martin    6 年前

    我们应该等待框架停止。

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            Launcher.logService.log(LogService.LOG_WARNING, "HANDLE SHUTDOWN");
            if(Launcher.framework != null) {
                try {
                    Launcher.framework.stop();
                    Launcher.framework.waitForStop(0); // !!!
                } catch (BundleException e) {
                    Launcher.logService.log(LogService.LOG_ERROR, "Shutdown" + ((Launcher.framework != null) ? " of " + Launcher.framework.getSymbolicName() + " " : " ") + "failed!", e);
                    System.exit(-1);
                }
            }
        }
    });