如何在 CQ5 中通过 OSGI 控制台配置声明式服务。我能够构建示例服务,捆绑代码我得到 jar 并通过捆绑从 OSGI 控制台安装
1 回答
The first step is define your service has having configuration parameters. You might have something like this:
package com.sample.osgi;
import java.util.Map;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Property;
@Component(label = "Service Label", description = "Service Description", metatype = true, immediate = true)
public class ConfigurableService {
@Property(value="default value", label = "Sample Parameter", description = "Example of a component parameter")
private static final String SAMPLE_PARAM_NAME = "param.one";
@Activate
protected void activate(final Map<String, Object> props) {
this.update(props);
}
@Modified
protected void update(final Map<String, Object> props) {
System.out.println(props.get(SAMPLE_PARAM_NAME));
}
}
Once you have your service, you should use maven to generate the scr descriptors, create your bundle and deploy it to your local server. This is described on this page.
Once you've deployed you should be able to see your service in the felix console on your server. For example:
http://localhost:4502/system/console/configMgr/com.sample.osgi.ConfigurableService
As we added an update method with the @Modified annotation, your component will receive updates to the configured values as they are made with calls to that method.
You can find more information on the SCR annotations on the felix site