Bundle activation/deactivation
Now that bundles don't need to provide BundleActivator class, we still have opportunities to do something at bundle activation/deactivation time by implementing the following methods in component class:
protected void activate(ComponentContext ctxt) {
//initialze something...
}
protected void deactivate(ComponentContext ctxt) {
//release resources...
}
DS surely use reflection to invoke them at the right time.
DS properties
Some componenets need to retrieve customized properties/configurations at runtime. There are the following ways:
1. retrieve from OSGi Configuration Admin service. Equinox hasn't released Configration Admin service bundle.
2. properties specified in the component description. For example:
<component name="MySQLAccess">
<implementation class="com.ibm.csdl.scakm.db.mysql.MySQLAccess"/>
<service>
<provide interface="com.ibm.csdl.scakm.db.DBAccess"/>
</service>
<properties entry="OSGI-INF/db.properties" />
</component>
Here, properties element points to a property file which can hold properties. Then in code the properties could be accessed by ComponentContext.getProperties(). For example:
protected void activate(ComponentContext ctxt) {
jdbcURL = (String) ctxt.getProperties().get("jdbcURL");
user = (String) ctxt.getProperties().get("user");
password = (String) ctxt.getProperties().get("password");
}
The cons of this way is that the property file has to be packaged in the bundle and it's not convinent to modify it.
Actually we can specify properties in config.ini file, and in applications use BundleContext.getProperty() to retrieve them. However, this approach makes the properties global visible to all bundles.
Some tips
1. remember to set DS description property in bundle manifest file:
Service-Component: OSGI-INF/service.xml
2. In development, remember to put DS description file into binary build in build.properties so that the exported bundle jar can include the file:
bin.includes = META-INF/,\
console.jar,\
OSGI-INF/
3. Even DS cannot find required service reference for a bundle, the bundle can still run with activated status. Check console output to make sure no error happens.
4. Service implmenation class will only have one instance in an OSGi runtime, which is to be registered in service registry. If the service component should have multiple instances, each of which is created when a service is needed, a kind of factory component is provided by DS. Never used it, so just go to OSGi specification.
5. Equinox will cache bundle jar files so if bundles get updated and something wierd happens, try to delete configuration/org.eclipse.osgi/ directory and restart again. Avoid update bundle with same version number, and therefore upgrade version numbers as applications are developed.
6. Read OSGi specification to understand all the things in deep dive.
分享到:
相关推荐
在压缩包子文件名"osgi-ds(1-1).zip"和"osgi-ds(1-n).zip"中,每个文件可能包含了不同场景下的DS配置和实现。例如,“1-1.zip”可能包含了一个简单的示例,演示了如何配置一个服务消费者依赖于单个服务提供者的例子...
本章聚焦于OSGi中的Declarative Services(DS),这是一种声明式的服务管理机制,它简化了服务的生命周期管理。 Declarative Services(DS)是OSGi服务模型的一部分,它允许开发者在配置文件中声明服务的依赖关系和...
DS(Declarative Services)是OSGI中的一个核心服务,它提供了声明式的方式来管理和装配服务。而EVENT则是DS中关于事件处理的部分,用于在OSGI组件之间传递信息和协调工作。 OSGI服务是一种动态的服务注册和发现...
本文将围绕"carrot-osgi-anno-scr-make-2.0.1.zip"这个开源项目,深入探讨其在OSGi DS(Declarative Services)中的作用和应用。 "carrot-osgi-anno-scr-make"项目是一个针对OSGi服务组件的工具,主要功能是帮助...
综上,本项目展示了在OSGi环境中如何利用DS和EventAdmin实现用户登录验证,这涉及到服务的声明式管理、事件驱动通信以及模块化的服务设计。通过这样的实践,我们可以更好地理解和运用OSGi框架,提升系统的灵活性和可...
Spring Dynamic Modules (Spring-DS) 是Spring框架在OSGi环境下的扩展,它帮助我们管理Spring应用在OSGi中的生命周期。`spring-osgi-core-1.2.1.jar`是Spring-DS的核心库。在这个场景下,我们可以使用Spring的...
OSGI(Open Services Gateway Initiative)是一种开放标准,用于创建可模块化的Java应用程序。它提供了一种灵活的框架,使得开发者可以构建、部署和管理模块化组件,这些组件被称为服务或bundle。OSGI的核心理念是将...
OSGi(Open Services Gateway Initiative)是一种Java模块化系统,它为开发人员提供了一种动态、模块化的运行时环境。在OSGi中,应用程序被分解为称为“bundle”的独立单元,这些bundle可以相互依赖并独立地加载、...
3. **使用Declarative Services(DS)**:OSGI DS提供了一种声明式的方式来管理OSGI服务的生命周期,使得Spring的bean可以与OSGI服务无缝集成。 4. **动态依赖注入**:由于OSGI的动态性,服务可以在运行时添加或...
OSGI(Open Services Gateway Initiative)是一种模块化系统和Java服务框架,它允许应用程序由一系列可独立更新和替换的模块组成,这些模块称为“bundle”。在本实例中,我们将探讨如何利用OSGI技术来开发Eclipse...
OSGI(Open Services Gateway Initiative)是一种Java模块化系统,它允许开发者将应用程序分解为一系列可独立部署、更新和交互的服务。林昊所著的《OSGI实战》与《OSGI进阶》是深入理解OSGI技术的重要参考资料,适合...
OSGI组件编程是一种在Java平台上构建模块化应用程序的方法,它由OSGi联盟制定标准,并被广泛应用于企业级软件开发,尤其是对于需要高度可扩展性和动态性的系统。在本教程中,我们将深入探讨如何使用Eclipse和Equinox...
OSGi规范中文版是一本全面介绍OSGi技术的书籍,它不仅涵盖了OSGi技术的基础知识,还详细介绍了OSGi的内部结构和工作原理,对于想要深入学习和应用OSGi技术的开发者而言,是一本非常有价值的参考书。 ### OSGi规范的...
OSGI(Open Services Gateway Initiative)是一种开放标准,用于创建模块化和动态的Java应用程序。它为Java开发人员提供了一个框架,使他们能够构建可热插拔的组件,从而实现更灵活、可扩展和可维护的软件系统。在本...
OSGi(Open Services Gateway Initiative)是一种Java平台上的模块化服务框架,它定义了一种标准,使得开发者能够构建可互操作的、动态的、模块化的软件系统。OSGi的核心概念是基于Java的模块化,它的主要目标是为...
HIGHLIGHT OSGi in Action is the definitive guide to OSGi, ... This book is based on hands-on experience with OSGI. Authors have contributed to high-profile OSGi implementations, including Apache Felix.
- **Declarative Services (DS)**:DS 是一种声明式服务配置机制,它允许开发者通过元数据定义服务及其依赖关系,而无需编写额外的代码。 - **Blueprint Container**:Spring OSGi 提供了一个基于 Blueprint 的...
客户端和服务端的实现通常会涉及到OSGi的Declarative Services(DS)或Blueprint,它们是声明式服务配置的方式,通过XML文件定义服务的依赖和行为。在DS中,我们可以使用`@Component`和`@Reference`注解来声明服务...
资源名称:OSGI原理与最佳实践内容简介:国内第一本OSGi图书OSGi国内推广者林昊多年经验的结晶涵盖OSGi从入门到深入的知识体系引领OSGi国内研究和普及本书基于作者多年使用OSGi的经验而编写,涵盖了OSGi从入门到深入...