【发布时间】:2014-02-04 13:43:37
【问题描述】:
我正在尝试让 FelixDependencyManager 正常工作。 我正在使用 Karaf 3,并且正在使用 2 个捆绑包进行测试,一个用于提供配置信息,一个用于实现 ManagedService-Interface 并取决于此信息。 所以一切都归结为 3 个类:
依赖包有 2 个类: 第一个是扩展使用 Felix DependencyManager 所需的 DependencyActivatorBase 类并实现 init 方法,在该方法中创建和配置组件:
public class ConfigAdminTester extends DependencyActivatorBase{
@Override
public void init(BundleContext context, DependencyManager manager)
throws Exception {
manager.add(createComponent()
.setImplementation(ConfigTestImpl.class)
.add(createConfigurationDependency()
.setPid("test.configadmintest.testconfig"))
.setCallbacks(null, "start", null, null)
);
System.out.println("ConfigAdminTester init finished");
}
@Override
public void destroy(BundleContext context, DependencyManager manager)
throws Exception {
}
}
包中的第二个类应该由 ConfigAdmin-Service 更新。它实现了 ManagedService 并具有更新方法,在部署包和更新配置时应该调用它:
public class ConfigTestImpl implements ManagedService{
String host;
String port;
int id;
String password;
public void start(){
System.out.println("Host, Port, Id, Password: " + host + ", " + port +", " + id + ", " + password);
}
@Override
public void updated(@SuppressWarnings("rawtypes") Dictionary properties) throws ConfigurationException {
if (properties != null) {
host= ((String)properties.get("host"));
if (host == null) {
throw new ConfigurationException("host", "must be specified");
}
port=((String)properties.get("port"));
if (port == null) {
throw new ConfigurationException("port", "must be specified");
}
id=((Integer)properties.get("id"));
password=((String)properties.get("password"));
System.out.println("Configuration in Bundle HttpServer was updated");
}
else {
System.out.println("Properties are null");
}
}
}
第二个包只有一个类,它实现了 BundleActivator,获取对 ConfigAdmin-Service 的引用,并使用与其他包相同的 pid 创建一个新配置:
public class ConfigCreator implements BundleActivator{
public void start(BundleContext context) throws Exception {
@SuppressWarnings("rawtypes")
ServiceReference serviceRef = context.getServiceReference(ConfigurationAdmin.class.getName());
@SuppressWarnings("unchecked")
ConfigurationAdmin configAdmin = (ConfigurationAdmin) context.getService(serviceRef);
Configuration config = configAdmin.getConfiguration("test.configadmintest.testconfig", null);
Dictionary <String, Object> properties = new Hashtable <String, Object>();
properties.put("host", "test");
properties.put("port", "test");
properties.put("id", 1);
properties.put("password", "test!");
config.update(properties);
System.out.println("config updated");
}
现在的问题是更新的方法永远不会被调用。我得到了 ConfigAdminTest init 完成并且 Bundle 被激活的输出,但是 ConfigtestImpl 中的回调方法 start 永远不会被调用。如果我只是声明没有配置依赖项的组件,一切正常,回调方法将按原样执行。配置已发布,可用(我使用 config:list 命令检查)并且有效,但似乎不知何故配置不可用于依赖包。 任何人的想法? 非常感谢帮助。
【问题讨论】:
标签: osgi apache-felix