OSGI(Open Services Gateway Initiative),或者通俗點說JAVA動態(tài)模塊系統(tǒng),定義了一套模塊應(yīng)用開發(fā)的框架。OSGI容器實現(xiàn)方案如Knopflerfish, Equinox, and Apache Felix允許你把你的應(yīng)用分成多個功能模塊,這樣通過依賴管理這些功能會更加方便。
和Servlet和EJB規(guī)范類似,OSGI規(guī)范包含兩大塊:一個OSGI容器需要實現(xiàn)的服務(wù)集合;一種OSGI容器和應(yīng)用之間通信的機制。開發(fā)OSGI平臺意味著你需要使用OSGI API編寫你的應(yīng)用,然后將其部署到OSGI容器中。從開發(fā)者的視角來看,OSGI提供以下優(yōu)勢:
- 你可以動態(tài)地安裝、卸載、啟動、停止不同的應(yīng)用模塊,而不需要重啟容器。
- 你的應(yīng)用可以在同一時刻跑多個同一個模塊的實例。
- OSGI在SOA領(lǐng)域提供成熟的解決方案,包括嵌入式,移動設(shè)備和富客戶端應(yīng)用等。
OK,你已經(jīng)有個Servlet容器來做web 應(yīng)用,有了EJB容器來做事務(wù)處理,你可能在想為什么你還需要一個新的容器?簡單點說,OSGI容器被設(shè)計專門用來開發(fā)可分解為功能模塊的復(fù)雜的Java應(yīng)用。
企業(yè)應(yīng)用領(lǐng)域的OSGI
OSGI規(guī)范最初是由OSGI聯(lián)盟在1999年3月發(fā)起。它的主要目的是成為向網(wǎng)絡(luò)設(shè)備傳輸服務(wù)管理的開放規(guī)范。核心思想是一旦你向網(wǎng)絡(luò)設(shè)備中添加了一個OSGI服務(wù)平臺,你可以在網(wǎng)絡(luò)中的任意位置管理該設(shè)備上的服務(wù)組件。這些服務(wù)組件可以任意安裝,更新或移除而不會對設(shè)備產(chǎn)生影響。
多年來,OSGI技術(shù)只出現(xiàn)在嵌入式系統(tǒng)和網(wǎng)絡(luò)設(shè)備市場?,F(xiàn)在,Eclipse使OSGI在企業(yè)開發(fā)領(lǐng)域煥發(fā)出新的光彩。
OSGI受到越來越廣泛的支持
2003年,Eclipse開發(fā)團隊開始尋找一種使eclipse成為一種功能更動態(tài)、工具更模塊化的富客戶端平臺。最終,他們的目光鎖定在OSGI框架上。Eclipse3.0,2004年6月發(fā)布,是基于OSGI技術(shù)搭建的首個Eclipse版本。
幾乎所有企業(yè)應(yīng)用服務(wù)提供商支持或計劃支持OSGI。Spring框架同樣支持OSGI,通過Spring DM(Spring Dynamic Modules for OSGI Service Platforms)項目,可以讓我們在Spring上更方便的應(yīng)用OSGI。
開源OSGI容器
從企業(yè)應(yīng)用開發(fā)者的角度看,OSGI容器侵入性非常小,你可以方便地將其嵌入一個企業(yè)應(yīng)用。舉個例子來說,假設(shè)你在開發(fā)一個復(fù)雜的web應(yīng)用。你希望將這個應(yīng)用分解成多個功能模塊。一個View層模塊,一個Model層模塊,一個DAO模塊。使用嵌入式OSGI容器來跨依賴地管理這些模塊可以讓你隨時更新你的DAO模塊卻不需要重啟你的服務(wù)器。
只要你的應(yīng)用完全符合OSGI規(guī)范,它就可以在所有符合OSGI規(guī)范的容器內(nèi)運行?,F(xiàn)在,有三種流行的開源OSGI容器:
- Equinox是OSGI Service Platform Release 4的一個實現(xiàn)。是Eclipse 模塊化運行時的核心。
- Knopflerfish另一個選擇。
- Apache Felix是Apache軟件基金會贊助的一個OSGI容器
在這篇文章里我們使用Equinox作為我們的OSGI容器。
嘗試開發(fā)一個Hello World bundle
在OSGI的領(lǐng)域,發(fā)布的軟件是以bundle的形式出現(xiàn)。bundle由java class類和資源文件組成,向設(shè)備所有者提供功能,同時可以為其他的bundles提供服務(wù)。Eclipse對開發(fā)bundles提供了強大的支持。Eclipse不僅僅提供創(chuàng)建bundles的功能,它還集成了Equinox這個OSGI容器,你可以在其上開發(fā)和調(diào)試OSGI組件。其實所有的Eclipse插件都是使用Eclipse規(guī)范代碼寫的OSGI bundle。接下來,你將可以學(xué)到如何使用Eclipse IDE開發(fā)一個Hello world osgi bundle。
開始開發(fā)bundle
我們一步步的開始:
- 啟動Eclipse,依次點 File --> New --> Project。
- 選擇Plug-in Project,next。
- 輸入Project Name項目名稱,比如com.howard.sample.HelloWorld,Target Platform(目標(biāo)平臺)里的an OSGI framework,選擇standard。
- 剩下的保持默認(rèn),next。
- 下個對話框也默認(rèn),next。
- 然后選擇Hello OSGI Bundle作為模版。Finish。
Eclipse會飛快的為你創(chuàng)建Hello world bundle的模版代碼。主要包含兩個文件:Activator.java和MANIFEST.MF。
Activator.java的代碼如下所示:
- import org.osgi.framework.BundleActivator;
- import org.osgi.framework.BundleContext;
- public class Activator implements BundleActivator {
- public void start(BundleContext context) throws Exception {
- System.out.println("Hello world");
- }
- public void stop(BundleContext context) throws Exception {
- System.out.println("Goodbye World");
- }
- }
如果你的bundle在啟動和關(guān)閉的時候需要被通知,你可以考慮實現(xiàn)BundleActivator接口。以下是定義Activator的一些注意點:
- 你的Activator類需要一個公有的無參數(shù)構(gòu)造函數(shù)。OSGI框架會通過類反射的方式來實例化一個Activator類。
- 容器啟動bundle過程中負(fù)責(zé)調(diào)用你的Activator類的start方法。bundle可以在此初始化資源比如說初始化數(shù)據(jù)庫連接。start方法需要一個參數(shù),BundleContext對象。這個對象允許bundles以取得OSGI容器相關(guān)信息的方式和框架交互。如果某一個bundle有異常拋出,容器將對該bundle標(biāo)記為stopped并不將其納入service列表。
- 容器關(guān)閉的時候會調(diào)用你的Activator類方法stop(),你可以利用這個機會做一些清理的操作。
MANIFEST.MF
這個文件是你的bundle的部署描述文件。格式和Jar里的MANIFEST.MF是一樣的。包含的不少名值對,就像如下:
- Manifest-Version: 1.0
- Bundle-ManifestVersion: 2
- Bundle-Name: HelloWorld Plug-in
- Bundle-SymbolicName: com.howard.sample.HelloWorld
- Bundle-Version: 1.0.0
- Bundle-Activator: com.howard.sample.helloworld.Activator
- Bundle-Vendor: HOWARD
- Bundle-RequiredExecutionEnvironment: JavaSE-1.6
- Import-Package: org.osgi.framework;version="1.3.0"
分別來看下:
Bundle-ManifestVersion
數(shù)值為2意味著本bundle支持OSGI規(guī)范第四版;如果是1那就是支持OSGI規(guī)范第三版。
Bundle-Name
給bundle定義一個短名,方便人員閱讀
Bundle-SymbolicName
給bundle定義一個唯一的非局部名。方便分辨。
Bundle-Activator
聲明在start和stop事件發(fā)生時會被通知的監(jiān)聽類的名字。
Import-Package
定義bundle的導(dǎo)入包。
Hello World bundle完成了,接下來我們運行一下。
執(zhí)行bundle
- 點擊Run --> Run Configuration
- 在左邊的OSGI Framework選項里右鍵 new ,創(chuàng)建一個新的OSGI Run Configuration
- 名字隨便取好了,我們?nèi)€OSGi hello world。
- 你會注意到中間的窗口里Workspace項目里有一子項 com.howard.sample.HelloWorld,將其勾選上,其他的不用管。這時的狀態(tài)應(yīng)該如下圖。
- 點擊Run按鈕。在控制臺你應(yīng)該可以看見點東西了。那是叫做OSGI控制臺的東東。與子相伴,還有一個"Hello world"。

