当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How to start immediate = false component?

问题描述:

I have following component:

@Component(

label = "Service name",

description = "Service description",

metatype = true,

immediate = false)

@Properties({

@Property(

label = "My Label"

name = "property-name",

value = "my value",

propertyPrivate = true

)

})

@Service

public class SampleServiceImpl implements SampleService {

private final Logger log =

LoggerFactory.getLogger(this.getClass());

/**

* OSGi Properties *

*/

private static final String DEFAULT_SAMPLE = "hello!";

private String mySample = DEFAULT_SAMPLE;

@Property(label = "Prop name",

description = "Prop description",

value = DEFAULT_SAMPLE)

public static final String PROP_SAMPLE = "sample";

...

@Activate

protected void activate(final Map<String, String> config) {

mySample = PropertiesUtil.toString(config.get(PROP_SAMPLE), DEFAULT_SAMPLE);

}

@Deactivate

protected void deactivate(final Map<String, String> config) { }

}

It registered after cq started. It is expected behaviour.

How to start it ? I cannot find this interface.

网友答案:

Yes, this is expected behaviour for non-immediate components. From a blog post by Felix Meschberger on dev.day.com,

The OSGi Declarative services specification defines three types of components:

  • Immediate Components are immediately created when the providing bundle is started and may or may not provide services

  • Delayed Components provide services but are only created when used by a service consumer.

  • Factory Components are created on demand by calling the ComponentFactory.newInstance(Dictionary) method of the Component Factory service registered for the component.

So, if you set immediate = false, you will have a delayed component, which is activated on-demand by the run-time — you do not need to explicitly start the component yourself.

The main thing that you should be checking for is that the component is in a state of "Enabled" or "Registered" (as you mentioned that it is) rather than "Unsatisfied" (where it can't find references to dependencies it needs).

From the Felix documentation…

STATE_REGISTERED

The Component has successfully been activated but is a Delayed or Service Factory Component pending instantiation on first use (value is 32).

网友答案:
@Component( enabled= false,...)

and go to system/console/components

分享给朋友:
您可能感兴趣的文章:
随机阅读: