May 10, 2020
Estimated Post Reading Time ~

OSGi Components and Services

ComponentServices
The component is started immediatelyComponents providing a service are activated only when they’re actually referenced by some other component/service.
Every component is not a service.Every service is a component.
 Its a software(java class) managed by a component container. The only container can create and manage the component(class) object. The object cannot be instantiated by us. The container can also pass some inputs to the component.It's a component which also provides some service, implements one or more interfaces and service used by other components or services.
 Declarative Services (DS) is one way of developing components and services for OSGiBlueprint, Apache Felix iPojo, Apache Felix Dependency Manager etc. are other approaches
@Component
public class MyComponent {@Activate
protected void activate() {
//Gets called when a component is instantiated by the conatiner.
// do something
}@Deactivate
protected void deactivate() {
//when gets deactivated by the container.
// do something
}
}
These methods can be of any name,have to be protected with no return value.
@Component
@Service(value=ServiceInterface.class)
public class MyService implements ServiceInterface{@Activate
protected void activate(final Map<String, Object> config) {}
}
It can implement multiple interfaces.
@Component
@Service(value={ServiceInterface1.class,ServiceInterface2.class})
public class MyService implements ServiceInterface1,ServiceInterface2
 Component Container manages the lifecycle of component.Services are initialized when used, when unused for some time, it gets destroyed.
To create a service-Define the service API, implement this API.
A service implementation should never be public but in a private package
 Usage example: Sling SchedulerUsage:
A component can use other services by using the @Reference SCR annotation.
Before the component gets activated, the services used get initialized. If service is unavailable component will be deactivated.
The component is registered by the Component class name itself.Services are registered by their interface they implement
The component can be made configurable:
@Activate
protected void activate(final Map<String, Object> configs) {
// use the configuration
//
}
Services can be configurable:
@Component(metatype=true)

@component(immediate=true)
With immediate set to true, the component is activated as soon as possible and kept as long as possible.This increases things like startup time, memory consumption etc.
 To create a service which should be active for every request, component should be set to immediate=true
If an interface is registered by multiple components, then one with max service ranking will be used.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.