2011-06-21 41 views
1

所以我發起了一個乾淨的OSGI關機 Best way to shutdown an OSGi Container (specifically equinox) 我使用bundle.stop()方法來實現相同。 現在問題出現了,如果我調用一個bundle.stop()以防發生某些嚴重故障時,執行乾淨關閉意味着我有一個進程退出代碼爲0,是否有任何方法可以發出退出代碼1從調用bundle.stop()之後的進程中,以便進程使用者知道這不是正常關閉?控制OSGI關機退出代碼

謝謝!

回答

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

上面的代碼不採取一對一的,但它給了理念。

+0

謝謝Danail,我做了一個有點類似的實現,設置系統屬性,監聽它並進一步傳播退出代碼 – javaresearcher