本筆記針對Spring-dynamic 1.2.0版本進行一個簡單開發應用講解,使用Apache Felix-1.8.0作為osgi運行環境。
安裝Felix
a. 運行Felix
下載 Felix-1.8.0版本,解壓后,可以看到bin,bundle,conf,doc四個目錄
運行命令
java -jar bin/felix.jar
b. 配置Felix, 打開conf/config.properties 文件,配置說明如下
#設置, Felix容器啟動時,自動加載的Bundles, 下是以個是必須的
felix.auto.start.1= \
file:bundle/org.apache.felix.shell-1.2.0.jar \
file:bundle/org.apache.felix.shell.tui-1.2.0.jar \
file:bundle/org.apache.felix.bundlerepository-1.4.0.jar
#設置log級別
felix.log.level=1
# 設置osgi framework 的啟動級別
#org.osgi.framework.startlevel=1
# 設置新安裝的bundle的啟動級別
#felix.startlevel.bundle=1
# 默認情況對不完整的bundles,是拋exception異常。
# 也可指定只提示warn,只需把下面的#注釋去年即可。
#felix.fragment.validation=warning
# Felix installs a stream and content handler factories by default,
# uncomment the following line to not install them.
#felix.service.urlhandlers=false
#
# Bundle config properties. 其它bunlde config properties
# 通過 BundleContext.getProperty("") 取得
#
org.osgi.service.http.port=8080
osgi.shell.telnet=on
obr.repository.url=http://felix.apache.org/obr/releases.xml
示例中,加載了 felix text ui bundle,可以通過命令行,對felix的osgi容器進行管理。
c. 給Felix加上 web console管理bundle
需要的類庫
org.apache.felix.framework-1.8.0.jar # osgi framework
org.apache.felix.http.jetty-1.0.0.jar # jetty http servlet container
org.apache.felix.log-1.0.0.jar # felix log support
org.apache.felix.webconsole-1.2.10.jar
啟動后,直接訪問
http://localhost:8080/system/console/bundles 默認用戶名和密碼為 admin/admin
運行Spring-dynamic 1.2.0 bundles模塊
a. 下載Spring-dynamic 1.2.0(簡稱dm)
運行 dm所需要類庫清單如下
spring-osgi-core-1.2.0.jar
com.springsource.junit-3.8.2.jar
com.springsource.org.objectweb.asm-2.2.3.jar
com.springsource.slf4j.api-1.5.0.jar
com.springsource.slf4j.log4j-1.5.0.jar
com.springsource.slf4j.org.apache.commons.logging-1.5.0.jar
org.springframework.aop-2.5.6.A.jar
org.springframework.beans-2.5.6.A.jar
org.springframework.context-2.5.6.A.jar
org.springframework.core-2.5.6.A.jar
org.springframework.test-2.5.6.A.jar
log4j.osgi-1.2.15-SNAPSHOT.jar
spring-osgi-annotation-1.2.0.jar
spring-osgi-extender-1.2.0.jar
修改 conf/conf.properties文件
1 felix.auto.start.1= \
2 file:bundle/org.apache.felix.shell-1.2.0.jar \
3 file:bundle/org.apache.felix.shell.tui-1.2.0.jar \
4 file:bundle/org.apache.felix.bundlerepository-1.4.0.jar \
5 file:bundle/org.apache.felix.scr-1.0.8.jar \
6 file:bundle/org.osgi.core-1.2.0.jar \
#新增以下的bundles加載
7 file:bundle/com.springsource.org.aopalliance-1.0.0.jar \
8 file:bundle/com.springsource.junit-3.8.2.jar \
9 file:bundle/com.springsource.org.objectweb.asm-2.2.3.jar \
10 file:bundle/com.springsource.slf4j.api-1.5.0.jar \
11 file:bundle/com.springsource.slf4j.log4j-1.5.0.jar \
12 file:bundle/com.springsource.slf4j.org.apache.commons.logging-1.5.0.jar \
13 file:bundle/org.springframework.aop-2.5.6.A.jar \
14 file:bundle/org.springframework.beans-2.5.6.A.jar \
15 file:bundle/org.springframework.context-2.5.6.A.jar \
16 file:bundle/org.springframework.core-2.5.6.A.jar \
17 file:bundle/org.springframework.test-2.5.6.A.jar \
18 file:bundle/log4j.osgi-1.2.15-SNAPSHOT.jar \
19 file:bundle/spring-osgi-annotation-1.2.0.jar \
20 file:bundle/spring-osgi-extender-1.2.0.jar \
21 file:bundle/spring-osgi-io-1.2.0.jar
b. 借助dm進行開發。本節將以一個簡單的應用,來演示如何使用Spring 1.2.0 進行開發。
功能說明:開發一個簡單的問候模型,功能非常簡單,傳入參數名稱,系統打印出 name, How are you today!
先定義一個接口
1 package com.xmatthew.practice.osgi;
2
3 /**
4 * Greeting interface
5 *
6 * @author xmatthew
7 * @since 2009-05-28
8 */
9 public interface Greeting {
10
11 String greet(String name);
12
13 }
下面實現這個接口
1 package com.xmatthew.practice.osgi.impl;
2
3 import com.xmatthew.practice.osgi.Greeting;
4
5 /**
6 * How are u greeting
7 *
8 * @author xmatthew
9 * @since 2009-05-28
10 */
11 public class HruGreeting implements Greeting {
12
13 public String greet(String name) {
14 if (name == null) {
15 name = "Matthew";
16 }
17 return name + ", How are you today!";
18 }
19
20 }
21
c. 接下來,借助dm,發布我們的服務
dm可以使我們借助Spring的配置文檔進行服務的發布。
要求必須配置文件必須以*.xml結尾,發布目錄為 META-INF/spring
目錄結構如下:
greeting.jar
|---- META-INF
|---- spring
|----- greet_service.xml
|----- greet_osgi_service.xml
1 <!-- greet_service.xml-->
2
3 <?xml version="1.0" encoding="UTF-8"?>
4 <beans xmlns="http://www.springframework.org/schema/beans"
5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
7
8 <!-- regular spring configuration file defining simple service
9 bean. We've kept the osgi definitions in a separate
10 configuration file so that this file can easily be used
11 for testing outside of an OSGi environment -->
12
13 <bean name="hruGreeting" class="com.xmatthew.practice.osgi.impl.HruGreeting" />
14
15 </beans>
1 <!-- greet_osgi_service.xml -->
2 <?xml version="1.0" encoding="UTF-8"?>
3 <beans xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:osgi="http://www.springframework.org/schema/osgi"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">
8
9 <!-- Export the simpleService bean (defined in a separate
10 config file in this case) as an OSGi service -->
11
12 <osgi:service id="hruGreetingOsgi" ref="hruGreeting"
13 interface="com.xmatthew.practice.osgi.Greeting" />
14
15 </beans>
MANIFEST.MF
Bundle-Version: 1.0
Bundle-SymbolicName: com.xmatthew.practice.osgi
Bundle-Name: Greeting Service
Bundle-Vendor: Spring Framework
Export-Package: com.xmatthew.practice.osgi,org.springframework.osgi.samples.simpleservice
Bundle-ManifestVersion: 2
需要注意: Export-Package必須指定,表示把包目錄發布出去。若不指定則在后面的測試中會報class not found exception
d.最后發布 greeting.jar
在Felix命令行,進行加載
start file:bundle/greeting.jar
e. 測試 greeting.jar, 看是否能正常工作
編寫一個BundleActivator實現類,來調用剛才發布的服務。
1
2 import org.osgi.framework.BundleActivator;
3 import org.osgi.framework.BundleContext;
4 import org.osgi.framework.ServiceReference;
5
6 import com.xmatthew.practice.osgi.Greeting;
7
8 /**
9 * Greeting service client
10 *
11 * @author xiemalin
12 * @since 2009-05-28
13 */
14 public class Activator implements BundleActivator {
15
16 /* (non-Javadoc)
17 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
18 */
19 public void start(BundleContext context) throws Exception {
20 System.out.println("starting greeting client");
21 // Query for all service references matching any language.
22 ServiceReference ref = context.getServiceReference(
23 Greeting.class.getName());
24 if (ref == null) {
25 System.out.println("Couldn't find any Greeting Service
");
26 } else {
27 Greeting service = (Greeting) context.getService(ref);
28 if (service == null) {
29 System.out.println("service is null");
30 } else
31 System.out.println(service.greet("Matthew"));
32 }
33 }
34
35 /* (non-Javadoc)
36 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
37 */
38 public void stop(BundleContext context) throws Exception {
39
40 }
41
42 }
編寫MANIFEST.MF文件
1 Manifest-Version: 1.0
2 Bundle-Name: Greeting Service client
3 Bundle-Description: A bundle that displays messages at startup and when service events occur
4 Bundle-Vendor: Apache Felix
5 Bundle-Version: 1.0.0
6 Bundle-Activator: com.xmatthew.practise.osgi.demo4.Activator
7 Require-Bundle: org.osgi.framework
8 Import-Package: org.osgi.framework,com.xmatthew.practice.osgi
注:Import-Package必須指定
com.xmatthew.practice.osgi。
部署 greeting_client bundle,將會輸出 Matthew, How are you today!
start file:bundle/greeting_client.jar
至此使用Spring dynamic 的基本入門開發已經完成。
Good Luck!
Yours Matthew!
posted on 2010-04-29 19:08
x.matthew 閱讀(4540)
評論(2) 編輯 收藏 所屬分類:
osgi