OSGi 系列(十三)之 Configuration Admin Service
OSGi 系列(十三)之 Configuration Admin Service
OSGi 的 CM 就是 Configuration Admin Service,是用于管理 Bundle 属性、并在属性发生变更时通知相应的 Service,这是保持 OSGi 动态性的很关键的一个服务。
1. ConfigurationAdmin
用于管理配置数据的服务,主要有两个作用:
- 获取配置数据
- 更新配置数据
public class ConfigurationAdminDemo {
private ConfigurationAdmin configurationAdmin;
public void init() {
try {
Configuration conf = configurationAdmin.getConfiguration("jdbc.mysql");
Dictionary<String, Object> dict = conf.getProperties();
System.out.println(dict);
Dictionary<String, String> properties = new Hashtable<>();
properties.put("username", "root");
properties.put("password", "123456");
conf.update(properties);
} catch (IOException e) {
e.printStackTrace();
}
}
public ConfigurationAdmin getConfigurationAdmin() {
return configurationAdmin;
}
public void setConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
this.configurationAdmin = configurationAdmin;
}
}
blueprint.xml 配制文件:
<reference id="configurationAdmin" interface="org.osgi.service.cm.ConfigurationAdmin" />
<bean class="com.edu.osgi.cm.ConfigurationAdminDemo" init-method="init">
<property name="configurationAdmin" ref="configurationAdmin" />
</bean>
结果如下:

2. ManagedService
用来关联和操作单个配置文件的服务当相关联的配置文件发生变化的时候,会通知到相应的服务。
public class DBConfigManagedService implements ManagedService {
public void updated(Dictionary<String, ?> properties) throws ConfigurationException {
System.out.println("======updated======");
System.out.println(properties);
}
}
blueprint.xml 配制文件:
<service interface="org.osgi.service.cm.ManagedService">
<service-properties>
<entry key="service.pid" value="jdbc.mysql" />
</service-properties>
<bean class="com.edu.osgi.cm.DBConfigManagedService" />
</service>
手动修改 jdbc.mysql.cfg 文件后,结果如下:

3. blueprint 中 cm 的使用
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd">
<cm:property-placeholder persistent-id="jdbc.mysql.conf" update-strategy="reload"/>
<bean class="com.edu.osgi.cm.MysqlConfig" init-method="display">
<property name="className" value="${className}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
</blueprint>
4. karaf 中 cm 命令
config:list "(service.pid=jdbc.mysql)"列出 jdbc.mysql 配制文件
config:edit编辑属性config:edit jdbc.mysql
config:property-set name join
config:update
5. ManagedServiceFactory
用来关联和操作多个配置文件的服务,当多个相关联的配置文件发生变化的时候,都会通知到相应的服务。
(1) 环境准备
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
(2) 实现 ManagedServiceFactory 接口
public class DataSourceServiceFactory implements ManagedServiceFactory {
private BundleContext bundleContext;
private Map<String, ServiceRegistration<?>> serviceMap = new ConcurrentHashMap<>();
public String getName() {
return "DataSourceServiceFactory";
}
// 更新配置文件
public void updated(String pid, Dictionary<String, ?> properties) throws ConfigurationException {
System.out.println("============updated============");
System.out.println(pid);
System.out.println(properties);
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName(properties.get("driverClassName").toString());
bds.setUrl(properties.get("url").toString());
bds.setUsername(properties.get("username").toString());
bds.setPassword(properties.get("password").toString());
Dictionary<String, String> props = new Hashtable<>();
props.put("instance", properties.get("instance").toString());
ServiceRegistration<?> sr = bundleContext.registerService(DataSource.class.getName(), bds, props);
System.out.println("------------");
serviceMap.put(pid, sr);
}
// 删除配置文件
public void deleted(String pid) {
System.out.println("============deleted============");
System.out.println(pid);
ServiceRegistration<?> sr = serviceMap.get(pid);
sr.unregister();
serviceMap.remove(pid);
}
public void setBundleContext(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}
}
(3) blueprint.xml
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd">
<service interface="org.osgi.service.cm.ManagedServiceFactory">
<service-properties>
<entry key="service.pid" value="jdbc.datasource"/>
</service-properties>
<bean class="com.edu.osgi.cm.DataSourceServiceFactory">
<property name="bundleContext" ref="blueprintBundleContext" />
</bean>
</service>
<!--<reference interface="javax.sql.DataSource" filter="(instance=test)"/>-->
</blueprint>
(4) 测试
# 安装事务
feature:install transaction
install -s mvn:org.apache.commons/commons-pool2/2.4.2
install -s mvn:org.apache.commons/commons-dbcp2/2.1.1
install -s mvn:mysql/mysql-connector-java/5.1.18
在 deploy 目录下新建二个文件 jdbc.datasource-dev.cfg、jdbc.datasource-test.cfg(注意:文件要以 jdbc.datasource 开头,"-" 分隔)
jdbc.datasource-dev.cfg
instance=dev
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///dev
username=root
password=root
jdbc.datasource-test.cfg
instance=test
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///test
username=root
password=root

OSGi 系列(十三)之 Configuration Admin Service的更多相关文章
- OSGi 系列(十四)之 Event Admin Service
OSGi 系列(十四)之 Event Admin Service OSGi 的 Event Admin 服务规范提供了开发者基于发布/订阅模型,通过事件机制实现 Bundle 间协作的标准通讯方式. ...
- OSGi 系列(十二)之 Http Service
OSGi 系列(十二)之 Http Service 1. 原始的 HttpService (1) 新建 web-osgi 工程,目录结构如下: (2) HomeServlet package com. ...
- OSGi 系列(十六)之 JDBC Service
OSGi 系列(十六)之 JDBC Service compendium 规范提供了 org.osgi.service.jdbc.DataSourceFactory 服务 1. 快速入门 1.1 环境 ...
- OSGi 系列(六)之服务的使用
OSGi 系列(六)之服务的使用 1. 为什么使用服务 降低服务提供者和服务使用者直接的耦合,这样更容易重用组件 隐藏了服务的实现细节 支持多个服务的实现.这样你可以互换这实现 2. 服务的使用 2. ...
- OSGi 系列(一)之什么是 OSGi :Java 语言的动态模块系统
OSGi 系列(一)之什么是 OSGi :Java 语言的动态模块系统 OSGi 的核心:模块化.动态.基于 OSGi 就可以模块化的开发 java 应用,模块化的部署 java 应用,还可以动态管理 ...
- OSGi 系列(十八)之 基于注解的 Blueprint
OSGi 系列(十八)之 基于注解的 Blueprint 1. 注解实现 blueprint 第一步:bundle 添加 Bundle-Blueprint-Annotation <plugin& ...
- OSGi 系列(三)之 bundle 详解
OSGi 系列(三)之 bundle 详解 1. 什么是 bundle bundle 是以 jar 包形式存在的一个模块化物理单元,里面包含了代码,资源文件和元数据(metadata),并且 jar ...
- OSGi 系列(十)之 Blueprint
OSGi 系列(十)之 Blueprint blueprint 是 OSGi 的一个规范,类似于 spring 的 IOC,用来处理 OSGi 的动态特性,可以大大简化服务的使用. blueprint ...
- OSGi 系列(七)之服务的监听、跟踪、声明等
OSGi 系列(七)之服务的监听.跟踪.声明等 1. OSGi 服务的事件监听 和 bundle 的事件监听类似,服务的事件监听是在服务注册.注销,属性被修改的时候,OSGi 框架会发出各种不同的事件 ...
随机推荐
- PyQt5系列教程(八)定时器QTimer的使用
软硬件环境 OS X EI Capitan Python 3.5.1 PyQt 5.5.1 前言 如果需要在程序中周期性地进行某项操作,比如检测某种设备的状态,就会用到定时器.本文就来看看PyQT5中 ...
- 用strings命令查看kafka-log内容 过滤二进制编码
kafka的log内容格式还不没怎么了解,想快速浏览消息内容的话,除了使用它自带的kafka-console-consumer.sh脚本,还可以直接去看log文件本身,不过内容里有部分二进制字符,通过 ...
- 23.OGNL与ValueStack(VS)-调用普通类的构造方法
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 建立一个新的类:Student,在此省略代码. 然后在loginSuc.js ...
- 线程安全计算 AtomicLong
一般如果我们自己写一个计数器方法,需要考虑线程安全问题,尤其高并发访问的时候. AtomicLong 已处理并发问题,直接使用.java.util.concurrent.atomic包提供多种线程安全 ...
- Spring cron 定时调度配置
IDEA 或者 STS http://spring.io/guides/gs/scheduling-tasks/ spring mvc : 结构: Seconds Minutes Hours Day ...
- Spring mvc RequestContextHolder分析
转载: http://blog.csdn.net/zzy7075/article/details/53559902
- python 2 类与对象
1.类与对象的概念 类即类别.种类,是面向对象设计最重要的概念,从一小节我们得知对象是特征与技能的结合体,而类则是一系列对象相似的特征与技能的结合体. 那么问题来了,先有的一个个具体存在的对象(比如一 ...
- JAVA中request.getParameterMap()用法笔记
一. 根据Java规范:request.getParameterMap()返回的是一个Map类型的值,该返回值记录着前端(如jsp页面)所提交请求中的请求参数和请求参数值的映射关系.这个返回值有个特别 ...
- adb连接过程中常见问题解决方法
在测试过程中经常会遇到需要使用adb连接服务器的问题,但是有时候经常会遇到连不上的情况,总结两种解决方式 1)error: unknown host service 此问题是由于端口号已经被占用了,可 ...
- 发布MVC项目到服务器上时候遇到的 模块 DirectoryListingModule 通知 ExecuteRequestHandler 处理程序 StaticFile 错误代码 0x00000000
应用程序“HMW121197”中的服务器错误错误摘要HTTP 错误 403.14 - ForbiddenWeb 服务器被配置为不列出此目录的内容. 详细错误信息模块 DirectoryListingM ...