JavaWorld@TW the best professional Java site in Taiwan sun training logo
      註冊 | 登入 | 全文檢索 | 排行榜  

» JavaWorld@TW » Java Tools  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 [note] 撰寫你的第一隻 Spring OSGi bundle
qrtt1





發文: 1004
積分: 28
於 2008-07-17 18:05 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
準備工作與需要的環境

1. 下載 Spring Dynamic Modules for OSGi(tm) Service Platforms
http://nchc.dl.sourceforge.net/sourceforge/springframework/spring-osgi-1.0.2-with-dependencies.zip

註:目前最近版本為 1.1.0 版,但是部分的 bundle 版本無法滿足相依性。所以我們採用與文章 Hello, OSGi, Part 2: Introduction to Spring Dynamic Modules http://www.javaworld.com/javaworld/jw-04-2008/jw-04-osgi2.html 相同的版本。

2. 您需要安裝好 Maven
3. 使用來練習的 IDE 為 Eclipse,並且裝好 M2Eclipse Plugin

建立 Spring OSGi Bundle 專案

假設專案的 package 為 foo.bar,名稱為 Orz,我們使用下列指令建立專案:

For Windows
1
2
3
4
5
6
7
8
mvn archetype:create ^
-DremoteRepositories=http://s3.amazonaws.com/maven.springframework.org/snapshot ^
-DarchetypeGroupId=org.springframework.osgi ^
-DarchetypeArtifactId=spring-osgi-bundle-archetype ^
-DarchetypeVersion=1.1.0 ^
-Dversion=1.0 ^
-DgroupId=s2h.osgi.example ^
-DartifactId=HelloSpringOSGi


For unix-like
1
2
3
4
5
6
7
8
mvn archetype:create \
-DremoteRepositories=http://s3.amazonaws.com/maven.springframework.org/snapshot \
-DarchetypeGroupId=org.springframework.osgi \
-DarchetypeArtifactId=spring-osgi-bundle-archetype \
-DarchetypeVersion=1.1.0 \
-Dversion=1.0 \
-DgroupId=s2h.osgi.example \
-DartifactId=HelloSpringOSGi


當專案建立完成後,您可以依習慣的 IDE 產生相關的 project 資訊。
我們現在要產生 eclipse 的專案資訊,請在建立好專案資料夾的裡面執行:
1
mvn eclipse:eclipse


接著,開啟 Eclipse 匯入專案:


選用 Maven Project 匯入:


(...經過 Maven 瘋狂地下載之後...)

開寫始 HelloWorld 老梗

在 src/main/java 資料夾的 foo.bar package 內新增 HelloWorld 類別
1
2
3
4
5
6
7
8
9
10
11
package foo.bar;
 
public class HelloWorld {
        public void start() throws Exception {
                System.out.println("Hello Spring OSGi World!! ");
        }
 
        public void stop() throws Exception {
                System.out.println("Goodbye Spring OSGi World!!");
        }
}


開啟檔案 src/main/resources/META-INF/spring/bundle-context.xml,新增 bean 的宣告
1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
  <bean name="hello" class="foo.bar.HelloWorld"
    init-method="start" destroy-method="stop" />
 
</beans>


打包 bundle

1. 專案名稱上以滑鼠右鍵點開選單,找到「Run AS」選擇「Maven Package」
2. (Maven 又瘋狂地下載後) 執行完打包的動作後,您會多出 target 目錄,內有檔案 Orz-1.0.jar。這就是一個 OSGi Bundle

執行 bundle

先將下載回來的 spring dm 解壓縮,並找出下列 jar 檔,統一複製到一個資料夾(Ex. C:/APP/spring-osgi-dep)。
1
2
3
4
5
6
7
8
9
log4j.osgi-1.2.15-SNAPSHOT.jar
spring-core-2.5.1.jar
spring-beans-2.5.1.jar
spring-context-2.5.1.jar
spring-aop-2.5.1.jar
aopalliance.osgi-1.0-SNAPSHOT.jar
spring-osgi-core-1.0.2.jar
spring-osgi-extender-1.0.2.jar
spring-osgi-io-1.0.2.jar


在您的 OSGi Container 裡,使用 install 與 start 指令,安裝與啟動這些 bundles
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
install file:C:/APP/spring-osgi-dep/log4j.osgi-1.2.15-SNAPSHOT.jar
install file:C:/APP/spring-osgi-dep/spring-core-2.5.1.jar
install file:C:/APP/spring-osgi-dep/spring-beans-2.5.1.jar
install file:C:/APP/spring-osgi-dep/spring-context-2.5.1.jar
install file:C:/APP/spring-osgi-dep/spring-aop-2.5.1.jar
install file:C:/APP/spring-osgi-dep/aopalliance.osgi-1.0-SNAPSHOT.jar
install file:C:/APP/spring-osgi-dep/spring-osgi-core-1.0.2.jar
install file:C:/APP/spring-osgi-dep/spring-osgi-extender-1.0.2.jar
install file:C:/APP/spring-osgi-dep/spring-osgi-io-1.0.2.jar
 
