感受OSGI在ServiceMix4.0中的應用
ServiceMix4介紹
ServiceMix4是一個令人期待的ServiceMix新版本,它不僅支持JBI規範,更重要的是支持OSGI。與ServiceMix3.x相比,對OSGI的支持是它最大的特色。它包括Kernel和NMR兩個項目。Kernel是基於OSGI實現框架Flex實現的,它是ServiceMix4的基石;NMR構建於Kernel之上,它是JBI規範的具體實現。
ServiceMix4架構圖
關於ServiceMix4可以參看這裏
。
關於Kernel可以參看這裏
。
關於NMR可以參看這裏
。
由於ServiceMix4同時支持OSGI和JBI,所以它支持OSGI和JBI這2種部署。部署在ServiceMix3.x上的組件絕大多數可以直接部署在ServiceMix4中。
關於ServiceMix3.x到ServiceMix4的遷移,可以參看這裏
。
下面來看看基於OSGI部署的組件如何部署在ServiceMix4中。
初試ServiceMix4
準備工作
下載ServiceMix4
下載地址:http://servicemix.apache.org/SMX4/download.html
安裝ServiceMix4
直接將下載後的文件解壓即可。
啓動ServiceMix4
直接雙擊/bin文件夾中的servicemix.bat,片刻之後,就可以啓動ServiceMix4。
啓動後的界面如下:
基於OSGI的最簡單實例
該實例實現了將一個文件夾中的xml文件拷貝到另外一個文件夾中。
代碼片段如下:
......
<bean class="org.apache.servicemix.common.osgi.EndpointExporter"/>
<file:poller service="honnom:pollFile" endpoint="pollEndpoint"
file="honnom/inBox/" targetService="honnom:sendFile" >
</file:poller>
<file:sender service="honnom:sendFile" endpoint="sendEndpoint"
directory="honnom/outBox/">
</file:sender>
.....
代碼介紹
<bean class="org.apache.servicemix.common.osgi.EndpointExporter"/>
使用該bean完成poller和sender的OSGI註冊。
<file:poller service="honnom:pollFile" endpoint="pollEndpoint"
file="honnom/inBox/" targetService="honnom:sendFile" >
</file:poller>
定義一個poller,週期性地從文件夾honnom/inBox中讀取xml格式的文件。
<file:sender service="honnom:sendFile" endpoint="sendEndpoint"
directory="honnom/outBox/">
</file:sender>
定義一個sender,將接收到的文件存放在文件夾honnom/outBox中。
部署
直接將file-osgi.xml拷貝到ServiceMix4的安裝目錄的deploy下。
運行
ServiceMix4部署完file-osgi.xml後,會在/bin文件夾下生成兩個子目錄:
honnom/inBox和honnom/outBox。拷貝一個xml格式的文件到inBox中,幾秒鐘後該文件會被轉移到outBox中。
總結
可以看出ServiceMix4的部署更加簡單,可以直接通過OSGI部署xml文件,而不需要將xml打包成sa(.zap文件)。當然也可以將xml打包成sa,以JBI組件的方式進行部署。