【问题标题】:Using Felix Dependency Manager to create Configuration Dependeny使用 Felix 依赖管理器创建配置依赖
【发布时间】: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


    【解决方案1】:

    我确实将您提供的三个类复制到了我的 IDE 中,并用它们制作了两个包。我还包括了一个实现 ConfigurationAdmin 和 DependencyManager 的包。当我开始一切时,我立即得到消息:

    Configuration in Bundle HttpServer was updated
    

    所以我不确定这里出了什么问题,但您的代码似乎是绝对正确的!让我向您展示我运行的完整输出,包括 GoGo shell 和 DM shell 命令:

    ConfigAdminTester init finished
    config updated
    Configuration in Bundle HttpServer was updated
    Host, Port, Id, Password: test, test, 1, test!
    ____________________________
    Welcome to Apache Felix Gogo
    
    g! lb
    START LEVEL 1
       ID|State      |Level|Name
        0|Active     |    0|System Bundle (4.2.1)
        1|Active     |    1|Apache Felix Configuration Admin Service (1.8.0)
        2|Active     |    1|Apache Felix Dependency Manager (3.1.0)
        3|Active     |    1|Apache Felix Dependency Manager Shell (3.0.1)
        4|Active     |    1|Apache Felix Gogo Command (0.12.0)
        5|Active     |    1|Apache Felix Gogo Runtime (0.10.0)
        6|Active     |    1|Apache Felix Gogo Shell (0.10.0)
        7|Active     |    1|osgi.cmpn (4.3.1.201210102024)
        8|Active     |    1|xxx.stackoverflow.b1 (0.0.0)
        9|Active     |    1|xxx.stackoverflow.b2 (0.0.0)
    g! dm
    [8] xxx.stackoverflow.b1
      class b1.ConfigTestImpl registered
        test.configadmintest.testconfig configuration required available
    
    g! 
    

    为了确定,我确实将代码放在了两个不同的包中。您提供的示例不显示包或导入,但您不能在 OSGi 中使用默认(空)包。另一个错误可能是一些错误的导入。无论如何,如果您仍然卡住,请告诉我,我很乐意提供完整的源代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 2021-07-05
      • 2012-06-30
      • 1970-01-01
      相关资源
      最近更新 更多