start file:C:/APP/spring-osgi-dep/log4j.osgi-1.2.15-SNAPSHOT.jar
start file:C:/APP/spring-osgi-dep/spring-core-2.5.1.jar
start file:C:/APP/spring-osgi-dep/spring-beans-2.5.1.jar
start file:C:/APP/spring-osgi-dep/spring-context-2.5.1.jar
start file:C:/APP/spring-osgi-dep/spring-aop-2.5.1.jar
start file:C:/APP/spring-osgi-dep/aopalliance.osgi-1.0-SNAPSHOT.jar
start file:C:/APP/spring-osgi-dep/spring-osgi-core-1.0.2.jar
start file:C:/APP/spring-osgi-dep/spring-osgi-extender-1.0.2.jar
start file:C:/APP/spring-osgi-dep/spring-osgi-io-1.0.2.jar


請使用查詢指令先確定是否這些 bundle 都已經啟動
1
2
3
4
5
6
7
8
9
10
11
id      State       Bundle
0       ACTIVE      org.eclipse.osgi_3.4.0.v20080605-1900
24      ACTIVE      org.springframework.osgi.log4j.osgi_1.2.15.SNAPSHOT
25      ACTIVE      org.springframework.bundle.spring.core_2.5.1
26      ACTIVE      org.springframework.bundle.spring.beans_2.5.1
27      ACTIVE      org.springframework.bundle.spring.context_2.5.1
28      ACTIVE      org.springframework.bundle.spring.aop_2.5.1
29      ACTIVE      org.springframework.osgi.aopalliance.osgi_1.0.0.SNAPSHOT
30      ACTIVE      org.springframework.bundle.osgi.core_1.0.2
31      ACTIVE      org.springframework.bundle.osgi.extender_1.0.2
32      ACTIVE      org.springframework.bundle.osgi.io_1.0.2


最後按裝您剛才撰寫的 Hello World (Orz-1.0.jar)
1
install file:C:/temp/Orz/target/Orz-1.0.jar


您可以這樣啟動它,或使用他的 pid
1
start file:C:/temp/Orz/target/Orz-1.0.jar


如果您看到了 Hello Spring OSGi World!! 那就是您成功了。


vote up 0 vote down
qrtt1 edited on 2008-07-17 19:30
reply to postreply to post
蝸牛角上爭何事?石火光中寄此身,隨富隨貧且歡樂,不開口笑是癡人。
qrtt1's page
作者 Re:[note] 撰寫你的第一隻 Spring OSGi bundle [Re:qrtt1]
qrtt1





發文: 1004
積分: 28
於 2008-09-10 22:37 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
看起來 spring 自己的 repository 已經移到了比較舊版本的 archetype plugin
查了一下公用的 maven repostiory 已經發佈至 1.1.1 版了
http://mvnrepository.com/artifact/org.springframework.osgi/spring-osgi-bundle-archetype

執行時只要拿掉額外的 remote plugin repository 設定, 重新指定 archetype version 即可
1
2
3
4
5
6
7
mvn archetype:create \
-DarchetypeGroupId=org.springframework.osgi \
-DarchetypeArtifactId=spring-osgi-bundle-archetype \
-DarchetypeVersion=1.1.1  \
-Dversion=1.0 \
-DgroupId=s2h.osgi.example \
-DartifactId=HelloSpringOSGi


vote up 0 vote down
reply to postreply to post
蝸牛角上爭何事?石火光中寄此身,隨富隨貧且歡樂,不開口笑是癡人。
qrtt1's page
作者 Re:[note] 撰寫你的第一隻 Spring OSGi bundle [Re:qrtt1]
qrtt1





發文: 1004
積分: 28
於 2008-09-11 09:54 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
執行 1.1.1 版需要的 bundle

log4j.osgi-1.2.15-SNAPSHOT.jar
com.springsource.slf4j.log4j-1.5.0.jar
com.springsource.slf4j.api-1.5.0.jar
com.springsource.slf4j.org.apache.commons.logging-1.5.0.jar

spring-core-2.5.5.jar
spring-beans-2.5.5.jar
spring-context-2.5.5.jar
spring-aop-2.5.5.jar
com.springsource.org.aopalliance-1.0.0.jar

spring-osgi-io-1.1.1.jar
spring-osgi-core-1.1.1.jar
spring-osgi-extender-1.1.1.jar


vote up 0 vote down
reply to postreply to post
蝸牛角上爭何事?石火光中寄此身,隨富隨貧且歡樂,不開口笑是癡人。
qrtt1's page

» JavaWorld@TW »  Java Tools

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JavaWorld@TW


Powered by Powerful JuteForum® Version Jute 1.5.8
Copyright© 2002-2003 Rainman Zhu,Zua,Netboy,Scott. All Rights Reserved.