在Apache Felix中运行bundle

在前面搭建了一个Apache Felix的运行环境,下面就写一个简单的bundle,测试测试。 javascript

一、新建一个插件工程,以下图: java


 

点击下一步。
  app

二、给插件工程命名一个名字,这里叫pig1。This plug-in is targeted to run with中选择an OSGI framework -->standard,以下图红框中所示: 测试


 

点击下一步。
  spa

三、bundle中有一个启动类,默认是Activator,至关于普通工程中的Main类。你也能够把它更改为其余名字,这里使用默认的名字。以下图: .net


 

点击下一步。
  插件

四、去掉Create a plug-in using one of the templates,以下图: component


 

点击Finish。 blog

五、插件工程建好后,打开Activator类,能够看到里面有一个start方法和一个stop方法,能够在bundle启动和中止的时候作一些事情。这里只是简单地输出一个字符串,做为bundle启动和中止时的标识。 图片

Java代码    收藏代码
  1. /* 
  2.      * (non-Javadoc) 
  3.      *  
  4.      * @see  
  5.      * org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext 
  6.      * ) 
  7.      */  
  8.     public void start(BundleContext bundleContext) throws Exception  
  9.     {  
  10.         Activator.context = bundleContext;  
  11.         System.out.println("start pig1");  
  12.     }  
  13.   
  14.     /* 
  15.      * (non-Javadoc) 
  16.      *  
  17.      * @see  
  18.      * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) 
  19.      */  
  20.     public void stop(BundleContext bundleContext) throws Exception  
  21.     {  
  22.         Activator.context = null;  
  23.         System.out.println("stop pig1");  
  24.     }  

 
 六、代码也写好后,就能够导出插件工程发布了。如何让这个工程做为一个bundle被部署到Felix容器中呢?右击插件工程pig1,选择Export。可看下图:


 

出现Export视图以后,选择Plug-in Development下的Deployable plug-ins and fragments,以下图:



 
点击下一步,选择要导出的插件,Destination选项卡的Directory选择咱们的Felix环境的物理地址,导出后,会在Felix工程的根目录自动建立一个plugins目录,bundle会默认导出这个目录。以下图:


 

点击Finish,你就能够看到Felix工程下面多了一个plugins目录,咱们所导出的bundle就在里面,以下图:


 

七、接着就是安装、运行了。

有三种方法能够安装、运行一个bundle。

(1)使用命令。

首先,启动Felix,在Console中先使用install命令安装bundle,接着使用start命令启动bundle,以下图:



 
启动的时候,start命令后接着那个bundle的启动ID就能够启动bundle了,如上图的12。

能够看到,当启动bundle的时候,输出了Activator类中start方法的输出语句,即"start pig1"。

Pig1的状态为Active,说明bundle启动成功了。

固然,你也可使用uninstall命令卸载一个bundle,用法如install命令。

 

(2)使用Felix配置文件,打开conf/config.properties,以下图:



 

打开config.properties,找到felix.auto.start.1参数,值写成file:plugins/pig1_1.0.0.201109291700.jar,如:

(若是你有多个bundle,之间用空格隔开)。

 

参数代码    收藏代码
  1. # The following property is a space-delimited list of bundle URLs  
  2. # to install and start when the framework starts. The ending numerical  
  3. # component is the target start level. Any number of these properties  
  4. # may be specified for different start levels.  
  5. felix.auto.start.1=file:plugins/pig1_1.0.0.201109291700.jar  

 

参数写好后,启动Felix,你就能够看到bundle Pig1自动安装并启动了,以下图所示:



 

(3)第三种方法就是使用File Install了,使用Apache Felix的File Install bundle,咱们能够安装和启动bundle而无需启动Felix,这个将在下面的章节中讲解。

 

八、OK,完成了。

 

相关文章
相关标签/搜索