OSGI Services and Components

14/08/2020 / BY / IN OSGI Components and Services / 1 Comments

Welcome to part 5, OSGI Services and Components.

After reading this article, you should be able to

  • Describe what OSGI services and components are
  • View Components and services in the web console
  • Create OSGI components.

Services: OSGI Third Layer

Let’s get familiar with third architectural layer of OSGI, In the previous part, we just instantiated and invoked classes that live in bundles. The third architectural layer standardizes the way, the classes are invoked by introducing a concept of a service.

Services: PUBLISH, FIND, BIND

Services is a framework for interaction among bundles. The idea of the framework is expressed in 3 words. Publish, Find, Bind.

A service is a Java class, usually it implements an interface. A service is registered in the service registry, to register a service one needs to provide the service object itself or a service factory object, the service interface name and optional properties that can be used to select the best implementation, if there is more than one –  that’s what word Publish stands for.

If a class in our bundle need some feature provided in another bundle, it finds reference to service in the service registry. If there are several implementations of one interface available we can specify which one of them we need using properties, from the reference we can get access to the actual service and use it, that’s what describe by Bind word.

As for the service life cycle it is determined by its bundle, when the bundle starts the service is registered, when the bundle stopped the service is de-registered. Since bundles can be started an stopped at any time the services can come n go at any time too.

Declarative Services: DI for OSGI

The core OSGI service API is too low level. Every action requires writing Java code. AEM uses Enterprise OSGI features built on top of OSGI API, in particular there are 2 features that add two more layers or worth as service API. Those are Declarative Services and Annotations.

Declarative Services remove the need to write Java code, to manage the service lifecycle. Everything that is needed to Publish, Find and Bind services is specified in the XML descriptor. The binding is done using dependency injection and auto wiring, however XML descriptor is not an easy thing for developers to maintain. The using all solution for that, in the Java world is using Annotations and that is how it is done in Felix and in AEM.

Older versions of Felix offer a special annotation that allow the developer to declare service then maven-scr-plugin generates service descriptors basing on it and some other meta information, you may still encounter those annotations in projects that has been work on for long time in addition to declare a service the developer also had to declare another entity called component that is why the approach were simplified in the newer AEM versions. So, in AEM version 6.2 or newer OSGI release 6 annotations are used, with OSGI release 6 annotations one does declare standalone services but declare them as part of components that takes us one layer up to the enterprise OSGI.

OSGI Components

In Adobe AEM services are provided using components, you cannot create a service without a component in AEM. OSGI components are Java objects managed by the OSGI environment, by its part called Service Component Runtime or SCR.

  • Components are described in the XML descriptors of their bundles.
  • Each component provides one or more or zero services
  • A component can be configured using properties- will review that in next part.
  • In the Java code one uses annotations to declare and describe components. So in AEM you deal with OSGI components that is unit of configuration, components provide services.

Annotated OSGI Component Class

@Component(
        service = StringGenerator.class,  
        immediate = true)
public class StringGeneratorImpl implements StringGenerator {
    @Reference
    private SlingRepository slingRepository;
    . . .
}


Service Component Runtime (SCR)

When a component is deployed as part of a bundle, it is handled by the part of the OSGI environment called Service Component Runtime or SCR, it performs the following 5 things.

  1. It instantiates the component
  2. Resolves their references-> in those component
  3. Activates the components that is calls their activation method
  4. Registers the services exposed by the components in the service registry
  5. Resolves the references TO the services exposed by the components.

Let me remind that AEM uses Apache Felix Service Component Runtime.

Summary

• Bundles interact with each other using services
• Service API is low-level and requires writing Java code
• Declarative services feature allows configuring services using XML descriptors
• In AEM, one declares services as part of OSGI components
• With Maven Bundle Plugin, one can use annotations to generate the descriptors
• AEM 6.2 and later use OSGi R6 annotations. Older versions use Felix SCR  annotations, which are now deprecated
• Components exist within bundles. Components have states.
• One can monitor and configure components in the Web Console

Hope you enjoyed this article, I would like to suggest, Kindly watch video below for practical session, Thank you for your attention! In the next part we will learn how to configure component in AEM. Click here to visit page!

Leave a Reply