转自:http://www.cnblogs.com/phoebus0501/archive/2011/03/02/1969361.html
对osgi有了一个初步的了解之后,准备写段代码跑跑,一试身手,
先下载了一份Bluedavy 的《OSGI实战》
里边有可以直接运行的代码,双击run.bat运行正常,暗爽!
开始练习《OSGI实战》中用户登录验证模块,一行一行敲代码,第一个变化就是工程之间相互引用不能在Build path里添加工程引用了,改成了在MANIFEST.MF当中添加Import-Package
在学习过程当中还是遇到了不少问题,记录下来,帮助遇到和我同样样问题的少走弯路。
我用的是eclipse3.4 jdk1.6
1.Import-Package时org.eclipse.equinox.servlet.api这个包死活找不到。
在eclipse3.4已经不存在了直接导入javax.servlet_2.4.0.v200806031604.jar就可以了
如果没有添加javax.servlet会出现 INSTALLED UserValidatorWebBundle_1.0.0
强行启动会抛出以下异常
org.osgi.framework.BundleException: The bundle could not be resolved. Reason: Missing Constraint: Require-Bundle: javax.servlet; bundle-version="2.4.0"
2.如果使用了Equinox OSGI Declarative Service需要下载 eclipse-equinox-SDK-3.4.2.zip
因为Declarative Service实现并没有包含在Eclipse的默认软件包中,需要单独从 Eclipse 的的网站上获得,下载包当中的plugins和features复制到eclipse当中重启
org.eclipse.equinox.ds_1.0.0.v20080427-0830.jar是运行时用到的bundle
org.eclipse.equinox.ds用到了org.eclipse.equinox.util_1.0.0.v20080414.jar
都要在config.ini当中添加并启动
3.一切看似启动正常了,log当中还是有以下异常
java.lang.IllegalStateException: Unable to acquire application service. Ensure that the org.eclipse.core.runtime bundle is resolved and started (see config.ini).
还得启动 org.eclipse.osgi.util_3.1.300.v20080303.jar 这个bundle
4.如果http://localhost/demo/page/login.htm 这个页面不能访问可能org.eclipse.equinox.http_1.0.200.v20080421-2006.jar没有启动,如何htm能访问了http://localhost/demo/login 不能访问 可能org.eclipse.equinox.http.servlet_1.0.100.v20080427-0830.jar没有启动
总结一下用户登录验证模块要启动的bundle
id State Bundle
0 ACTIVE org.eclipse.osgi_3.4.2.R34x_v20080826-1230
1 ACTIVE ConfigFileValidatorBundle_1.0.0
2 ACTIVE DBValidatorBundle_1.0.0
4 ACTIVE UserValidatorBundle_1.0.0
5 ACTIVE LDAPValidatorBundle_1.0.0
9 ACTIVE UserValidatorWebBundle_1.0.0
10 ACTIVE org.eclipse.equinox.util_1.0.0.v20080414
11 ACTIVE org.eclipse.equinox.ds_1.0.0.v20080427-0830
12 ACTIVE javax.servlet_2.4.0.v200806031604
13 ACTIVE org.eclipse.osgi.services_3.1.200.v20071203
14 ACTIVE org.eclipse.equinox.http.servlet_1.0.100.v20080427-0830
15 ACTIVE org.eclipse.equinox.http_1.0.200.v20080421-2006
17 ACTIVE org.eclipse.osgi.util_3.1.300.v20080303
当缺少什么bundle时,自己去网上下就行,然后用导入Plug- ins and Fragments的方式将bundle导入,便可以引用了,所以别当成什么高深的东西,别忘了在运行时用"Add Required Bundles",它帮我们加入会用的的bundles,这个就帮我们很大的忙了.
在第一个例子(源码中的classic)中,用代码注册的方式来注册服务,即在每个实现服务接口的bundle里的Activator注册自己的服务到指定服务名下,如
- package org.riawork.demo.user.validator;
- /*
- * RIAWork.org
- *
- * OSGI Opendoc Demo
- */
- import org.osgi.framework.BundleActivator;
- import org.osgi.framework.BundleContext;
- import org.osgi.framework.ServiceRegistration;
- import org.riawork.demo.service.user.Validator;
- import org.riawork.demo.service.user.impl.ConfigFileValidatorImpl;
- /**
- * desc: ConfigFileBundle Activator,采用传统的方式完成服务的注册
- *
- * @author jerry
- */
- public class Activator implements BundleActivator {
- // --------------------------------------------Instance Variables
- private ServiceRegistration serviceReg=null;
- // --------------------------------------------Public Method
- /* (non-Javadoc)
- * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
- */
- //实现Validator的Bunble都会在Activator里将自己的新的实现注册到Validator.class.getName()名下,这样Validator.class.getName()为名的服务就有多个了
- public void start(BundleContext context) throws Exception {
- serviceReg=context.registerService(Validator.class.getName(), new ConfigFileValidatorImpl(), null);
- }
- /* (non-Javadoc)
- * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
- */
- public void stop(BundleContext context) throws Exception {
- if(serviceReg!=null)
- serviceReg.unregister();
- }
- }
package org.riawork.demo.user.validator; /* * RIAWork.org * * OSGI Opendoc Demo */ import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.riawork.demo.service.user.Validator; import org.riawork.demo.service.user.impl.ConfigFileValidatorImpl; /** * desc: ConfigFileBundle Activator,采用传统的方式完成服务的注册 * * @author jerry */ public class Activator implements BundleActivator { // --------------------------------------------Instance Variables private ServiceRegistration serviceReg=null; // --------------------------------------------Public Method /* (non-Javadoc) * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ //实现Validator的Bunble都会在Activator里将自己的新的实现注册到Validator.class.getName()名下,这样Validator.class.getName()为名的服务就有多个了 public void start(BundleContext context) throws Exception { serviceReg=context.registerService(Validator.class.getName(), new ConfigFileValidatorImpl(), null); } /* (non-Javadoc) * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext context) throws Exception { if(serviceReg!=null) serviceReg.unregister(); } }
在第二个例子(源码中的ds),采用的是在配置文件里声明的方式来发布一个bundle里的服务的,即在 ConfigFileValidatorBundle,DBValidatorBundle,LDAPValidatorBundle这三个实现 UserValidatorBundle接口的bundle里没有Activator来注册它们的实现服务,而是在MANIFEST.MF配置文件里加入 Service-Component: OSGI-INF/component.xml来发布声明在项目目录下的OSGI-INF/component.xml配置文件的服务。
打包时,按着文档来做是不行的,打包的时候,org.eclipse.osgi的jar包和run.bat是在一个目录里,而在它们目录下再建 configuration目录(存放config.ini文件)和bundles目录(存放自己打包出来的bundle和它们运行依赖的 bundle),还有一点在注意的是拷贝文档里的config.ini内容到config.ini文件是不能运行的,可以是有中文字符在里面。
我这里能运行的如下:
- osgi.noShutdown=true
- # 当前系统下运行的 Bundle,可以在此指定 Bundle 的启动顺序,在后续的
- # StartLevel Service章节中会详细的介绍
- #避免Unable to acquire application service. Ensure that the org.eclipse.core.runtime错误
- eclipse.ignoreApp=true
- #osgi.bundles=reference\:file\:bundles/ConfigFileValidatorBundle_1.0.0.jar@start,reference\:file\:bundles/DBValidatorBundle_1.0.0.jar@start,reference\:file\:bundles/LDAPValidatorBundle_1.0.0.jar@start,reference\:file\:bundles/org.eclipse.equinox.http_1.0.301.R35x_v20090728.jar@start,reference\:file\:bundles/javax.servlet_2.5.0.v200806031605.jar@start,reference\:file\:bundles/org.eclipse.osgi.services_3.2.0.v20090520-1800.jar@start,reference\:file\:bundles/UserValidatorBundle_1.0.0.jar@start, reference\:file\:bundles/UserValidatorWebBundle_1.0.0.jar@start
- osgi.bundles=plugins/ConfigFileValidatorBundle_1.0.0.jar@start,\
- plugins/DBValidatorBundle_1.0.0.jar@start,\
- plugins/LDAPValidatorBundle_1.0.0.jar@start,\
- plugins/org.eclipse.equinox.http_1.0.301.R35x_v20090728.jar@start,\
- plugins/javax.servlet_2.5.0.v200806031605.jar@start,\
- plugins/org.eclipse.osgi.services_3.2.0.v20090520-1800.jar@start,\
- plugins/UserValidatorBundle_1.0.0.jar@start,\
- plugins/UserValidatorWebBundle_1.0.0.jar@start,\
- plugins/org.eclipse.equinox.ds_1.1.1.R35x_v20090806.jar@start,\
- plugins/org.eclipse.equinox.util_1.0.100.v20090520-1800.jar@start
- osgi.bundles.defaultStartLevel=4
osgi.noShutdown=true # 当前系统下运行的 Bundle,可以在此指定 Bundle 的启动顺序,在后续的 # StartLevel Service章节中会详细的介绍 #避免Unable to acquire application service. Ensure that the org.eclipse.core.runtime错误 eclipse.ignoreApp=true #osgi.bundles=reference\:file\:bundles/ConfigFileValidatorBundle_1.0.0.jar@start,reference\:file\:bundles/DBValidatorBundle_1.0.0.jar@start,reference\:file\:bundles/LDAPValidatorBundle_1.0.0.jar@start,reference\:file\:bundles/org.eclipse.equinox.http_1.0.301.R35x_v20090728.jar@start,reference\:file\:bundles/javax.servlet_2.5.0.v200806031605.jar@start,reference\:file\:bundles/org.eclipse.osgi.services_3.2.0.v20090520-1800.jar@start,reference\:file\:bundles/UserValidatorBundle_1.0.0.jar@start, reference\:file\:bundles/UserValidatorWebBundle_1.0.0.jar@start osgi.bundles=plugins/ConfigFileValidatorBundle_1.0.0.jar@start,\ plugins/DBValidatorBundle_1.0.0.jar@start,\ plugins/LDAPValidatorBundle_1.0.0.jar@start,\ plugins/org.eclipse.equinox.http_1.0.301.R35x_v20090728.jar@start,\ plugins/javax.servlet_2.5.0.v200806031605.jar@start,\ plugins/org.eclipse.osgi.services_3.2.0.v20090520-1800.jar@start,\ plugins/UserValidatorBundle_1.0.0.jar@start,\ plugins/UserValidatorWebBundle_1.0.0.jar@start,\ plugins/org.eclipse.equinox.ds_1.1.1.R35x_v20090806.jar@start,\ plugins/org.eclipse.equinox.util_1.0.100.v20090520-1800.jar@start osgi.bundles.defaultStartLevel=4
这里不要忘记加入ds的bundle不然用Service-Component: OSGI-INF/component.xml配置就不起作用了,发布不了服务的,因为是由ds的bundle来扫描发现相应的服务,并加于管理的。
---------------------------------------------------------------------------------------------------------
在之前的文章中介绍了部署和应用OSGI系统,但是文章中介绍的是手动部署和管理方式。今天介绍的主要就是自动查找Bundle和安装部署的应用,还是以Equinox为实现框架来介绍,同时也参考了Eclipse官方的http://www.eclipse.org/equinox/documents/quickstart.php介绍,有兴趣的朋友可以自行参阅该文档。 首先介绍的是如何让Equinox自动发现新Bundle并部署。首先新建立一个目录demo,在该目录下建立如下文件结构:
-
demo(目录)
-
|--configuration(目录)
-
|--config.ini(文件)
-
|--plugins(目录)
-
|--org.eclipse.osgi_3.4.0.v20080605-1900.jar(文件)
-
|--org.eclipse.equinox.common_3.4.0.v20080421-2006.jar(文件)
-
|--org.eclipse.update.configurator_3.2.200.v20080417.jar(文件)
-
|--startup.bat(文件)
然后在config.ini中加入如下内容:
-
osgi.bundles=org.eclipse.equinox.common@2:start,org.eclipse.update.configurator@3:start
-
eclipse.ignoreApp=true
-
osgi.noShutdown=true
在startup.bat中加入如下内容:
-
java -jar org.eclipse.osgi_3.4.0.v20080605-1900.jar -console pause
这里主要是用到了common和configurator(主要是这个)两个Bundle来自动发现和部署plugins目录下的Bundle,所以,如果有新的Bundle需要发布,只需要放到plugins目录下,然后重新启动OSGI即可。我们在config.ini文件中设置了common的启动级别为2,configurator的启动级别为3,这样,OSGI框架启动后它们将被最先启动,因为Equinox的默认启动级别为4,所以plugins目录下的所有Bundle的启动级别为4。
相关推荐
其后进入OSGi实战,结合实例讲解如何基于OSGi框架编写模块化、动态化的各种Java应用;最后对OSGi知识进行深入讲解,通过对OSGi规范和实现框架(Equinox、Felix、Spring-DM和Apache CXF)的分析,以及最佳实践的介绍...
### OSGI原理及实战知识点概述 #### 一、OSGI简介 - **定义**: OSGI(Open Service Gateway Initiative)是一种动态模块化系统标准,主要用于构建Java平台上的复杂应用程序和服务网关。它通过提供一个模块化框架,...
总之,这个压缩包和相关博客文章为我们提供了一个学习OSGi实战经验的机会,涵盖了从基础概念到具体实践的多个方面。通过研究源码和工具,我们可以深入理解OSGi如何实现模块化,以及如何利用这一技术来构建更灵活、更...
通过阅读《OSGi原理与最佳实践》和《OSGI实战》这两本书,可以深入了解OSGi的细节,学习如何在实际项目中有效地使用OSGi,解决上述挑战,并充分利用其优势。这些书籍通常会涵盖OSGi的配置、服务注册与查找、打包规范...
OSGI实战部分可能会涵盖如何创建、配置和部署bundle。开发者需要学习如何编写遵循OSGI规范的代码,使用MANIFEST.MF文件声明bundle的依赖关系和服务。此外,可能还会介绍使用工具,如Eclipse Equinox或Apache Felix,...
6. "OSGI实战及例子(普通下载).rar":这个资源可能包含更多的实战项目和示例代码,对于动手实践非常有帮助。 通过对这些书籍的学习和实践,开发者能够全面理解OSGi的机制,掌握如何在项目中有效应用OSGi,从而构建...
这通常意味着我们将讨论如何在OSGI环境中利用Spring框架的优点,如服务发现、依赖管理以及在动态环境中部署和管理组件。 在标签中,"OSGISpring"进一步确认了主题,我们聚焦于OSGI与Spring的集成技术。 压缩文件...
#### 面向C/S应用的OSGi实战 本节主要介绍了几种流行的OSGi实现者及其具体应用实例,包括Eclipse Equinox、Apache Felix和Knopflerfish。 **Eclipse Equinox** - **概述**:Eclipse Equinox是Eclipse项目中的一个...
"OSGi原理与最佳实践"这本书可能会详细讲解以上各个方面,并可能包含实例代码和实战经验分享,对于深入理解OSGi并应用到实际项目中非常有帮助。另外,书中可能还会涵盖一些高级话题,如服务事件、远程服务、蓝绿部署...
5. **OSGi实战.rar**: 这可能是一个包含实际项目案例的压缩包,提供了使用OSGi进行开发的实例教程。通过这些实战案例,开发者可以学习如何将理论知识应用于实际开发,解决具体问题,如如何构建模块化的Web应用,...
6. 实战应用:可能通过实例讲解如何在实际项目中配置Maven和OSGi,例如创建和管理OSGi服务,或者在大型分布式系统中利用OSGi的动态性进行模块化设计。 综上所述,“maven+osgi”这个主题涵盖了Java开发中的两个重要...
6. 示例和实战:提供实际的代码示例,演示如何创建、部署和管理OSGI模块化的Spring-Hibernate应用。 通过对这份手册的深入学习,开发者将能够掌握在OSGI环境下使用Spring和Hibernate进行高效、灵活的Java应用开发。
本文档主要面向已经了解OSGi基本概念或已阅读过《OSGi实战》一书的读者。适合希望深入了解OSGi框架及其高级应用的技术人员。 ##### 1.2 编写目的 文档旨在通过具体案例深入讲解OSGi框架的应用技巧,帮助读者掌握...
6. **源代码分析**:提供的源代码可能包含示例应用、模块示例以及服务实现等,这些代码实例将帮助读者更好地理解和应用OSGI技术。通过分析这些代码,读者可以学习到如何在实际项目中应用OSGI,例如如何定义Bundle、...
- **插件体系结构**:解释Eclipse的插件模型,如OSGi框架,以及插件之间的依赖关系。 - **开发插件**:指导如何使用Eclipse的插件开发工具PDE,创建自己的功能插件,并发布到Eclipse Marketplace。 5. **版本控制...
4. **源码分析**:通过书中提供的源码实例,学习如何编写和组织OSGi模块,以及如何处理模块间的依赖关系。 5. **开发工具**:掌握使用Eclipse等IDE进行OSGi开发的方法,以及Bndtools等工具的使用技巧。 6. **动态...
通过丰富的实例,作者引领读者深入到Spring dm Server的内部机制,学习如何构建、部署和管理现代Java应用程序。 OSGi是Java平台上的一个模块化系统和动态服务框架,它为创建可重用和可组合的组件提供了标准。Spring...
soa/SCAWSClient.zip //19.SCA客户端实例——HelloWorld实例 soa/OSGiHelloWorld.zip.zip //21.OSGi实例——HelloWorld实例 soa/demo.sql //24.SOA服务架构实战——企业信息管理系统数据库脚本 soa/ssh2.zip //24....
soa/OSGiHelloWorld.zip.zip //21.OSGi实例——HelloWorld实例 soa/demo.sql //24.SOA服务架构实战——企业信息管理系统数据库脚本 soa/ssh2.zip //24.SOA服务架构实战——企业信息管理系统SSH2代码 soa/...
soa/OSGiHelloWorld.zip.zip //21.OSGi实例——HelloWorld实例 soa/demo.sql //24.SOA服务架构实战——企业信息管理系统数据库脚本 soa/ssh2.zip //24.SOA服务架构实战——企业信息管理系统SSH2代码 soa/...