protected void activate(ComponentContext componentContext) throws Exception and protected void deactivate(ComponentContext ctx) | Community
Skip to main content
New Participant
October 16, 2015
Solved

protected void activate(ComponentContext componentContext) throws Exception and protected void deactivate(ComponentContext ctx)

  • October 16, 2015
  • 2 replies
  • 2591 views

Hi All,

I have seen this being used in java class written to use services and bundles.

Can you please let me know 

- Are these two methods mandatory?

- What exactly does ComponentContext do?

Any thoughts on this will be helpful.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

Here is the discussion from the Sling docs:

It may well be that the component needs to be notified, when it is activated and deactivated. For this, the component may implement an activate method and a deactivate method. Both methods must be public or protected and take a single argument, the org.osgi.service.ComponentContext. It is recommended for this method to the protected as it is only used by the Service Component Runtime and should of course not be part of the public API of the component.

Read the following Sling topic for more information about this:

http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html

When you create an Adobe Archetype project using Maven - you get this class that contains these methods:

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;

import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Just a simple DS Component
 */
@Component(metatype=true)
@Service
public class SimpleDSComponent implements Runnable {
    
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    private BundleContext bundleContext;
    
    public void run() {
        logger.info("Running...");
    }
    
    protected void activate(ComponentContext ctx) {
        this.bundleContext = ctx.getBundleContext();
    }
    
    protected void deactivate(ComponentContext ctx) {
        this.bundleContext = null;
    }

}

 

See: https://helpx.adobe.com/experience-manager/using/first-osgi.html

2 replies

smacdonald2008
smacdonald2008Accepted solution
New Participant
October 16, 2015

Here is the discussion from the Sling docs:

It may well be that the component needs to be notified, when it is activated and deactivated. For this, the component may implement an activate method and a deactivate method. Both methods must be public or protected and take a single argument, the org.osgi.service.ComponentContext. It is recommended for this method to the protected as it is only used by the Service Component Runtime and should of course not be part of the public API of the component.

Read the following Sling topic for more information about this:

http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html

When you create an Adobe Archetype project using Maven - you get this class that contains these methods:

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;

import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Just a simple DS Component
 */
@Component(metatype=true)
@Service
public class SimpleDSComponent implements Runnable {
    
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    private BundleContext bundleContext;
    
    public void run() {
        logger.info("Running...");
    }
    
    protected void activate(ComponentContext ctx) {
        this.bundleContext = ctx.getBundleContext();
    }
    
    protected void deactivate(ComponentContext ctx) {
        this.bundleContext = null;
    }

}

 

See: https://helpx.adobe.com/experience-manager/using/first-osgi.html

joerghoh
Employee
October 16, 2015

Hi,

you can found a number of good information about OSGI and the SCR (service component runtime) on the Apache Felix website ([1]). To answer your questions:

(1) These methods are optional

(2) Please check the OSGI API documentation at [2].

kind regards,
Jörg

 

[1] http://felix.apache.org/documentation/subprojects/apache-felix-service-component-runtime.html
[2] https://osgi.org/javadoc/r4v41/org/osgi/service/component/ComponentContext.html