在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,完成了。

 

相關文章
相關標籤/搜索