OSGI控制臺
OSGI控制臺是一個OSGI容器的命令行界面。你可以利用它做些諸如啟動,關(guān)閉,安裝bundles,更新和刪除bundles等操作。現(xiàn)在,點擊OSGI控制臺所在的位置,回車,你就會發(fā)現(xiàn)可以輸入命令了。這時的OSGI控制臺應(yīng)該如下圖:

下面列出一些常用的OSGI命令,你可以試著和OSGI容器交互。
ss 顯示已安裝的bundles的狀態(tài)信息,信息包括bundle ID,短名,狀態(tài)等等。
start 啟動一個bundle
stop 關(guān)閉一個bundle
update 載入一個新的JAR文件更新一個bundle
install 安裝一個新的bundle到容器中
uninstall 卸載一個已在容器中的bundle
依賴管理
OSGI規(guī)范允許你把你的應(yīng)用分解成多個模塊然后管理各個模塊間的依賴關(guān)系。
這需要通過bundle scope來完成。默認(rèn)情況下,一個bundle內(nèi)的class對其他bundle來說是不可見的。那么,如果要讓一個bundle訪問另一個bundle里的class要怎么做?解決的方案就是從源bundle導(dǎo)出包,然后在目標(biāo)bundle里導(dǎo)入。
接下來我們對此做一個例子。
首先,我們需要先創(chuàng)建一個com.howard.sample.HelloService bundle,我們將通過它導(dǎo)出一個包。
然后,我們在com.howard.sample.HelloWorld 這個bundle里導(dǎo)入包。
導(dǎo)出包
1、創(chuàng)建名為com.howard.sample.HelloService的bundle,創(chuàng)建步驟和前面一樣。
2、在這個bundle內(nèi),添加一個com.howard.sample.service.HelloService.java 接口,代碼如下:
- public interface HelloService {
- public String sayHello();
- }
3、創(chuàng)建一個com.howard.sample.service.impl.HelloServiceImpl.java類實現(xiàn)剛才的接口:
- public class HelloServiceImpl implements HelloService{
- public String sayHello() {
- System.out.println("Inside HelloServiceImple.sayHello()");
- return "Say Hello";
- }
- }
4、打開MANIFEST.MF,選擇Runtime標(biāo)簽項,在Exported Packages選項欄,點擊Add并且選擇com.howard.sample.service這個包。然后MANIFEST.MF的代碼應(yīng)該如下:
- Manifest-Version: 1.0
- Bundle-ManifestVersion: 2
- Bundle-Name: HelloService Plug-in
- Bundle-SymbolicName: com.howard.sample.HelloService
- Bundle-Version: 1.0.0
- Bundle-Activator: com.howard.sample.helloservice.Activator
- Bundle-Vendor: HOWARD
- Bundle-RequiredExecutionEnvironment: JavaSE-1.6
- Import-Package: org.osgi.framework;version="1.3.0"
- Export-Package: com.howard.sample.service
你可以看到,MANIFEST.MF文件和剛才的HelloWorld的那份很類似。唯一的區(qū)別就是這個多了Export-Package這個標(biāo)記,對應(yīng)的值就是我們剛才選擇的com.howard.sample.service。
Export-Package標(biāo)記告訴OSGI容器在com.howard.sample.service包內(nèi)的classes可以被外部訪問。
注意,我們僅僅暴露了HelloService接口,而不是直接暴露HelloServiceImpl實現(xiàn)。
導(dǎo)入包
接下來我們要更新原來的HelloWorld bundle以導(dǎo)入com.howard.sample.service包。步驟如下:
1、進入HelloWorld bundle,打開MANIFEST.MF,進入Dependencies標(biāo)簽頁,在Imported Packages里添加com.howard.sample.service。MANIFEST.MF文件應(yīng)該如下所示:
- Manifest-Version: 1.0
- Bundle-ManifestVersion: 2
- Bundle-Name: HelloWorld Plug-in
- Bundle-SymbolicName: com.howard.sample.HelloWorld
- Bundle-Version: 1.0.0
- Bundle-Activator: com.howard.sample.helloworld.Activator
- Bundle-Vendor: HOWARD
- Bundle-RequiredExecutionEnvironment: JavaSE-1.6
- Import-Package: com.howard.sample.service,
- org.osgi.framework;version="1.3.0"
沒錯,Import-package標(biāo)記的值也就是導(dǎo)入的包名之間是用逗號隔開的。在這里導(dǎo)入了兩個包om.howard.sample.service和org.osgi.framework。后者是使用Activator類時必須導(dǎo)入的包。
2、接下來,打開HelloWorld項目下的Activator.java文件,這時候你會發(fā)現(xiàn)可以使用HelloService這個接口了。但還是不能使用HelloServiceImpl實現(xiàn)類。Eclipse會告訴你:Access restriction(立入禁止)。
Class級別可見域
為什么OSGI容器可以做到讓jar包中的一些classes可見而另一些又不可見呢。
答案其實就是OSGI容器自定義了java class loader來有選擇的加載類。OSGI容器為每一個bundle都創(chuàng)建了不同的class loader。因此,bundle可以訪問的classes包括
- Boot classpath:所有的java基礎(chǔ)類。
- Framework classpath:OSGI框架級別的classloader加載的類
- Bundle classpath:Bundle本身引用的關(guān)系緊密的JAR的路徑
- Imported packages:就是在MANIFEST.MF里聲明的導(dǎo)入包,一旦聲明,在bundle內(nèi)就可見了。
bundle級別的可見域允許你可以隨時放心的更改HelloServiceImpl實現(xiàn)類而不需要去擔(dān)心依賴關(guān)系會被破壞。
OSGI服務(wù)
OSGI框架是實現(xiàn)SOA的絕佳土壤。通過它可以實現(xiàn)bundles暴露服務(wù)接口給其他bundles消費而不需要讓細(xì)節(jié)暴露。消費bundles甚至可以完全不知道提供服務(wù)的bundles。憑著可以良好的隱藏具體實現(xiàn)的能力,OSGI當(dāng)之無愧是SOA的一種較完美的實現(xiàn)方案。
OSGI中,提供服務(wù)的bundle在OSGI容器上將一個POJO注冊成一個service。消費者bundle請求OSGI容器中基于某個特殊接口的注冊service。一旦找到,消費者bundle就會綁定它,然后就可以調(diào)用service中的方法了。舉個例子會更容易說明。
導(dǎo)出services
1、確保com.howard.sample.HelloService里的MANIFEST.MF導(dǎo)入org.osgi.framework包
2、創(chuàng)建com.howard.sample.service.impl.HelloServiceActivator.java,代碼如下:
- public class HelloServiceActivator implements BundleActivator {
- ServiceRegistration helloServiceRegistration;
- @Override
- public void start(BundleContext context) throws Exception {
- HelloService helloService = new HelloServiceImpl();
- helloServiceRegistration = context.registerService(HelloService.class
- .getName(), helloService, null);
- }
-
- @Override
- public void stop(BundleContext context) throws Exception {
- helloServiceRegistration.unregister();
- }
-
- }
OK,我們就是用BundleContext的registerService方法注冊service的。這個方法需要三個參數(shù)。
- service的接口名。如果service實現(xiàn)了多個接口,那樣你需要傳入一個包含所有接口名的String數(shù)組。在這里我們傳入的是HelloService這個接口。
- 真正的service實現(xiàn)。在例子中我們傳了一個HelloServiceImpl實現(xiàn)。
- service屬性。這個參數(shù)可以在有多個service實現(xiàn)同一個接口的情況下,消費者用來區(qū)分真正感興趣的service。
3、最后一步就是修改HelloService的MANIFEST.MF文件,將Bundle-Activator改成com.howard.sample.service.impl.HelloServiceActivator
現(xiàn)在HelloService bundle已經(jīng)隨時準(zhǔn)備將HelloServiceImpl服務(wù)發(fā)布了。OSGI容器啟動HelloServie bundle的時候會讓HelloServiceActivator運作,在那個時候?qū)elloServiceImpl注冊到容器中,接下來就是創(chuàng)建消費者的問題了。
導(dǎo)入service
我們的消費者就是HelloWorld bundle,主要修改的就是其中的Activator.java,修改代碼如下:
- public class Activator implements BundleActivator {
- ServiceReference helloServiceReference;
- public void start(BundleContext context) throws Exception {
- System.out.println("Hello World!!");
- helloServiceReference=context.getServiceReference(HelloService.class.getName());
- HelloService helloService=(HelloService)context.getService(helloServiceReference);
- System.out.println(helloService.sayHello());
- }
- public void stop(BundleContext context) throws Exception {
- System.out.println("Goodbye World!!");
- context.ungetService(helloServiceReference);
- }
- }
代碼很簡單,就不多說了。
在運行之前我們在Run-->Run Configurations對話框里,把HelloWorld和HelloService這兩個bundle前面的鉤都打上。然后運行時你會發(fā)現(xiàn)HelloServiceImpl.sayHello()方法已經(jīng)被調(diào)用了。
在OSGI控制臺輸入ss并回車,所有容器內(nèi)的bundle狀態(tài)一目了然。其中id為0的bundle是OSGI框架基礎(chǔ)bundle,另兩個就是HelloService和HelloWorld了,它倆的id是隨機的,狀態(tài)是ACTIVE也就是已啟動狀態(tài)。假設(shè)HelloService的id為7,HelloWorld為8。
輸入stop 8就可以暫停bundle的運行,容器內(nèi)這個bundle還是存在的,只是狀態(tài)變成了RESOLVED。再次啟動使用start 8,然后就會看見HelloWorld bundle消費了HelloService的服務(wù)。
創(chuàng)建服務(wù)工廠
剛才例子所示,我們會在HelloService bundle啟動時初始化并注冊service。然后不管存不存在消費端,這個service都會存在,而且消費端取得的service 實例其實都是同一個。OK,某些servie是比較耗費資源的主,我們不希望它一直占用資源,最好是在真正用它的時候創(chuàng)建不用的時候銷毀就最好了。
解決如上問題的方案就是使用ServiceFactory接口的實現(xiàn)來代替原先service具體的實現(xiàn)到OSGI容器去注冊。這樣,以后只有當(dāng)其他bundle請求該服務(wù)時,才會由ServiceFactory實現(xiàn)類來處理請求并返回一個新的service實例。
實例步驟如下:
1、在HelloService bundle創(chuàng)建一個實現(xiàn)ServiceFactory接口的類HelloServiceFactory類,代碼如下:
- public class HelloServiceFactory implements ServiceFactory {
- private int usageCounter = 0;
- @Override
- public Object getService(Bundle bundle, ServiceRegistration registration) {
- System.out.println("Create object of HelloService for " + bundle.getSymbolicName());
- usageCounter++;
- System.out.println("Number of bundles using service " + usageCounter);
- HelloService helloService = new HelloServiceImpl();
- return helloService;
- }
- @Override
- public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
- System.out.println("Release object of HelloService for " + bundle.getSymbolicName());
- usageCounter--;
- System.out.println("Number of bundles using service " + usageCounter);
- }
- }
ServiceFactory接口定義了兩個方法:
- getService方法:特定的bundle在第一次調(diào)用BundleContext的getService方法時由OSGI框架調(diào)用,在實例代碼中,我們用這個方法來返回一個新的HelloService的實現(xiàn)。OSGI框架會緩存這個返回的對象,如果同一個bundle在未來再次調(diào)用BundleContext的getService方法的話,會直接返回這個緩存中的對象。
- ungetService方法:bundle釋放service的時候由OSGI容器調(diào)用。
2、修改HelloServiceActivator.java的start方法,將ServiceFactory作為服務(wù)注冊,代碼如下:
- public class HelloServiceActivator implements BundleActivator {
- ServiceRegistration helloServiceRegistration;
- @Override
- public void start(BundleContext context) throws Exception {
- HelloServiceFactory helloServiceFactory = new HelloServiceFactory();
- helloServiceRegistration = context.registerService(HelloService.class
- .getName(), helloServiceFactory, null);
- }
-
- @Override
- public void stop(BundleContext context) throws Exception {
- helloServiceRegistration.unregister();
- }
- }
現(xiàn)在運行下試試看,你會發(fā)現(xiàn)HelloWorld bundle啟動時才會初始化HelloService,控制臺會打印出"Number of bundles using service 1",當(dāng)HelloWorld bundle暫停時會打印出"Number of bundles using service 0"。
services跟蹤
某種情形下,我們可能需要在某個特殊的接口有新的服務(wù)注冊或取消注冊時通知消費端。這時我們可以使用ServiceTracker類。如下步驟所示:
1、在HelloWorld bundle里的MANIFEST.MF導(dǎo)入org.util.tracker包。
2、創(chuàng)建HelloServiceTracker類,代碼如下:
- public class HelloServiceTracker extends ServiceTracker {
- public HelloServiceTracker(BundleContext context) {
- super(context, HelloService.class.getName(),null);
- }
- public Object addingService(ServiceReference reference) {
- System.out.println("Inside HelloServiceTracker.addingService " + reference.getBundle());
- return super.addingService(reference);
- }
- public void removedService(ServiceReference reference, Object service) {
- System.out.println("Inside HelloServiceTracker.removedService " + reference.getBundle());
- super.removedService(reference, service);
- }
- }
我們在HelloServiceTracker的構(gòu)造函數(shù)里將HelloService接口名傳進去,ServiceTracker會跟蹤實現(xiàn)這個接口的所有的注冊services。ServiceTracker主要有兩個重要方法:
- addingService方法:bundle注冊一個基于給定接口的service時調(diào)用。
- removeService方法:bundle取消注冊一個基于給定接口的service時調(diào)用。
3、修改Activator類,使用剛剛創(chuàng)建的HelloServiceTracker來獲取service:
- public class Activator implements BundleActivator {
- HelloServiceTracker helloServiceTracker;
- public void start(BundleContext context) throws Exception {
- System.out.println("Hello World!!");
- helloServiceTracker= new HelloServiceTracker(context);
- helloServiceTracker.open();
- HelloService helloService=(HelloService)helloServiceTracker.getService();
- System.out.println(helloService.sayHello());
- }
-
- public void stop(BundleContext context) throws Exception {
- System.out.println("Goodbye World!!");
- helloServiceTracker.close();
- }
- }
現(xiàn)在運行一下,可以發(fā)現(xiàn)只要HelloService bundle啟動或是暫停都會導(dǎo)致HelloServiceTracker的對addingService或removedService方法的調(diào)用。
ServiceTracker不僅僅能跟蹤Service的動向,它還能通過getService方法取得Service實例并返回。但是如果同一個接口下有多個service注冊,這時返回哪個service呢?這時候就需要看service的等級哪個高了。這個等級是service注冊時的property屬性里的一項:SERVICE_RANKING。誰的SERVICE_RANKING高,就返回誰。
如果有兩個一樣高的呢?這時再看這兩個service誰的PID更低了。
如果對OSGI的ClassLoader機制有疑問,可以看看這篇解釋ClassLoader機制和自定義ClassLoader相關(guān)的文章:
http://longdick.iteye.com/blog/442213
OSGI的基本原理和入門開發(fā)就到這里了。希望同學(xué)們能對OSGI開發(fā)有個簡單而清晰的認(rèn)識。