Mayank Pratap
Mayank Pratap

Reputation: 185

How to configure declarative service in CQ5

How to configure declarative service through OSGI console in CQ5. I was able build sample service, bundled code i got jar and installed through bundle from OSGI console

Upvotes: 3

Views: 2423

Answers (1)

diffa
diffa

Reputation: 2996

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

Upvotes: 4

Related Questions