资源预览内容
第1页 / 共17页
第2页 / 共17页
第3页 / 共17页
第4页 / 共17页
第5页 / 共17页
第6页 / 共17页
第7页 / 共17页
第8页 / 共17页
第9页 / 共17页
第10页 / 共17页
亲,该文档总共17页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
浅析OSGi技术中国银行常德分行李宇龙20121231目录浅析OSGi技术1一,面向对象和面向服务的工厂模式的设计区别3二,用OSGi技术进行工厂模式设计,剖析模块间的依赖关系51, 开发准备62, 开发过程6A, 仓|建接 口模块 com.alon.osgifruit.interf6B, 创建服务模块 com.alon.osgifruit.apple8C, 创建服务模块 com.alon.osgifruit.oranger9D, Virgo中加载两个服务模块,以便下一步创建web应用模块时调用。10E, 创建 web 应用模块 com.alon.osgifruit.apps103, 测试13三,OSGi技术应用前景14概述OSGi (Open Service Gateway Initiative)是一个基于 Java 语言,为 规范接口服务所提供的服务平台(OSGi Service Platform)。OSGi技术 的神奇作用,是让Java程序模块化,在系统不停止服务情况下,实 现程序“即插即用即拔即无”,动态部署各应用程序。与传统的程序 设计模式相比,具有更灵活的扩展性。模块化技术不必按面向对象思 想,把重点放在高度抽象出对象模型设计上。而面对服务,根据实际 业务需求划分出各服务接口的边界和各服务接口间的依赖关系,这可 让Java程序按服务功能不同,分成不同的模块,模块间通过服务来 调用。从而提高了程序的内聚性,降低了程序的耦合度。目前能找到的OSGi中文参考资料很少,想了解其内部运行机制 并不容易。笔者经过反复摸索和测试,从工厂设计模式着手分析,更 直观说明OSGi的实质。一,面向对象和面向服务的工厂模式的设计区别Java设计模式之一,工厂设计模式是为了解决接口选择自身实现 类的问题。面向对象的最佳做法是在应用端中输入实现类,或用反射 为接口找到实现类,但要在配置文件里修改,并需重启服务器才能更 新接口的实现。而面向服务的模块化设计是把同一个接口的N个实现 类,划分为N个bundle项冃(模块),并完全封装,不再更改,也就 不再编译,通过热拔插装缶卩服务模块,进行实现类的选择。请看实例比较。而向对象的工厂模式,把所有接口、实现、应用都放在一个项目 里。以下实例代码,把“水果”抽象为一个接11,让”苹果”和”桔 子”实现了水果的eat()方法。在选择各种水果时,需要输入apple- 或orange,才能进行具体的服务。其代码块与面向服务方式所划分 模块的对应关系块如图。interface Fruitpublic void eat();晦人A、class Apple implements Fruitpublic void eat()System, out. printin (N 吃苹果H);class Orange implements Fruitpublic void eat()Sysen. out.prmtln (吃桔子n);class :rui*:actorypublic static 土二:cetri-it (String name) Fm f=null;if (Bapplen.equals(nane)f=new Apple 0;伏丫尽if (orange99, equals (case) fnew Orange();return f;public class FruitDeac public static void mam (String args) Frm:actory.gftFruir(wapplew) .eat ();tOSGi公然快OSGi略歡OSGi畛麒virgo麹OSGi聘鶴,为却靛聘osGigm面向服务的工厂模式,把接口(com.alon.osgifruit.interf)划为公 共模块;实现类(com.alon.osgifruit.apple 和 com.alon.osgifruit.oranger) 划为服务模块;为接口指定服务的工作交给Virgo服务器进行模块装 卸;调用服务部分(com.alon.osgifruit.apps)被单独划作web应用模OSGi服务模块OSGi应用模块&/ leb Resources WebContent 上 but IdSVtbConleatS 1c/ eg eloxt os(i frux t int”SISprixic Elements曰上sre三 三 coe. aloxt osgifruit. interf 上 Fruit, java 三 KTA-IWS M JKE SysteB Librtry jdkl. 7.0 Bundi Dpndnci Virjo Web Server B eg tlon os(ifruit, ortnftr ffl 卫 Sprint ElAntsv2 1 曰 4 xrcS tb coe tig sjifruit 0丫5丫 田 d 0ran(r jtvas 匚 inA-iwffi JRE Syitta Library jdkl 7 0田 Bundle Deptndtnciei Virjo Web Server (Runt) v2 1吕口:cm tlon osciltii dbvtlidateggouiuit vaidu virgo装卸OSGi服务模块为接口指疋服务coe tlon ositst. validtie. webOSGi公共模块OSGi服务模块i* Markers E Prptrtitii. Servers建 D.|曰筆com lon oi(i frm t tppltIco* tlon. os(xfruit apps4# eg. al on osp frm t ortnerVir(o Web Srvr t lctlhost Stopped块。以下是各bundle模块的结构简图。E cob. aloxL osgifruit, appleS Spring ElementsB 4 sre- 田 coa tlon os(ifruitSB .J Aplt. jraS & inA-IMF田 JBE Systs Library jdkl . 7 0S Bundle Dp*ndncis Virjo Wtb Servtr iRuntw) v2 1 曰coa tlon八 apps田 0 Sprint Elents fl Deploy*nt Descriptor: com. frui t ppsffi Jj Frm tDo. jtva囹 LibrariesE K. JavaScript Resources显而易见,OSGi是把java代码块变成了相应的模块。虽然把简单 的事情变繁杂,但这样会使穆序粒度更细,封装更完全,解耦性更好, 服务扩展更具灵活性。二用OSGi技术进行工厂模式设计,剖析模块间的依赖关系各模块通过MANIFEST.MF文件导出本模块问包,导入支持运彳亍 的jar包。通过配置文件定制、发布、调用模块提供的服务。由此有 效组织和协调系统的服务部署。为了深入了解OSGi运行机制,笔者 把而向对象的“水果”实例改用OSGi技术进行而向服务的工厂模式设计,剖析模块间的依赖关系,加深对接口调用服务即程序“热拔插”的OSGi技术特色的认识。1,开发准备卄发工具:spring 社区提供的 EclipseSpringSource Tool Suite 2.5.0. RELEASEweb 服务器:前身是 springDM 的 virgo-tomcat-server-3.0.2.RELEASE支持包:共有49个包1个库。限于篇幅和阅读习惯(包括下面各模块的源代码),不宜列出。如需支持包和源代码可致电笔者15697365593c部署支持包:把包拷贝到virgo-tomcat-server-3.0.2.RELEASErepositoryext 或usr文件夹下。2,开发过程A,创建接 口模块 com.alon.osgifruit.interf文件结构和接口 Fruit.java代码Q Project Explorer戸Jj Fruit, javapackage com.alon.03gLfruLtLnterf;甲0:日com. al on. osgiru it. apple com. al on osgifiu it. apps com. al on. osgi&u it. int erfSpring Elementssrc日 出 com. alon. osgifruit interf 日7 Fruit, javaS-O FruitB & META-INF:电 MANIFEST. MFpublic interface Fruit public String eaz ();6 7MANIFEST.MF配置文件,导出接口包Project Explorercom. al on. osgi fru it. intra-:.-:日com. al on. osgi&uit apple com. al on. osgifru it. apps com. al oil osgi fru it. intwr Q Spring Elements 已src3 出 com. al on. osgi fru it. inter B i Frui t. javaS O Fruit直 C META-IHF水 MANIFEST. MFProject Explorer & 、- El鶴丨+ com. al on. osgi fruit, apple E 俘 com. al on. osgi fru it. apps 三 com. al on. osgi frui t inter 吵口 Jem:a-B.s-s-ffl- 8/ Spring Elements 曰srcj-j 说 com. al on. osgi fru it. i nt erfS-j) Fruit, javaE O Fruit 自 0 META-IHF半 MANIFEST. MF JRE System Library jdkl. 7. 0Bundle Dependencies Virgo Web Serve com. al on. osgi fr uit. orangr com. al on. osg it est dbvalida te com. al on. osgi test, vali date com. al on. osgi t est. vali date web com. al on. osgi t est. xmlvalidaterui
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号