OSGi应用中怎么启动bundle

OSGi应用中怎么启动bundle

本篇内容主要讲解“OSGi应用中怎么启动bundle”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“OSGi应用中怎么启动bundle”吧!

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

51CTO编辑推荐: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, 代码如下:

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<allBundles.length; 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应用中怎么启动bundle”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!