`
housen1987
  • 浏览: 340949 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

Osgi SpringDM struts2 学习笔记

 
阅读更多

本笔记针对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 文件,配置说明如下

Js代码  收藏代码
  1. #设置, Felix容器启动时,自动加载的 Bundles, 下是以个是必须的  
  2. felix.auto.start.1= \  
  3.  file:bundle/org.apache.felix.shell-1.2.0.jar \  
  4.  file:bundle/org.apache.felix.shell.tui-1.2.0.jar \  
  5.  file:bundle/org.apache.felix.bundlerepository-1.4.0.jar  
  6.   
  7. #设置log级别  
  8. felix.log.level=1   
  9.  
  10. # 设置osgi framework 的启动级别  
  11. #org.osgi.framework.startlevel=1  
  12.  
  13. # 设置新安装的bundle的启动级别  
  14. #felix.startlevel.bundle=1  
  15.  
  16. # 默认情况对不完整的bundles,是抛exception异常。  
  17. # 也可指定只提示warn,只需把下面的#注释去年即可。  
  18. #felix.fragment.validation=warning  
  19.  
  20. # Felix installs a stream and content handler factories by default,  
  21. # uncomment the following line to not install them.  
  22. #felix.service.urlhandlers=false  
  23.  
  24. #  
  25. # Bundle config properties. 其它bunlde config properties  
  26. # 通过 BundleContext.getProperty("") 取得  
  27. #  
  28. org.osgi.service.http.port=8080  
  29. osgi.shell.telnet=on  
  30. 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所需要类库清单如下

Java代码  收藏代码
  1. spring-osgi-core-1.2.0.jar                                     
  2. com.springsource.junit-3.8.2.jar                               
  3. com.springsource.org.objectweb.asm-2.2.3.jar                   
  4. com.springsource.slf4j.api-1.5.0.jar                           
  5. com.springsource.slf4j.log4j-1.5.0.jar                         
  6. com.springsource.slf4j.org.apache.commons.logging-1.5.0.jar    
  7. org.springframework.aop-2.5.6.A.jar                            
  8. org.springframework.beans-2.5.6.A.jar                          
  9. org.springframework.context-2.5.6.A.jar                        
  10. org.springframework.core-2.5.6.A.jar                           
  11. org.springframework.test-2.5.6.A.jar                           
  12. log4j.osgi-1.2.15-SNAPSHOT.jar                                 
  13. spring-osgi-annotation-1.2.0.jar                               
  14. spring-osgi-extender-1.2.0.jar      

 

修改 conf/conf.properties文件

Java代码  收藏代码
  1.  1 felix.auto.start.1= \  
  2.  2  file:bundle/org.apache.felix.shell-1.2.0.jar \  
  3.  3  file:bundle/org.apache.felix.shell.tui-1.2.0.jar \  
  4.  4  file:bundle/org.apache.felix.bundlerepository-1.4.0.jar \  
  5.  5  file:bundle/org.apache.felix.scr-1.0.8.jar \  
  6.  6  file:bundle/org.osgi.core-1.2.0.jar \  
  7. #新增以下的bundles加载  
  8.  7  file:bundle/com.springsource.org.aopalliance-1.0.0.jar \  
  9.  8  file:bundle/com.springsource.junit-3.8.2.jar                              \  
  10.  9  file:bundle/com.springsource.org.objectweb.asm-2.2.3.jar                  \  
  11. 10  file:bundle/com.springsource.slf4j.api-1.5.0.jar                          \  
  12. 11  file:bundle/com.springsource.slf4j.log4j-1.5.0.jar                        \  
  13. 12  file:bundle/com.springsource.slf4j.org.apache.commons.logging-1.5.0.jar   \  
  14. 13  file:bundle/org.springframework.aop-2.5.6.A.jar                           \  
  15. 14  file:bundle/org.springframework.beans-2.5.6.A.jar                         \  
  16. 15  file:bundle/org.springframework.context-2.5.6.A.jar                       \  
  17. 16  file:bundle/org.springframework.core-2.5.6.A.jar                          \  
  18. 17  file:bundle/org.springframework.test-2.5.6.A.jar                          \  
  19. 18  file:bundle/log4j.osgi-1.2.15-SNAPSHOT.jar                                \  
  20. 19  file:bundle/spring-osgi-annotation-1.2.0.jar                              \  
  21. 20  file:bundle/spring-osgi-extender-1.2.0.jar                                \  
  22. 21  file:bundle/spring-osgi-io-1.2.0.jar                                 

 

b. 借助dm进行开发。本节将以一个简单的应用,来演示如何使用Spring 1.2.0 进行开发。
功能说明:开发一个简单的问候模型,功能非常简单,传入参数名称,系统打印出 name, How are you today!

  先定义一个接口

Java代码  收藏代码
  1.  1 package com.xmatthew.practice.osgi;  
  2.  2   
  3.  3 /** 
  4.  4  * Greeting interface 
  5.  5  *  
  6.  6  * @author xmatthew 
  7.  7  * @since 2009-05-28 
  8.  8  */  
  9.  9 public interface Greeting {  
  10. 10   
  11. 11     String greet(String name);  
  12. 12       
  13. 13 }  

 

下面实现这个接口

Java代码  收藏代码
  1.  1 package com.xmatthew.practice.osgi.impl;  
  2.  2   
  3.  3 import com.xmatthew.practice.osgi.Greeting;  
  4.  4   
  5.  5 /** 
  6.  6  * How are u greeting 
  7.  7  *  
  8.  8  * @author xmatthew 
  9.  9  * @since 2009-05-28 
  10. 10  */  
  11. 11 public class HruGreeting implements Greeting {  
  12. 12   
  13. 13     public String greet(String name) {  
  14. 14         if (name == null) {  
  15. 15             name = "Matthew";  
  16. 16         }  
  17. 17         return name + ", How are you today!";  
  18. 18     }  
  19. 19   
  20. 20 }  
  21. 21   
 

c. 接下来,借助dm,发布我们的服务
 dm可以使我们借助Spring的配置文档进行服务的发布。
 要求必须配置文件必须以*.xml结尾,发布目录为 META-INF/spring
目录结构如下:
 greeting.jar
   |---- META-INF
          |---- spring
                  |----- greet_service.xml
                  |----- greet_osgi_service.xml  
<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->

Java代码  收藏代码
  1.  1 <!-- greet_service.xml-->  
  2.  2   
  3.  3 <?xml version="1.0" encoding="UTF-8"?>  
  4.  4 <beans xmlns="http://www.springframework.org/schema/beans"  
  5.  5   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.  6   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  7.  7   
  8.  8   <!-- regular spring configuration file defining simple service  
  9.  9        bean. We've kept the osgi definitions in a separate   
  10. 10        configuration file so that this file can easily be used  
  11. 11        for testing outside of an OSGi environment -->  
  12. 12   
  13. 13   <bean name="hruGreeting" class="com.xmatthew.practice.osgi.impl.HruGreeting" />  
  14. 14   
  15. 15 </beans>  

 

Java代码  收藏代码
  1.  1 <!-- greet_osgi_service.xml -->  
  2.  2 <?xml version="1.0" encoding="UTF-8"?>  
  3.  3 <beans xmlns="http://www.springframework.org/schema/beans"  
  4.  4   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.  5   xmlns:osgi="http://www.springframework.org/schema/osgi"  
  6.  6   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.  7                       http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">  
  8.  8   
  9.  9   <!-- Export the simpleService bean (defined in a separate  
  10. 10        config file in this case) as an OSGi service -->  
  11. 11   
  12. 12   <osgi:service id="hruGreetingOsgi" ref="hruGreeting"  
  13. 13     interface="com.xmatthew.practice.osgi.Greeting" />  
  14. 14   
  15. 15 </beans>  

 

MANIFEST.MF

Java代码  收藏代码
  1. Bundle-Version: 1.0  
  2. Bundle-SymbolicName: com.xmatthew.practice.osgi  
  3. Bundle-Name: Greeting Service  
  4. Bundle-Vendor: Spring Framework  
  5. Export-Package: com.xmatthew.practice.osgi,org.springframework.osgi.samples.simpleservice  
  6. Bundle-ManifestVersion: 2  

 

需要注意: Export-Package必须指定,表示把包目录发布出去。若不指定则在后面的测试中会报class not found exception
d.最后发布 greeting.jar
  在Felix命令行,进行加载

  start file:bundle/greeting.jar

e. 测试 greeting.jar, 看是否能正常工作

编写一个BundleActivator实现类,来调用刚才发布的服务。

Java代码  收藏代码
  1. 1   
  2.  2 import org.osgi.framework.BundleActivator;  
  3.  3 import org.osgi.framework.BundleContext;  
  4.  4 import org.osgi.framework.ServiceReference;  
  5.  5   
  6.  6 import com.xmatthew.practice.osgi.Greeting;  
  7.  7   
  8.  8 /** 
  9.  9  * Greeting service client 
  10. 10  *  
  11. 11  * @author xiemalin 
  12. 12  * @since 2009-05-28 
  13. 13  */  
  14. 14 public class Activator implements BundleActivator {  
  15. 15   
  16. 16     /* (non-Javadoc) 
  17. 17      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) 
  18. 18      */  
  19. 19     public void start(BundleContext context) throws Exception {  
  20. 20         System.out.println("starting greeting client");  
  21. 21         // Query for all service references matching any language.  
  22. 22         ServiceReference ref = context.getServiceReference(  
  23. 23                 Greeting.class.getName());  
  24. 24         if (ref == null) {  
  25. 25             System.out.println("Couldn't find any Greeting Service");  
  26. 26         } else {  
  27. 27             Greeting service = (Greeting) context.getService(ref);  
  28. 28             if (service == null) {  
  29. 29                 System.out.println("service is null");  
  30. 30             } else  
  31. 31             System.out.println(service.greet("Matthew"));  
  32. 32         }  
  33. 33     }  
  34. 34   
  35. 35     /* (non-Javadoc) 
  36. 36      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) 
  37. 37      */  
  38. 38     public void stop(BundleContext context) throws Exception {  
  39. 39   
  40. 40     }  
  41. 41   
  42. 42 }  

 

编写MANIFEST.MF文件

Java代码  收藏代码
  1. 1 Manifest-Version: 1.0  
  2. 2 Bundle-Name: Greeting Service client  
  3. 3 Bundle-Description: A bundle that displays messages at startup and when service events occur  
  4. 4 Bundle-Vendor: Apache Felix  
  5. 5 Bundle-Version: 1.0.0  
  6. 6 Bundle-Activator: com.xmatthew.practise.osgi.demo4.Activator  
  7. 7 Require-Bundle: org.osgi.framework  
  8. 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 的基本入门开发已经完成。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics