OSGI 生命周期
1 生命周期管理
對于非模塊化應(yīng)用,生命周期將應(yīng)用作為一個整體來操作; 而對于模塊化應(yīng)用,則可以以細(xì)粒度的方式來管理應(yīng)用的某一個獨立部分。OSGi生命周期管理
OSGi生命周期層有兩種不同的作用:
- 在應(yīng)用程序外部,定義了對bundle生命周期的相關(guān)操作。OSGi生命周期層允許在執(zhí)行時,從外部安裝、啟動、更新、停止、卸載不同的bundle,進(jìn)而定制應(yīng)用的配置。
- 在應(yīng)用程序內(nèi)部,定義了bundle訪問其執(zhí)行上下文的方式,為bundle提供了一種與OSGi框架交互的途徑以及一些執(zhí)行時的便利條件。
標(biāo)準(zhǔn)Java生命周期管理
安裝:jar下載時被安裝; 執(zhí)行:用戶啟動JVM時被執(zhí)行;(雙擊) 更新:替換jar文件; 移除:刪除jar文件。Servlet生命周期管理
生命周期由servlet容器管理
安裝:通過容器的特定進(jìn)程被安裝; 執(zhí)行:servlet容器調(diào)用各種生命周期API,如Servlet.init()、Servlet.destroy(); 更新:替換WAR文件; 移除:通過容器的特定進(jìn)程被移除。2 生命周期層的三個接口
BundleActivator
如果要對一個bundle進(jìn)行生命周期管理,必須要在這個bundle中聲明一個bundle激活器類,實現(xiàn)org.osgi.framework.BundleActivator接口。這個接口為bundle提供了掛接到生命周期層的鉤子,同時自定義bundle在啟動或停止時執(zhí)行的操作。還必須在MANIFEST.MF中描述該bundle激活器:
?
Bundle-Activator:?org.alpha.MyActivator?? 注意:并非所有bundle都需要一個激活器。只有當(dāng)明確需要與OSGI API進(jìn)行交互,或者需要執(zhí)行自定義的初始化/銷毀動作時,才必須要激活器。- 當(dāng)bundle被安裝并啟動以后,框架將構(gòu)建相應(yīng)的BundleActivator的一個實例,觸發(fā)BundleActivator.start();
- 當(dāng)bundle停止后,框架會調(diào)用BundleActivator.stop();stop()方法應(yīng)該取消start()中執(zhí)行過的所有操作。
?
public???interface??BundleActivator {?????public???void??start(BundleContext context)??throws??Exception;??
???public???void??stop(BundleContext context)??throws??Exception;??
}??
?
BundleContext
BundleActivator的兩個方法都接受一個BundleContext對象作為參數(shù)。BundleContext是bundle與OSGi framework通信的橋梁。BundleContext可以做的事有:
- Look up system-wide configuration properties;
——獲取系統(tǒng)properties - Find another installed bundle by its ID;
——通過ID查詢已安裝的其他bundle - Obtain a list of all installed bundles;
——獲取已安裝的bundle列表 - Introspect and manipulate other bundles programmatically: start them, stop them, un-install them, update them, etc;
——在程序中操作其他bundle(啟動、停止、卸載、更新) - Install new bundles programmatically;?
——在程序中安裝新bundle - Store or retrieve a file in a persistent storage area managed by the framework;?
——往框架管理的持久化存儲區(qū)中存儲或查詢文件 - Register and unregister bundle listeners, which tell us when the state of any bundle in the framework changes;?
——注冊bundle listener,監(jiān)聽bundle狀態(tài)的改變 - Register and unregister service listeners, which tell us when the state of any service in the framework changes?
——注冊service listener,監(jiān)聽service狀態(tài)的改變? - Register and unregister framework listeners, which tell us about general framework events.
——注冊framework listener,監(jiān)聽一般框架事件
每個已激活的bundle都會接收到屬于自己的BundleContext對象,該對象不能在bundle直接自由傳遞!
只有當(dāng)bundle處于激活狀態(tài)(從start()開始,到stop()結(jié)束),BundleContext對象才是有效的。其他狀態(tài)下調(diào)用該對象,會拋出異常。
?
public??interface??BundleContext {????String getProperty(String key);??
??Bundle getBundle();??
??Bundle getBundle(?long??id);??
??Bundle[] getBundles();??
??
??Bundle installBundle(String location)??throws??BundleException;??
??Bundle installBundle(String location, InputStream input)??throws??BundleException;??
??
???void??addBundleListener(BundleListener listener);??
???void??removeBundleListener(BundleListener listener);??
???void??addFrameworkListener(FrameworkListener listener);??
???void??removeFrameworkListener(FrameworkListener listener);??
}??
?
Bundle
對于每個已安裝的bundle,框架都會相應(yīng)地創(chuàng)建一個邏輯上代表他的Bundle對象。Bundle接口定義了一系列API,用于管理已安裝bundle的生命周期。
?
public?interface?Bundle {???? BundleContext getBundleContext();??
???long??getBundleId();??
?? Dictionary getHeaders();??
???int??getState();??
?? String getSymbolicName();??
?? Version getVersion();??
??
???void??start(?int??options)??throws??BundleException;??
???void??start()??throws??BundleException;??
???void??stop(?int??options)??throws??BundleException;??
???void??stop()??throws??BundleException;??
???void??update(InputStream input)??throws??BundleException;??
???void??update()??throws??BundleException;??
???void??uninstall()??throws??BundleException;??
}??
注意:BundleId=0的bundle表示OSGi框架本身,稱為系統(tǒng)bundle。
當(dāng)停止系統(tǒng)bundle時,它會首先停止其他bundle,然后才將自身完全關(guān)閉;以友好的方式關(guān)閉框架。
3 生命周期狀態(tài)
INSTALLED
調(diào)用BundleContext.installBundle()后,會創(chuàng)建一個INSTALLED狀態(tài)的bundle
RESOLVED
如果其所依賴的所有bundle都存在,即解析成功,轉(zhuǎn)到RESOLVED狀態(tài)
注意:不能直接從INSTALLED --> STARTING
STARTING
當(dāng)開始執(zhí)行BundleActivator.start(),則處于STARTING狀態(tài);
注意:這是一個瞬時狀態(tài)
ACTIVE
當(dāng)執(zhí)行BundleActivator.start()成功,則轉(zhuǎn)到ACTIVE狀態(tài);
如果拋出異常,則回到RESOLVED狀態(tài)。
STOPPING
當(dāng)開始執(zhí)行BundleActivator.stop(),則處于STOPPING狀態(tài);
如果執(zhí)行成功,則轉(zhuǎn)到RESOLVED狀態(tài)。 注意:這是一個瞬時狀態(tài)UNINSTALLED
INSTALLED狀態(tài)的bundle可以被卸載,轉(zhuǎn)到UNINSTALLED狀態(tài);
如果卸載一個ACTIVE狀態(tài)的bundle,則框架會首先自動停止該bundle,使其轉(zhuǎn)到RESOLVED狀態(tài);然后再卸載之前,將其狀態(tài)轉(zhuǎn)為INSTALLED。
Although the UNINSTALLED state is shown here, we can never see a bundle in that state, and?an UNINSTALLED bundle cannot transition to any other state. Even if we reinstall the same bundle JAR file, it will be considered a different bundle by the framework, and assigned a new bundle ID.?刷新/更新
刷新或更新一個bundle,將使其狀態(tài)退回INSTALLED狀態(tài)!
轉(zhuǎn)載于:https://www.cnblogs.com/deepbreath/p/4383059.html
總結(jié)
- 上一篇: php文件操作基本使用方法
- 下一篇: 关于笔试的一些博客