Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is 'enable=true' attribute of @Component in OSGi Component?

I had came through an attribute of @Component in OSGi which I dont understand through docs and didn't get any detailed information i.e enabled.

What I got from Felix documentation about scr annotation is:

enabled

enabled 
Default: true 
SCR Descriptor: component.enabled 
Metatype Descriptor: -- 

Whether the component is enabled when the bundle starts

immediate

immediate 
Default: -- 
SCR Descriptor: component.immediate 
Metatype Descriptor: -- 

Whether the component is immediately activated

Though I know "Activating a component", but I don't understand what the term "Enabling a component" means?

Any answers with the help of use-cases or example, when to use what, are more appreciated.

Thank you in Advance.

like image 337
Manisha Avatar asked Mar 06 '23 09:03

Manisha


2 Answers

Components are enabled=true by default, which means they will be available for activation as soon as their dependencies -- e.g. required configuration and/or mandatory service references -- have been satisfied.

A component that is enabled=false will NOT be available for activation even if all its dependencies are satisfied. It is completely disabled and will not start.

So... what use is this?? Well, a disabled component can be programmatically enabled by another component in the same bundle.

The primary use-case for this is shared initialization. Suppose you have a bundle that contains several components that all need to wait for some initialization steps to occur, like setting up a bunch of files. You can make all of the components except one enabled=false. The single enabled component does the initialization in its activation method and then calls ComponentContext.enableComponent(null) to enable all of the other components in the bundle.

immediate is a completely separate and orthogonal lifecycle concept. A component that provides a service is, by default, "delayed", meaning the component is only loaded and activated when some other bundle actually uses the service. This is a really useful lazy-loading optimization. However sometimes you want your component to start as soon as possible even if nobody is using its service. In those cases you set immediate=true.

like image 104
Neil Bartlett Avatar answered Apr 26 '23 02:04

Neil Bartlett


Enabled is one of the states in a components life cycle. This is the initial and default state of the component. Where as immediate=true is an action you perform on an enabled component to change it to Active state immediately rather than delayed activation(immediate = false).

Now between the enabled state and the active state, the service component goes through a few more states where it satisfies all the dependencies, binds all the references(@References), creates the component instance, component context objects, etc - all of which takes time and memory.

With regard to use cases for using enabled and immediate, you can use these descriptors to optimize the initial memory footprint and load time of the component. There might be cases where you would want to programmatically enable or activate a disabled component only under certain conditions. In such a scenario, the component is disabled initially and it does not even attempt to move to the next state, thereby optimizing the time and memory footprint of the bundle(the bundle this component is part of).

Have a read through the life cycle section of this blog - gives you a clear understanding of OSGi DS.

like image 22
SubSul Avatar answered Apr 26 '23 02:04

SubSul