OSGi 中怎样自动启动所有 bundle

最近开发了一个 OSGi 的应用,部署之后发现,当应用启动的时候,几乎所有 bundle 都处于 Resolved 状态,而不是 Started 状态。
怎样启动这些 bundle 呢?有如下几种方法 :
1. 手工启动,即在 console 中使用命令 start N 来逐个启动所有bundle,其中 N 表示每个 bundle 的 id
    这种方法过于麻烦,要耗费大量时间,因此不可取。
2.在配置文件中声明为自动启动。在 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++)
        {
             Bundle currBundle = allBundles[i];
             if(currBundle instanceof BundleFragment)
             {
                  continue;
             }

            int currState = currBundle.getState();
            if ( Bundle.ACTIVE != currState && Bundle.RESOLVED==currState  )
            {
                    System.out.println("starting bundle : " + currBundle.getSymbolicName());

                    try
                    {
                            currBundle.start();
                    }
                    catch (BundleException e)
                    {
                            e.printStackTrace();
                    }
              }
          }
    }

    public void stop(BundleContext context) throws Exception
    {
    }
 }


转载于:https://www.cnblogs.com/zxnblake/archive/2009/09/29/1576075.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值