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.
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
.