OSGi (第一部分): Bundles入门(续,翻译)
2009-03-04 10:06
344 查看
OSGi (第一部分): Bundles入门(续,翻译)
4. 依赖性管理 OSGi允许您把您的应用程序分成多个模块,并能管理这些模块之间的依赖性。为了达到这个目的,它引入了Bundle访问域的概念。Bundle中类的缺省访问范围只对本Bundle内部可见,但对其它任何Bundle都是不可见的;在Bundle内部,类的可访问性遵循Java语言的一般规范。那么,您如果想要从一个Bundle中访问另一个Bundle中的类,您应该怎么办呢?解决方法是将源Bundle中的包导出来,然后把它们导入到目标Bundle中。在本小结中,我们将通过一个示例程序说明这个概念。 首先,我们新建一个名com.javaworld.sample.HelloService的Bundle,并从其中导出一个包,然后将该包导入到我们的com.javaworld.sample.HelloWorld Bundle中。 4.1. 导出Java包 我们开始新建一个com.javaworld.sample.HelloServiceBundle,并从其中导出一个Java包,具体步骤如下: 1) 新建com.javaworld.sample.HelloService Bundle,具体步骤请参见上小节中新建com.javaworld.sample.HelloWorldBundle的步骤; 2) 在HelloService Bundle中,新建一个com.javaworld.sample.service.HelloService.java接口,其源代码如清单3所示。 源代码清单3. HelloService.java package com.javaworld.sample.service; public interface HelloService { public String sayHello(); } 3) 新建类com.javaworld.sample.service.impl.HelloServiceImpl.java,该类实现HelloService接口,其源代码如清单4所示。 源代码清单4. HelloServiceImpl.java package com.javaworld.sample.service.impl; import com.javaworld.sample.service.HelloService; public class HelloServiceImpl implements HelloService { public StringsayHello() { System.out.println("InsideHelloServiceImple.sayHello()"); return"Say Hello"; } } 4) 请在您的Eclipse Manifest编辑器中打开HelloService包中的MANIFEST.MF文件,点击“Runtime(运行时)” 标签,在“导出包”小节,单击“Add(添加)”按钮,并选择com.javaworld.sample.service包。这时,HelloServiceBundle中的MANIFEST.MF文件代码应如源代码清单5所示。 源代码清单5. HelloService Bundle中的Manifest文件 Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: HelloService Plug-in Bundle-SymbolicName:com.javaworld.sample.HelloService Bundle-Version: 1.0.0 Bundle-Vendor: JAVAWORLD Bundle-Localization: plugin Export-Package: com.javaworld.sample.service Import-Package:org.osgi.framework;version="1.3.0" 您可以看到,HelloService Bundle中的MANIFEST.MF文件和HelloWorldBundle非常相似,唯一的区别就是多了一个Export-Package属性头,该属性头的值为com.javaworld.sample.service;Export-Package属性头通知OSGi容器,其它Bundle可以从HelloService Bundle外面访问com.javaworld.sample.service包中的类。请注意,在示例代码中,我们只暴露了接口类HelloService,而没有暴露其实现类的HelloServiceImpl。 4.2. 导入Java包 下面,我们将从HelloServiceBundle中导出的com.javaworld.sample.service包并将其导入到HelloWorldBundle中,具体步骤如下: 1). 请在com.javaworld.sample.HelloWorld Bundle中找到MANIFEST.MF文件,并在Manifest编辑器中打开,点击“Dependencies(依赖性)”标签,然后点击“ImportPackage(导入包)”按钮,将com.javaworld.sample.service添加为导入包,这时,您的HelloWorldBundle中的MANIFEST.MF文件内容应如源代码清单6所示: 源代码清单6. HelloWorld Bundle中的MANIFEST.MF文件 Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: HelloWorld Plug-in Bundle-SymbolicName: com.javaworld.sample.HelloWorld Bundle-Version: 1.0.0 Bundle-Activator: com.javaworld.sample.helloworld.Activator Bundle-Vendor: JAVAWORLD Bundle-Localization: plugin Import-Package: com.javaworld.sample.service, org.osgi.framework;version="1.3.0" 从上面的代码可以看出,Import-Package属性头的值是一个由逗号分隔的字符串,这是您想导入包的列表。在HelloWorldBundle示例代码中,我们引入了两个包,即com.javaworld.sample.service和org.osgi.framework。 org.osgi.framework包中包含有OSGi框架类,比如,在HelloWorldBundle中的Activator.java中用到的BundleContext和BundleActivator类都属于这个包。 2) 下面,请在Eclipse Java编辑器中打开com.javaworld.sample.helloworld.Activator.java,您会注意到,您现在可以访问HelloService接口,但不能访问HelloServiceImpl实现类,这是因为HelloServiceBunlde只导出了com.javaworld.sampel.service包,同时HelloWorldBundle也导入了这个包。HelloServiceImpl是HelloServiceBundle的一个内部类,任何其它的Bundle都不能访问它。 4.3. 类级别上的访问域 如果您运行示例的HelloService服务包,它会在Eclipse控制台上打印出”HelloWorld”。但是,如果您想在HelloWorld Bundle的Activator中访问HelloServiceImpl类,这时,编译没有问题,但在OSGi容器中运行这个Bundle时会抛出异常。 OSGi容器是如何能将jar文件中的一些类隐藏掉,而让另外一些类可见呢?这是因为OSGi容器使用Java类加载器来管理类的可见性,OSGi容器为每个Bundle创建不同的类加载器,因此每个Bundle能访问位于下列位置中的类: a) 位于Java启动类路径下的、所有以Java.*开头的包中的类; b) 位于OSGi框架类路径下的类,通常有一个独立的类加载器负责加载框架的实现类及关键的接口类; c) 位于Bundle空间中的类,这些类通常包含在与Bundle相关的jar文件中,以及加到这个Bundle中的其它jar包中的类。 d) 导入包中的类,例如,HelloWorld Bundle导入了com.javaworld.sample.service包,因此它能访问该包中的类。Bundle级别的访问域是OSGi一个非常强大的功能,例如,它可以让您安全地更新HelloServiceImpl.java类,而不必担心依赖于这个类的代码受到破坏。 5. OSGi服务 前面我们提到,OSGi架构非常适合我们实现面向服务的应用(SOA)。它可以让Bundles导出服务,而其它的Bundles可以在不必了解源Bundles任何信息的情况下消费这些导出的服务。由于OSGi具有隐藏真实的服务实现类的能力,所有它为面向服务的应用提供了良好的类与接口的组合。 在OSGi框架中,源Bundle在OSGi容器中注册POJO对象,该对象不必实现任何接口,也不用继承任何超类,但它可以注册在一个或多个接口下,并对外提供服务。目标Bundle可以向OSGi容器请求注册在某一接口下的服务,一旦它发现该服务,目标Bundle就会将该服务绑定到这个接口,并能调用该接口中的方法。下面我们举个例子,以便我们能更好理解与OSGi相关的这些概念。 5.1. 导出服务 在本小节中,我们将更新HelloService Bundle,以便它能把HelloServiceImpl类的对象导出为服务,具体步骤如下: 1) 修改com.javaworld.sample.HelloService Bundle中的MANIFEST.MF文件,让它导入org.osgi.framework包(译者注,这一步我们已经完成); 2) 新建Java类com.javaworld.sample.impl.HelloServiceActivator.java,其源代码如清单7所示; 源代码清单7. HelloServiceActivator.java publicclass HelloServiceActivator implements BundleActivator { ServiceRegistrationhelloServiceRegistration; public void start(BundleContext context)throws Exception { HelloService helloService = newHelloServiceImpl(); helloServiceRegistration=context.registerService(HelloService.class.getName(), helloService, null); } public void stop(BundleContext context)throws Exception { helloServiceRegistration.unregister(); } } 请注意,在源Bundle中,我们应使用BundleContext.registerService()方法导出服务,这个方法带三个参数: a) 该方法第一个参数为您要注册的服务的接口名称。如果您想把您的服务注册到多个接口下,您需要新建一个String数组存放这些接口名,然后把这个数组作为第一个参数传给registerService()方法。在示例代码中,我们想把我们的服务导出到HelloServer接口名下; b) 第二个参数是您要注册的服务的实际Java对象。在示例代码中,我们导出HelloServiceImpl类的对象,并将其作为服务; c) 第三个参数为服务的属性,它是一个Dictionary对象。如果多个Bundle导出服务的接口名相同,目标Bundle就可以使用这些属性对源Bundle进行过滤,找到它感兴趣的服务。 3) 最后,请修改HelloServiceBundle中的MANIFEST.MF文件,将Bundle-Activator属性头的值改为com.javaworld.sample.service.impl.HelloServiceActivator。 现在HelloService Bundle就可以导出HelloServiceImpl对象了。当OSGi容器启动HelloServiceBundle时,它会将控制权交给HelloServiceActivator.java类,HelloServiceActivator将HelloServiceImpl对象注册为服务。下面,我们开始创建该服务的消费者。 5.2. 导入服务 在本小节中,我们将修改上面开发的HelloWorld Bundle,以便让它成为HelloService服务的消费者。您主要需要修改HelloWorldBundle中的Activator.java代码,修改后的代码如源代码清单8所示: 源代码清单8. HelloWorld Bundle中的Activator.java packagecom.javaworld.sample.helloworld; importorg.osgi.framework.BundleActivator; importorg.osgi.framework.BundleContext; importorg.osgi.framework.ServiceReference; importcom.javaworld.sample.service.HelloService; publicclass Activator implements BundleActivator { ServiceReference helloServiceReference; public void start(BundleContext context)throws Exception { System.out.println("HelloWorld!!"); helloServiceReference=context.getServiceReference(HelloService.class.getName()); HelloService helloService=(HelloService)context.getService(helloServiceReference); System.out.println(helloService.sayHello()); } public void stop(BundleContext context)throws Exception { System.out.println("Goodbye World!!"); context.ungetService(helloServiceReference); } } 在上面的代码中,BundleContext.getServiceReference()方法将为注册在HelloService接口下的服务返回一个ServiceReference对象。如果存在多个HelloService服务,该方法会返回排行最高的服务(服务的排行是通过Constants.SERVICE_RANKING属性指定的)。您一旦获得ServiceReference对象,您就可以调用其BundleContext.getService()方法获取真实的服务对象。 您可以参照运行Bundle的方法运行上面的示例应用,请点击“RunàRun…”菜单,并确保HelloWorld和HelloService这两个Bundle被选中。当您启动HelloServiceBundle时,您会在控制台上看到“InsideHelloServiceImple.sayHello()”,这个消息是由HelloServiceImpl.sayHello()方法打印出来的。 5.3. 创建服务工厂 在上节中,我们学会了如何使用OSGi框架新建一个Java对象,并把它注册为一个服务,然后让其它的Bundle去消费这个服务。如果您看一下HelloServiceActivator.start()方法,您会注意到我们在start()方法中新建了HelloServiceImpl类对象,然后将它注册到HelloService接口名下。这样注册后,任何其它的Bundle在请求HelloService服务时,OSGi容器将返回同一对象。 在大多数情况下,这样的实现方法没有问题。但是,比如说我们要为每一个Bundle消费者返回不同的HelloServiceImpl对象,再比如说,您的服务对象要提供的服务为打开一个数据库连接,但并不是马上就打开它,而是在真正需要的时候才打开这个数据库连接。 对这两种情况,我们的解决方法是,新建一个类实现ServiceFactory接口,并把该类的对象注册为服务,但并不是注册实际的服务对象。一旦您完成这一步,其它Bundle在请求该服务时,您的ServiceFactory实现类将接管该请求,ServiceFactory会为每个Bundle新建一个服务对象,并将真实服务的创建时间延迟到有人真正需要该服务的时候。 下面我们将使用ServiceFactory更新我们上面开发的com.javaworld.sample.HelloServiceBundle,具体步骤如下: 1) 新建工厂 类HelloServiceFactory.java,源代码如清单9所示。 源代码清单9 . HelloServiceFactory.java publicclass HelloServiceFactory implements ServiceFactory{ private int usageCounter = 0; public Object getService(Bundle bundle,ServiceRegistration registration) { System.out.println("Create objectof HelloService for " + bundle.getSymbolicName()); usageCounter++; System.out.println("Number ofbundles using service " + usageCounter); HelloService helloService = newHelloServiceImpl(); return helloService; } public void ungetService(Bundle bundle,ServiceRegistration registration, Object service) { System.out.println("Release objectof HelloService for " + bundle.getSymbolicName()); usageCounter--; System.out.println("Number ofbundles using service " + usageCounter); } } 从上面的代码中,我们可以看到,ServiceFactory接口定义了两个方法: a) getService()方法:当某个Bundle第一次使用BundleContext.getService(ServiceReference)方法请求一个服务对象时,OSGi框架会调用该方法。在源代码清单9中,我们用这个方法为每个Bundle新建并返回不同的HelloServiceImpl对象,如果这个对象不是null,OSGi框架会缓存这个对象。如果同一个Bundle再次调用BundleContext.getService(ServiceReference)方法,OSGi将返回同一个服务对象。 b) ungetService()方法:当Bundle释放服务时,OSGi容器可以调用该方法销毁服务对象。在源代码清单9中,我们使用usageCounter变量来跟踪服务的使用数目,并打印出该服务的客户端数量。 2) 修改HelloService Bundle中的HelloServiceActivator.java的start()方法,让它注册到ServiceFactory接口名下,而不是注册到HelloService接口。详细代码如清单10所示: 源代码清单10. 修改后的HelloServiceBundle中的HelloServiceActivator.java packagecom.javaworld.sample.service.impl; importorg.osgi.framework.BundleActivator; importorg.osgi.framework.BundleContext; importorg.osgi.framework.ServiceRegistration; importcom.javaworld.sample.helloservice.HelloServiceFactory; importcom.javaworld.sample.service.HelloService; publicclass HelloServiceActivator implements BundleActivator { ServiceRegistrationhelloServiceRegistration; public void start(BundleContext context)throws Exception { HelloServiceFactory helloServiceFactory= new HelloServiceFactory(); helloServiceRegistration=context.registerService(HelloService.class.getName(), helloServiceFactory,null); } public void stop(BundleContext context)throws Exception { helloServiceRegistration.unregister(); } } 现在,您可以试运行示例代码。您会注意到,当HelloWorld Bundle启动时,服务计数器变为1;当HelloWorldBundle停止时,服务计数器的数目将变为0。 5.4. 跟踪服务 在“OSGi服务”小节,您学会了如何使用服务的接口名搜索服务。但如果有多个Bundle使用同一接口名注册服务,那会发生什么呢?这时,OSGi容器将返回排行最高的服务,即,返回注册时那个SERVICE_RANKING属性值最大的服务。如果有多个服务的排行值相等,那么OSGi容器将返回PID值最小的那个服务。 但是,如果您的服务消费者需要了解某一接口下的服务对象何时注册、何时取消注册,这时,您应使用ServiceTracker类。下面,我们看看如何使用服务跟踪器来修改我们的示例代码,具体步骤如下。 1) 修改HelloWorldBundle的MANIFEST.MF文件,让它导入org.osgi.util.tracker包; 2) 新建类HelloServiceTracker.java,其源代码参见清单11。 源代码清单11.HelloServiceTracker.java public class HelloServiceTracker extends ServiceTracker { public HelloServiceTracker(BundleContext context) { super(context, HelloService.class.getName(),null); } public Object addingService(ServiceReference reference) { System.out.println("Inside HelloServiceTracker.addingService " + reference.getBundle()); return super.addingService(reference); } public void removedService(ServiceReference reference, Object service) { System.out.println("Inside HelloServiceTracker.removedService " + reference.getBundle()); super.removedService(reference, service); } } 在上面的HelloSerivceTracker类的构造函数中,您可以看到,我们把HelloService接口名传入其父类中,这相当于说,HelloServiceTracker应跟踪注册到HelloService接口名下的所有服务,HelloServiceTracker继承自ServiceTracker类,实现了下面两个方法: a) addingService()方法:当Bundle使用接口名注册服务时,该方法将会被调用; b)removedService()方法:当Bundle取消注册某个接口名下的服务时,该方法将会被调用。 3) 用HelloServiceTracker类更新我们的Activator.java类,以便让它来管理服务,而不是直接去查找它们,源代码请参见清单12。 源代码清单12. 使用了HelloServiceTracker的Activator.java public class Activator implements BundleActivator { HelloServiceTracker helloServiceTracker; public void start(BundleContext context) throws Exception { System.out.println("Hello World!!"); helloServiceTracker= new HelloServiceTracker(context); helloServiceTracker.open(); HelloService helloService = (HelloService)helloServiceTracker.getService(); System.out.println(helloService.sayHello()); } public void stop(BundleContext context) throws Exception { System.out.println("Goodbye World!!"); helloServiceTracker.close(); } } 我们看到,在初始的start()方法中,我们首先新建一个HelloServiceTracker对象,然后要求这个对象跟踪HelloService接口下的服务。这时,我们可以调用getService()方法获得HelloService对象。 如果您试运行上面的示例代码,您会注意到,在启动或停止HelloSerivceBundle时,OSGi容器都会调用HelloServiceTracker对象的addingService()方法或removedService()方法。 6. 结论 这是“你好,OSGi”系列三篇文章的第一篇,我向您介绍了使用OSGi进行模块化应用开发的一些基本概念。您现在已了解到,当前有三种开源的OSGi容器,而且您还练习了如何使用Eclipse自带的OSGi容器Equinox开发一个简单的Bundle;同时,您也学会了Bundle之间是怎样通过导出导入彼此的包和服务,从而达到彼此交互的目的。 在本文中,您也许注意到,开发OSGi Bundle的一个挑战是,您的每个Bundle都需要了解OSGi API,在某些开发场景中,这可能意味着我们要许多基础代码。在本系列的下一篇文章中,我将向您介绍“SpringDynamic Modules for OSGi Service Platforms(OSG服务平台的Spring动态模块,亦称为SpringOSGi)”项目,该项目将会简化OSGi Bundle 的开发。另外,您也可以参考本文的资源部分,学习更多关于OSGi的知识。 7. 作者介绍 Sunil Patil是一位Java企业/门户开发者,现就职于加州三藩市的AscendentTechnology公司。他是Java Portlets 101一书的作者(该书由SourceBeat公司2007年4月出版),而且,他还写了许多文章,通过O’ReillyMedia发表。Sunil曾经在IBM的WebspherePortal Server开发团队工作过3年。现在,他积极参与Pluto社区。另外,他拥有IBM WebspherePortal Server 5.0和5.1版本的开发者证书,Sun 的SCJP证书和Web组件开发者证书,同时,他还是商务组件的开发者。您可以访问Sunil的博客:http://jroller.com/page/SunilPatil。 8. 资源 非官方的OSGi规范开始于1999年,最近,JSR对Java模块化支持分为两个规范,即,JSR 277(Java模块系统,http://jcp.org/en/jsr/detail?id=277 )和JSR 291(Java动态组件支持,http://jcp.org/en/jsr/detail?id=291 )。在这两个JSR规范中,人们对哪个规范应该包含在Java EE6中,有过很多争议,请参考http://www.infoq.com/news/2007/08/osgi-jsr277-debate 。 和许多Java开发者一样,SebastienArbogast最近意识到OSGi的重要性(请参见http://osgi.dzone.com/news/why-osgi-zone ),他在Javalobby上开辟了OSGi专区(请参见http://osgi.dzone.com/ ),作为OSGi相关信息和讨论的平台。 如果您想了解OSGi服务规范第4版的信息,请参考http://www2.osgi.org/Release4/HomePage;关于OSGi服务规范第4版的Java API,请参考http://www2.osgi.org/javadoc/r4/;关于OSG服务平台的Spring动态模块项目,有时也称作Spring OSGi项目,请参考http://www.springframework.org/osgi。 |
相关文章推荐
- 你好,OSGi (第一部分): Bundles入门
- 安卓开发入门之系统下载管理工具DownloadManager-第一部分(翻译)
- Jetty9.4官方文档部分翻译【入门+嵌入式开发】
- Spring5文档翻译(一)-Spring入门和简介
- 翻译 Cesium入门教程(二)------图层 Imagery Layers
- 重温c#入门经典有感(第一部分)
- SSIS入门教程(一) - Execute Process包示例[翻译] Level 100
- [翻译]使用asp.net mvc再造一个digg 第一部分
- 翻译 - Hibernate入门指南
- 翻译:Angular 2 - TypeScript 5 分钟快速入门
- Nutch入门教程,第一部分
- enyo官方开发入门教程翻译一Key Concepts之Event Handling
- enyo官方开发入门教程翻译一Consuming Web Services
- Spring OSGI 快速入门中文教程
- OSGi入门
- OSGI入门笔记
- Kinect帮助文档翻译之一 入门
- Opencascade(OCC)曲面造型 第一部分(翻译Roman Lygin Blog)
- sbt入门指南翻译(1)--渣翻译有错误望指教
- Nutch入门教程,第一部分