OSGI CM之ManagedService的使用

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方法执行


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值