您的位置:首页 > 其它

OSGI CM之ManagedService的使用

2015-12-01 09:57 295 查看
OSGI里面用于操作配置文件(cfg)的接口有2个

org.osgi.service.cm.ManagedService 用于操作单个配置文件

org.osgi.service.cm.ManagedServiceFactory 用于操作一组相关的配置文件 用法见 OSGI CM之ManagedServiceFactory的使用

org.osgi.service.cm.ManagedService可以实现,文件改动就会收到通知的效果,下面是一个简单的例子

package com.pp;

import java.util.Dictionary;

import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;

public class ConfigManagedExample implements ManagedService
{
	public void updated(Dictionary<String, ?> properties) throws ConfigurationException
	{
		System.out.println("--------文件被触发---------");
		System.out.println(properties);
	}
}


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" 
	xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> 	
	<bean id="configManagedExample" class="com.pp.ConfigManagedExample" />
	
	<service ref="configManagedExample" interface="org.osgi.service.cm.ManagedService">
		<service-properties>
			<entry key="service.pid" value="conf.example" />
		</service-properties>
	</service>
    
</blueprint>


然后,打包成bundle,扔到karaf、servicemix的deploy目录中去

注意上面blueprint.xml 的conf.example

下一步,是在deploy或者etc目录下,新建一个conf.example.cfg文件,里面按照properties标准格式来写

以后,这要这个文件有改动,就会触发ConfigManagedExample的updated方法执行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: