Your SlideShare is downloading. ×
0
OSG i 動態服務模組 開發實作 詹景逸 Ching Yi, Chan. aka qrtt1 [email_address]
Agenda <ul><li>開發環境準備 </li></ul><ul><ul><li>JDK </li></ul></ul><ul><ul><li>Maven </li></ul></ul><ul><ul><li>Felix </li></u...
開發環境準備 <ul><li>JDK  #2 </li></ul><ul><ul><li>Java SE http://java.sun.com/javase/downloads/index.jsp </li></ul></ul><ul><ul...
開發環境準備 <ul><li>Maven  #3 </li></ul><ul><ul><li>取得檔案 http://maven.apache.org/download.html </li></ul></ul><ul><ul><li>解壓縮 <...
開發環境準備 <ul><li>Felix – OSGi Container  #5 </li></ul><ul><ul><li>取得檔案 http://apache.stu.edu.tw/felix/felix-1.4.1.zip </li><...
Felix  基本操作 <ul><li>啟動  Felix  #6 </li></ul><ul><ul><li>java -jar bin/felix.jar </li></ul></ul><ul><li>常用指令 </li></ul><ul>...
PART 1 Everything  is  Bundle
安裝  Bundle <ul><li>File Install Bundle  #7 </li></ul><ul><ul><li>功能:在  Felix  執行目錄下建立  load   資料夾。定期監看  load  資料夾的檔案,若是  O...
建立  Bundle <ul><li>使用  Maven  建立新的專案  #9 </li></ul><ul><ul><li>for unix-like </li></ul></ul><ul><ul><li>for windows </li><...
Maven  專案導覽 <ul><li>專案目錄  #9 </li></ul><ul><ul><li>Artifact  名稱 </li></ul></ul><ul><ul><li>hello.bundle </li></ul></ul><ul...
使用  Maven-Bundle-Plugin (1) <ul><li>修改  pom.xml  檔案  #10 </li></ul><ul><ul><li>改變  <packaging />  為  bundle </li></ul></ul...
使用  Maven-Bundle-Plugin (2) <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin...
建立  Bundle Activator package  javatwo2009; import  org.osgi.framework.BundleActivator; import  org.osgi.framework.BundleCo...
編譯與安裝  bundle <ul><li>使用  Maven  編譯並打包專案  #14 </li></ul><ul><ul><li>mvn clean package </li></ul></ul><ul><li>安裝  bundle </...
OSGi  怎麼跑? System  Bundle B B B PID  永遠為 0 JVM OSGi Framework Module Life Cycle Service
OSGi  怎麼跑? Installed Resolving & Loading Classes Resolved JVM OSGi Framework System  Bundle M O D U L E Export-Package: or...
OSGi  怎麼跑? Resolved public   class  Activator  implements  BundleActivator { public   void  start(BundleContext context)  ...
PART 2 Service  is  Everything
OSGi Service  在哪裡? JVM OSGi Framework SERVICE REGISTRY Bundle Activator Bundle Activator Bundle Activator publish publish ...
服務與實作角色  (1) <ul><li>Service Provider Interface , SPI  #16 </li></ul>Export-Package Bundle Activator
服務與實作角色  (2) <ul><li>Application Programming Interface , API  </li></ul>Import-Package Bundle Activator
服務與實作角色  (3) <ul><li>Service Provider </li></ul>Import-Package ServiceRegistration registration =  bundleContext. register...
服務與實作角色  (4) <ul><li>Service Consumer </li></ul>Import-Package ServiceReference ref = bundleContext . getServiceReference ...
實作  TinyURL Command(1) <ul><li>擴充  Felix Shell  指令 </li></ul>Apache Felix Shell Service Activator Apache Felix Shell TUI A...
實作  TinyURL Command(2) org.apache.felix.shell Export-Package Import-Package //  發佈  Service bundleContext.registerService(...
實作  TinyURL Command(3) publish publish 追蹤  Command  服務 加入新增的  Command  服務 使用  ShellService  擁有的  Command Apache Felix Shel...
動手做看看 <ul><li>實作  TinyurlCommand  替  Felix  增加指令 </li></ul><ul><ul><li>See TODO 2 &  #17..#21 </li></ul></ul><ul><ul><li>t...
詹景逸 Ching Yi, Chan. aka qrtt1 [email_address] Thank You
Upcoming SlideShare
Loading in...5
×

OSGi Small Lab

2,207

Published on

Published in: Education
1 Comment
5 Likes
Statistics
Notes
No Downloads
Views
Total Views
2,207
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
74
Comments
1
Likes
5
Embeds 0
No embeds

No notes for slide
  • Transcript of "OSGi Small Lab"

    1. 1. OSG i 動態服務模組 開發實作 詹景逸 Ching Yi, Chan. aka qrtt1 [email_address]
    2. 2. Agenda <ul><li>開發環境準備 </li></ul><ul><ul><li>JDK </li></ul></ul><ul><ul><li>Maven </li></ul></ul><ul><ul><li>Felix </li></ul></ul><ul><li>練習主題介紹 </li></ul><ul><ul><li>Felix – OSGi Container 的使用 </li></ul></ul><ul><ul><li>Bundle – 建立 OSGi 模組 </li></ul></ul><ul><ul><li>Service – 建立 OSGi 服務 </li></ul></ul>
    3. 3. 開發環境準備 <ul><li>JDK #2 </li></ul><ul><ul><li>Java SE http://java.sun.com/javase/downloads/index.jsp </li></ul></ul><ul><ul><li>檢查環境變數 </li></ul></ul><ul><ul><ul><li>JAVA_HOME </li></ul></ul></ul><ul><ul><ul><li>CLASSPATH </li></ul></ul></ul><ul><ul><ul><li>PATH </li></ul></ul></ul>
    4. 4. 開發環境準備 <ul><li>Maven #3 </li></ul><ul><ul><li>取得檔案 http://maven.apache.org/download.html </li></ul></ul><ul><ul><li>解壓縮 </li></ul></ul><ul><ul><ul><li>~/app </li></ul></ul></ul><ul><ul><ul><li>c:app </li></ul></ul></ul><ul><li>修改 PATH 變數 </li></ul><ul><ul><li>export PATH=$PATH:~/app/apache-maven- x.y.z /bin </li></ul></ul><ul><ul><li>set PATH=%PATH%;c:appapache-maven- x.y.z in </li></ul></ul><ul><li>執行測試 </li></ul><ul><ul><li>mvn -v </li></ul></ul>
    5. 5. 開發環境準備 <ul><li>Felix – OSGi Container #5 </li></ul><ul><ul><li>取得檔案 http://apache.stu.edu.tw/felix/felix-1.4.1.zip </li></ul></ul><ul><ul><li>解壓縮 </li></ul></ul><ul><ul><ul><li>~/app </li></ul></ul></ul><ul><ul><ul><li>c:app </li></ul></ul></ul><ul><li>啟動 Felix </li></ul><ul><ul><li>java -jar bin/felix.jar </li></ul></ul><ul><li>停止 Felix </li></ul><ul><ul><li>stop 0 或是 shutdown </li></ul></ul>
    6. 6. Felix 基本操作 <ul><li>啟動 Felix #6 </li></ul><ul><ul><li>java -jar bin/felix.jar </li></ul></ul><ul><li>常用指令 </li></ul><ul><ul><li>help </li></ul></ul><ul><ul><li>ps – 列出已安裝 Bundle </li></ul></ul><ul><ul><li>install – 安裝 Bundle </li></ul></ul><ul><ul><li>start – 啟動 Bundle </li></ul></ul><ul><ul><li>stop – 停止 Bundle </li></ul></ul><ul><ul><li>update – 更新 Bundle </li></ul></ul><ul><ul><li>uninstall – 移除 Bundle ( 請先別執行 ) </li></ul></ul><ul><ul><li>shutdown – 離開 felix </li></ul></ul>
    7. 7. PART 1 Everything is Bundle
    8. 8. 安裝 Bundle <ul><li>File Install Bundle #7 </li></ul><ul><ul><li>功能:在 Felix 執行目錄下建立 load 資料夾。定期監看 load 資料夾的檔案,若是 OSGi Bundle 就 自動安裝 。 </li></ul></ul><ul><li>使用 Install 指令安裝 Bundle </li></ul><ul><ul><li>透過網路檔案安裝 </li></ul></ul><ul><ul><li>透過檔案系統安裝 </li></ul></ul><ul><li>測試 File Install Bundle ( 需先啟動 Bundle) </li></ul><ul><ul><li>複製 hello.service-1.0.0.jar 至 load 資料夾 </li></ul></ul>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.jar install file:// /home/qrtt1/temp/ org.apache.felix.fileinstall-0.9.0.jar
    9. 9. 建立 Bundle <ul><li>使用 Maven 建立新的專案 #9 </li></ul><ul><ul><li>for unix-like </li></ul></ul><ul><ul><li>for windows </li></ul></ul>mvn archetype:create -Dversion=1.0.0 -DgroupId=javatwo2009 -DartifactId=hello.bundle mvn archetype:create ^ -Dversion=1.0.0 ^ -DgroupId=javatwo2009 ^ -DartifactId=hello.bundle
    10. 10. Maven 專案導覽 <ul><li>專案目錄 #9 </li></ul><ul><ul><li>Artifact 名稱 </li></ul></ul><ul><ul><li>hello.bundle </li></ul></ul><ul><li>編譯並打包專案 ( 請先執行這個步驟 ) </li></ul><ul><ul><li>cd hello.bundle </li></ul></ul><ul><ul><li>mvn clean package </li></ul></ul>
    11. 11. 使用 Maven-Bundle-Plugin (1) <ul><li>修改 pom.xml 檔案 #10 </li></ul><ul><ul><li>改變 <packaging /> 為 bundle </li></ul></ul><ul><ul><li>增加 OSGi Framework 的 Dependency </li></ul></ul><ul><ul><li>設定 Maven-Bundle-Plugin ( 接續下頁 ) </li></ul></ul><dependency> <groupId>org.apache.felix</groupId> <artifactId>org.osgi.core</artifactId> <version>1.0.0</version> </dependency>
    12. 12. 使用 Maven-Bundle-Plugin (2) <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> Manifest-Version: 1.0 Export-Package: javatwo2009;uses:=&quot;org.osgi.framework&quot; Built-By: Administrator Tool: Bnd-0.0.308 Bundle-Name: hello.bundle Created-By: Apache Maven Bundle Plugin Bundle-Version: 1.0.0 Build-Jdk: 1.6.0_06 Bnd-LastModified: 1235833008625 Bundle-ManifestVersion: 2 Bundle-Activator: javatwo2009.Activator Import-Package: javatwo2009,org.osgi.framework;version=&quot;1.3&quot; Bundle-SymbolicName: hello.bundle
    13. 13. 建立 Bundle Activator package 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(&quot;Hey!&quot;); } public void stop(BundleContext context) throws Exception { System. out .println(&quot;Bye!&quot;); } } #12 .. #13
    14. 14. 編譯與安裝 bundle <ul><li>使用 Maven 編譯並打包專案 #14 </li></ul><ul><ul><li>mvn clean package </li></ul></ul><ul><li>安裝 bundle </li></ul><ul><ul><li>將 target/hello.bundle-1.0.0.jar 複製到 FELIX_HOME/load 資料夾 </li></ul></ul>
    15. 15. OSGi 怎麼跑? System Bundle B B B PID 永遠為 0 JVM OSGi Framework Module Life Cycle Service
    16. 16. OSGi 怎麼跑? Installed Resolving & Loading Classes Resolved JVM OSGi Framework System Bundle M O D U L E 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
    17. 17. OSGi 怎麼跑? Resolved public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { System. out .println(&quot;Hey!&quot;); } public void stop(BundleContext context) throws Exception { System. out .println(&quot;Bye!&quot;); } } JVM OSGi Framework System Bundle L I F E C Y C L E Starting Active Stopping Activator.stop() Activator.start()
    18. 18. PART 2 Service is Everything
    19. 19. OSGi Service 在哪裡? JVM OSGi Framework SERVICE REGISTRY Bundle Activator Bundle Activator Bundle Activator publish publish publish find & bind find & bind find & bind unpublish unpublish unpublish
    20. 20. 服務與實作角色 (1) <ul><li>Service Provider Interface , SPI #16 </li></ul>Export-Package Bundle Activator
    21. 21. 服務與實作角色 (2) <ul><li>Application Programming Interface , API </li></ul>Import-Package Bundle Activator
    22. 22. 服務與實作角色 (3) <ul><li>Service Provider </li></ul>Import-Package ServiceRegistration registration = bundleContext. registerService ( ServiceA .class.getName(), new ConcreteServiceA() , prop); Bundle Activator
    23. 23. 服務與實作角色 (4) <ul><li>Service Consumer </li></ul>Import-Package ServiceReference ref = bundleContext . getServiceReference ( ServiceA .class.getName()); ServiceA service = (ServiceA) bundleContext.getService(ref); Bundle Activator
    24. 24. 實作 TinyURL Command(1) <ul><li>擴充 Felix Shell 指令 </li></ul>Apache Felix Shell Service Activator Apache Felix Shell TUI Activator Tinyurl Command Activator
    25. 25. 實作 TinyURL Command(2) org.apache.felix.shell Export-Package Import-Package // 發佈 Service bundleContext.registerService( org.apache.felix.shell. Command .class.getName(), new TinyURLCommand (), null); Apache Felix Shell Service Activator Apache Felix Shell TUI Activator Tinyurl Command Activator 實作 Service
    26. 26. 實作 TinyURL Command(3) publish publish 追蹤 Command 服務 加入新增的 Command 服務 使用 ShellService 擁有的 Command Apache Felix Shell Service Activator Apache Felix Shell TUI Activator Tinyurl Command Activator
    27. 27. 動手做看看 <ul><li>實作 TinyurlCommand 替 Felix 增加指令 </li></ul><ul><ul><li>See TODO 2 & #17..#21 </li></ul></ul><ul><ul><li>tinyurl http://very_long_url.com.tw </li></ul></ul>
    28. 28. 詹景逸 Ching Yi, Chan. aka qrtt1 [email_address] Thank You
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×