【问题标题】:Control over exit codes in a OSGI shutdown控制 OSGI 关闭中的退出代码
【发布时间】:2011-09-19 20:11:55
【问题描述】:

所以我启动了一个干净的 OSGI 关闭 Best way to shutdown an OSGi Container (specifically equinox) 我使用 bundle.stop() 方式来实现相同的目的。 现在问题出现了,如果我调用 bundle.stop() 以防发生某些严重故障,执行干净关闭意味着我的进程退出代码为 0,有什么方法可以发送退出代码为 1调用 bundle.stop() 后的进程,以便进程使用者知道这不是正常关闭?

谢谢!

【问题讨论】:

    标签: osgi exit-code


    【解决方案1】:

    您应该使用org.eclipse.equinox.app.IApplication 接口,它使您能够从start() 方法返回结果,然后作为退出代码从Java 进程返回。如果您不想使用此 API,以下代码显示 Equinox 本身如何控制 Java 进程的退出代码:

    import org.eclipse.osgi.service.environment.EnvironmentInfo;
    
    private static EnvironmentInfo getEnvironmentInfo() {
        BundleContext bc = Activator.getContext();
        if (bc == null)
            return null;
        ServiceReference infoRef = bc.getServiceReference(EnvironmentInfo.class.getName());
        if (infoRef == null)
            return null;
        EnvironmentInfo envInfo = (EnvironmentInfo) bc.getService(infoRef);
        if (envInfo == null)
            return null;
        bc.ungetService(infoRef);
        return envInfo;
    }
    
    
    public static void setExitCode(int exitCode) {
        String key = "eclipse.exitcode";
        String value = Integer.toString(exitCode); // the exit code
        EnvironmentInfo envInfo = getEnvironmentInfo();
        if (envInfo != null)
            envInfo.setProperty(key, value);
        else
            System.getProperties().setProperty(key, value);
    }
    

    上面的代码不是一一对应的,而是给出了思路。

    【讨论】:

    • 感谢 Danail,我做了一个类似的实现,设置系统属性,监听它并进一步传播退出代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多