コミュニティアチーブメントバーを展開する。

Submissions are now open for the 2026 Adobe Experience Maker Awards.

Mark Solution

この会話は、活動がないためロックされています。新しい投稿を作成してください。

解決済み

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

Avatar

Level 9

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.

1 受け入れられたソリューション

Avatar

正解者
Level 10

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 返信

Avatar

正解者
Level 10

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

Avatar

Employee Advisor

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