osgi small lab

Post on 30-Aug-2014

3.445 Views

Category:

Education

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

OSG i 動態服務模組開發實作詹景逸Ching Yi, Chan. aka qrtt1Chingyichan.tw@gmail.com

Agenda

開發環境準備JDKMavenFelix

練習主題介紹Felix – OSGi Container 的使用Bundle – 建立 OSGi 模組Service – 建立 OSGi 服務

開發環境準備JDK #2

Java SEhttp://java.sun.com/javase/downloads/index.jsp

檢查環境變數JAVA_HOMECLASSPATHPATH

開發環境準備 Maven #3

取得檔案http://maven.apache.org/download.html

解壓縮 ~/app c:\app

修改 PATH 變數 export PATH=$PATH:~/app/apache-maven-x.y.z/bin set PATH=%PATH%;c:\app\apache-maven-x.y.z\bin

執行測試mvn -v

開發環境準備 Felix – OSGi Container #5

取得檔案http://apache.stu.edu.tw/felix/felix-1.4.1.zip

解壓縮 ~/app c:\app

啟動 Felixjava -jar bin/felix.jar

停止 Felixstop 0 或是 shutdown

Felix 基本操作啟動 Felix #6

java -jar bin/felix.jar常用指令

help ps – 列出已安裝 Bundleinstall – 安裝 Bundlestart – 啟動 Bundlestop – 停止 Bundleupdate – 更新 Bundle uninstall – 移除 Bundle ( 請先別執行 )shutdown – 離開 felix

PART 1

Everything is

Bundle

安裝 Bundle File Install Bundle #7

功能:在 Felix 執行目錄下建立 load 資料夾。定期監看 load 資料夾的檔案,若是 OSGi Bundle 就自動安裝。使用 Install 指令安裝 Bundle

透過網路檔案安裝透過檔案系統安裝

測試 File Install Bundle ( 需先啟動 Bundle)複製 hello.service-1.0.0.jar 至 load 資料夾

install http://apache.ntu.edu.tw/felix/org.apache.felix.fileinstall-0.9.0.jar

install file:///c:/temp/org.apache.felix.fileinstall-0.9.0.jarinstall file:///home/qrtt1/temp/org.apache.felix.fileinstall-0.9.0.jar

建立 Bundle

使用 Maven 建立新的專案 #9for unix-like

for windows

mvn archetype:create \-Dversion=1.0.0 \-DgroupId=javatwo2009 \-DartifactId=hello.bundle

mvn archetype:create ^-Dversion=1.0.0 ^-DgroupId=javatwo2009 ^-DartifactId=hello.bundle

Maven 專案導覽專案目錄 #9

Artifact 名稱hello.bundle

編譯並打包專案 ( 請先執行這個步驟 )cd hello.bundlemvn clean package

使用 Maven-Bundle-Plugin (1)

修改 pom.xml 檔案 #10改變 <packaging /> 為 bundle增加 OSGi Framework 的 Dependency

設定 Maven-Bundle-Plugin ( 接續下頁 )

<dependency><groupId>org.apache.felix</groupId><artifactId>org.osgi.core</artifactId><version>1.0.0</version>

</dependency>

<build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Export-Package>${pom.groupId}</Export-Package> <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName> <Bundle-Activator>${pom.groupId}.Activator</Bundle-Activator> </instructions> </configuration> </plugin> </plugins></build>

使用 Maven-Bundle-Plugin (2)

Manifest-Version: 1.0Export-Package: javatwo2009;uses:="org.osgi.framework"Built-By: AdministratorTool: Bnd-0.0.308Bundle-Name: hello.bundleCreated-By: Apache Maven Bundle PluginBundle-Version: 1.0.0Build-Jdk: 1.6.0_06Bnd-LastModified: 1235833008625Bundle-ManifestVersion: 2Bundle-Activator: javatwo2009.ActivatorImport-Package: javatwo2009,org.osgi.framework;version="1.3"Bundle-SymbolicName: hello.bundle

建立 Bundle Activatorpackage javatwo2009;

import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;

/** * Hello world!*/public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { System.out.println("Hey!"); } public void stop(BundleContext context) throws Exception { System.out.println("Bye!"); }}

#12 .. #13

編譯與安裝 bundle

使用 Maven 編譯並打包專案 #14 mvn clean package

安裝 bundle將 target/hello.bundle-1.0.0.jar 複製到 FELI

X_HOME/load 資料夾

OSGi 怎麼跑?

JVMOSGi Framework

Module

Life Cycle

Service

System Bundle B B B

PID 永遠為0

Installed

OSGi 怎麼跑?

JVMOSGi Framework

System Bundle

MODULE

Export-Package: org.osgi.framework, org.osgi.framework.hooks.service,org.osgi.service.packageadmin,org.osgi.service.startlevel,org.osgi.service.url,org.osgi.util.tracker

Resolving & Loading Classes

Resolved

OSGi 怎麼跑?

JVMOSGi Framework

System Bundle

LIFE

CYCLE

Resolved

Starting Active

Stopping

Activator.stop()

Activator.start()

PART 2

Service is

Everything

OSGi Service 在哪裡?

JVMOSGi Framework

Bundle

Activator

Bundle

Activator

Bundle

Activator

SERVICE REGISTRY

publish publish publish

find & bind find & bind find & bindunpublish unpublish

unpublish

服務與實作角色 (1)

Service Provider Interface , SPI #16

Bundle

Activator Export-Package

服務與實作角色 (2)

Application Programming Interface , API

Bundle

ActivatorImport-Package

服務與實作角色 (3)

Service Provider

Bundle

ActivatorImport-Package

ServiceRegistration registration = bundleContext.registerService( ServiceA.class.getName(), new ConcreteServiceA(), prop);

服務與實作角色 (4)

Service Consumer

Bundle

ActivatorImport-Package

ServiceReference ref = bundleContext .getServiceReference(ServiceA.class.getName());

ServiceA service = (ServiceA) bundleContext.getService(ref);

Apache Felix Shell ServiceActivator

Apache Felix Shell TUIActivator

Tinyurl CommandActivator

實作 TinyURL Command(1)

擴充 Felix Shell 指令

Apache Felix Shell ServiceActivator

Apache Felix Shell TUIActivator

Tinyurl CommandActivator

實作 TinyURL Command(2)

org.apache.felix.shellExport-Package

Import-Package

Apache Felix Shell ServiceActivator

Apache Felix Shell TUIActivator

Tinyurl CommandActivator

實作 TinyURL Command(3)

publish

publish

追蹤 Command 服務加入新增的 Command 服務

使用 ShellService 擁有的 Command

動手做看看實作 TinyurlCommand 替 Felix 增加指令

See TODO 2 & #17..#21 tinyurl http://very_long_url.com.tw

詹景逸Ching Yi, Chan. aka qrtt1Chingyichan.tw@gmail.com

Thank You

top related