OSGi应用中自动启动bundle

OSGi即Java模块系统,而OSGi bundle则是OSGi中软件发布的形式。本文讲述OSGi应用中如何自动启动bundle。作者最近开发了一个 OSGi 的应用,部署之后发现,当应用启动的时候,几乎所有 bundle 都处于 Resolved 状态,而不是 Started 状态。

创新互联专业为企业提供安州网站建设、安州做网站、安州网站设计、安州网站制作等企业网站建设、网页设计与制作、安州企业网站模板建站服务,十多年安州做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

编辑推荐: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. 在应用的所有bundle 中选择一个bundle,将其在 config.ini 中配置为自动启动,然后在这个bundle 中,再把

应用的所有其他bundle 启动起来。假定该bundle 的Activator 类为 OSGiStartingBundleActivator, 代码如下:

 
 
 
  1. public class OSGiStartingBundleActivator implements BundleActivator  
  2. {  
  3.     public static BundleContext bundleContext = null;  
  4.       
  5.     public void start(BundleContext context) throws Exception  
  6.     {  
  7.         bundleContext = context;  
  8.          
  9.         // start bundles if it has been installed and not started  
  10.         Bundle[] allBundles = context.getBundles();  
  11.         for (int i=0; i
  12.         {  
  13.             int currState = allBundles[i].getState();  
  14.             if ( Bundle.ACTIVE != currState && Bundle.RESOLVED==currState  )  
  15.             {  
  16.                     System.out.println("starting bundle : " + allBundles[i].getSymbolicName());  
  17.  
  18.                     try 
  19.                     {  
  20.                             allBundles[i].start();  
  21.                     }  
  22.                     catch (BundleException e)  
  23.                     {  
  24.                             e.printStackTrace();  
  25.                     }  
  26.               }  
  27.           }  
  28.     }  
  29.  
  30.     public void stop(BundleContext context) throws Exception  
  31.     {  
  32.     }  
  33.  }  
  34.  

【编辑推荐】

  1. OSGi入门与实践全攻略
  2. 你好,OSGi!OSGi入门必读系列
  3. OSGi与Spring:设置Spring DM开发环境
  4. OSGi和Spring入门:什么是Spring动态模型(Spring DM)?
  5. OSGi是什么:Java语言的动态模块系统

网页标题:OSGi应用中自动启动bundle
网址分享:http://www.shufengxianlan.com/qtweb/news4/237654.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联