新聞中心
OSGi即Java模塊系統(tǒng),而OSGi bundle則是OSGi中軟件發(fā)布的形式。本文講述OSGi應(yīng)用中如何自動啟動bundle。作者最近開發(fā)了一個 OSGi 的應(yīng)用,部署之后發(fā)現(xiàn),當(dāng)應(yīng)用啟動的時候,幾乎所有 bundle 都處于 Resolved 狀態(tài),而不是 Started 狀態(tài)。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供安州網(wǎng)站建設(shè)、安州做網(wǎng)站、安州網(wǎng)站設(shè)計、安州網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、安州企業(yè)網(wǎng)站模板建站服務(wù),十多年安州做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
編輯推薦:OSGi入門與實踐全攻略
怎樣啟動bundle 呢?有如下幾種方法 :
1. 手工啟動bundle,即在 console 中使用命令 start N 來逐個啟動所有bundle,其中 N 表示每個 bundle 的 id
這種方法過于麻煩,要耗費大量時間,因此不可取。
2.在配置文件中聲明為自動啟動bundle。在 WEB-INF\eclipse\configuration 中的 config.ini 中,如下配置:
osgi.bundles=bundle1@start, bundle2@start,......bundleN@start
這種方法可以自動啟動所有bundle,但是寫起來仍然比較麻煩,需要把所有bundle 一個一個都配置為@start。
3. 在應(yīng)用的所有bundle 中選擇一個bundle,將其在 config.ini 中配置為自動啟動,然后在這個bundle 中,再把
應(yīng)用的所有其他bundle 啟動起來。假定該bundle 的Activator 類為 OSGiStartingBundleActivator, 代碼如下:
- public class OSGiStartingBundleActivator implements BundleActivator
- {
- public static BundleContext bundleContext = null;
- public void start(BundleContext context) throws Exception
- {
- bundleContext = context;
- // start bundles if it has been installed and not started
- Bundle[] allBundles = context.getBundles();
- for (int i=0; i
- {
- int currState = allBundles[i].getState();
- if ( Bundle.ACTIVE != currState && Bundle.RESOLVED==currState )
- {
- System.out.println("starting bundle : " + allBundles[i].getSymbolicName());
- try
- {
- allBundles[i].start();
- }
- catch (BundleException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- public void stop(BundleContext context) throws Exception
- {
- }
- }
【編輯推薦】
- OSGi入門與實踐全攻略
- 你好,OSGi!OSGi入門必讀系列
- OSGi與Spring:設(shè)置Spring DM開發(fā)環(huán)境
- OSGi和Spring入門:什么是Spring動態(tài)模型(Spring DM)?
- OSGi是什么:Java語言的動態(tài)模塊系統(tǒng)
網(wǎng)頁題目:OSGi應(yīng)用中自動啟動bundle
文章轉(zhuǎn)載:http://www.dlmjj.cn/article/dpdesed.html


咨詢
建站咨詢
