[英]Cannot start bundle programmatically
注意 :就我而言,如果需要的話,我正在使用Apache Felix
實現。
我已經寫了捆綁軟件,我將其用作測試。 這是一個非常簡單的“ Hello World”捆綁軟件,僅在啟動/停止時向stdout
打印消息:
public class Activator implements BundleActivator {
@Override
public void start(BundleContext context) throws Exception {
System.out.println("Hello, World.");
}
@Override
public void stop(BundleContext context) throws Exception {
System.out.println("Goodbye, World.");
}
}
還有一個MANIFEST
文件,它毫無意義地發布,因為當我通過Apache Felix
控制台從標准發行版( 可在此處下載 )部署以上捆綁軟件時,捆綁軟件啟動並打印出消息。
我要嘗試的下一步是使用編程方法部署相同的捆綁軟件。 不幸的是,這不適用於我。 我的代碼如下:
public static void main(String[] args) throws Exception {
FrameworkFactory frameworkFactory = getFrameworkFactory();
Framework framework = frameworkFactory.newFramework(null);
System.out.println("BundleID = " + framework.getBundleId());
System.out.println("State = " + getState(framework.getState()));
framework.init();
System.out.println("BundleID = " + framework.getBundleId());
System.out.println("State = " + getState(framework.getState()));
BundleContext bundleContext = framework.getBundleContext();
bundleContext.addBundleListener((event) -> {
System.out.println("Bundle Changed Event");
});
bundleContext.addFrameworkListener((event) -> {
System.out.println("Framework Event");
});
bundleContext.addServiceListener((event) -> {
System.out.println("Service Changed Event");
});
Bundle bundle = bundleContext.installBundle("file://<absolute-path-to-bundle-jar-same-as-above");
System.out.println("BundleID = " + bundle.getBundleId());
System.out.println("State = " + getState(bundle.getState()));
bundle.start();
System.out.println("BundleID = " + bundle.getBundleId());
System.out.println("State = " + getState(bundle.getState()));
}
private static FrameworkFactory getFrameworkFactory() throws IllegalStateException {
ServiceLoader<FrameworkFactory> loader = ServiceLoader.load(FrameworkFactory.class);
FrameworkFactory factory = null;
for (FrameworkFactory iterator : loader) {
if (factory != null) {
throw new IllegalStateException("Ambiguous SPI implementations.");
}
factory = iterator;
}
return factory;
}
private static String getState(int state) {
switch (state) {
case Bundle.UNINSTALLED:
return "UNINSTALLED";
case Bundle.INSTALLED:
return "INSTALLED";
case Bundle.RESOLVED:
return "RESOLVED";
case Bundle.STARTING:
return "STARTING";
case Bundle.STOPPING:
return "STOPPING";
case Bundle.ACTIVE:
return "ACTIVE";
default:
throw new IllegalStateException("Unknown state");
}
}
輸出如下:
BundleID = 0
State = INSTALLED
BundleID = 0
State = STARTING
Bundle Changed Event
BundleID = 1
State = INSTALLED
BundleID = 1
State = INSTALLED
據我了解,bundle已安裝,但最后四行表明bundle.start()
由於某種原因而被忽略。
您能否指出我要進行這項工作的缺失?
經過一小時的調試和更仔細地閱讀javadoc
后,由於框架僅被初始化而不是被啟動,因此發生了這種情況。 為了使例子工作,你必須簡單地添加framework.start()
后framework.init()
或只需調用framwork.start()
這就要求framework.init()
如果認為有必要)。
我將保留這些信息,因為幾乎沒有什么令人困惑的事情:
Apache Felix
官方文檔提供了有關將框架嵌入主機應用程序的信息。 不幸的是,只有使用Apache Felix
自定義機制的示例使其無法移植到其他實現中。 令人困惑的是警告說明 ,如果要創建可移植的解決方案,則應使用init()
和getBundleContext()
。 整個注釋引述如下: 警告
felix.systembundle.activators
配置屬性特定於Felix框架實現。 如果希望代碼與其他框架實現一起使用,則應在框架實例上調用init()
並直接使用getBundleContext()
。 否則,方法將非常相似。
init()
方法的無參數版本沒有提及初始化與啟動框架不一樣,盡管init(FrameworkListener...)
具有此類信息。 直到調用start,該框架才會真正啟動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.