posts - 156,  comments - 601,  trackbacks - 0

本笔记针对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 阅读(4521) 评论(2)  编辑  收藏 所属分类: osgi

只有注册用户登录后才能发表评论。


网站导航: