【问题标题】:Karaf add additional property to existing config fileKaraf 向现有配置文件添加附加属性
【发布时间】:2015-10-09 16:30:04
【问题描述】:

我有一个捆绑包,它使用一个配置文件 org.jemz.karaf.tutorial.hello.service.config.cfg 和一个属性:

org.jemz.karaf.tutorial.hello.service.msg="I am a HelloServiceConfig!!"

我使用 ConfigAdmin 的蓝图是这样的:

<cm:property-placeholder persistent-id="org.jemz.karaf.tutorial.hello.service.config" update-strategy="reload" >
    <cm:default-properties>
        <cm:property name="org.jemz.karaf.tutorial.hello.service.msg" value="Hello World!"/>
</cm:default-properties>
</cm:property-placeholder>



<bean   id="hello-service-config"
        class="org.jemz.karaf.tutorial.hello.service.config.internal.HelloServiceConfig"
        init-method="startup"
        destroy-method="shutdown">

    <property name="helloServiceConfiguration">
        <props>
              <prop key="org.jemz.karaf.tutorial.hello.service.msg" value="${org.jemz.karaf.tutorial.hello.service.msg}"/>
        </props>
    </property>
</bean>

<service ref="hello-service-config" interface="org.jemz.karaf.tutorial.hello.service.IHelloService" />

只要我可以更改属性的值并且捆绑包会自动更新该属性,它就可以正常工作。

我想知道是否有任何方法可以在我的配置文件中添加新属性而无需更改蓝图(这涉及再次编译/打包)。当然我的包应该准备好处理新属性。

不确定这在 OSGi 中是否有意义。谁能告诉我如何将新属性动态添加到现有配置文件并使其在 ConfigAdmin 中可用?

【问题讨论】:

    标签: osgi apache-karaf karaf blueprint-osgi


    【解决方案1】:

    您可以在 karaf shell 中添加属性:

    1/config:edit org.jemz.karaf.tutorial.hello.service.config

    2/config:propset &lt;key&gt; &lt;value&gt;

    3/ 最后config:update 将更改保存到文件。

    如果您想以编程方式进行,请查看 karaf 源代码以查看实现

    【讨论】:

    • 感谢您的回答。您的解决方案有效,属性存储在属性占位符中,我可以列出它们。问题是我的捆绑包无法获得这些新属性。我认为这与 bean 定义有关,我只设置了一个要注入的属性“org.jemz,karaf.tutorial.service.msg”,因此我无法在setHelloServiceConfiguration 方法中获得更多属性。我想出了另一种从我的配置文件中获取新属性的方法,我将在我的答案中发布它。无论如何,我会给你一个赞成票
    • 嗨 Jorge,如果您使用的是 SpringDM,我可以为您提供配置示例,以便能够为一个捆绑包管理多个配置文件
    【解决方案2】:

    @yodamad 向我展示了我的 ConfigurationAdmin 服务中的属性正在更新,但不幸的是,我的 bundel 没有收到新属性,因为在 bean 定义中我只是使用了一个固定属性。

    最后,为了从配置文件中获取新属性,我将蓝图定义更改如下:

    <cm:property-placeholder persistent-id="org.jemz.karaf.tutorial.hello.service.config" update-strategy="reload" >
    </cm:property-placeholder>
    
    <bean   id="hello-service-config"
            class="org.jemz.karaf.tutorial.hello.service.config.internal.HelloServiceConfig"
            init-method="startup"
            destroy-method="shutdown">
    </bean>
    
    <service ref="hello-service-config" interface="org.jemz.karaf.tutorial.hello.service.IHelloService" />
    
    
    <reference id="hello-service-config-admin" interface="org.osgi.service.cm.ConfigurationAdmin"
               availability="optional">
        <reference-listener bind-method="setConfigAdmin"
                            unbind-method="unsetConfigAdmin">
            <ref component-id="hello-service-config"/>
        </reference-listener>
    </reference>
    

    我有一个 ConfigurationAdmin 服务的引用,所以现在我可以检查我的包的所有属性:

    public void setConfigAdmin(ConfigurationAdmin cfgAdmin) {
        this.cfgAdmin = cfgAdmin;
        try {
            Configuration cfg =   cfgAdmin.getConfiguration("org.jemz.karaf.tutorial.hello.service.config");
            Dictionary<String, Object> properties = cfg.getProperties();
            Enumeration<String> en = properties.keys();
            while(en.hasMoreElements()) {
                String key = en.nextElement();
    
                System.out.println("KEY: " + key + " VAL: " + properties.get(key));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    因为我的“更新策略”是reload,所以只要注册了新的 ConfigurationAdmin 服务,我就可以得到更新。

    欢迎对我的方法发表任何评论!

    【讨论】:

    • 你也可以使用 implements ConfigurationListener, BundleContextAware 接口的类。然后您将收到有关配置更改的所有事件,因此您可以在应用程序中处理您想要的任何内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 2019-01-25
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多