Tuesday, 8 August 2023

OSGi Service

What is OSGi Service?

  • OSGI services are the reusable services just like method in java.
  • Suppose, we have functionality like email triggering so, we will have requirement to reuse this email triggering functionality in whole website.
  • So, instead of writing this code again & again we will write this in one time in OSGI Service.
  • It has additional annotation which let AEM manipulate this OSGI Service as per the Bundle & Life cycle i.e:- Start, Stop etc.
What is the mandatory component that designates a class as an OSGi service?

  • We should always have an interface when you are writing a service and implement that interface.
  • The mandatory annotation is @Component, which designates a class as a service.
  • Inside that you have [Service = whenever your interface is and this is mandatory part].
  • Some optional methods are executed when your service is activated and your bundle is deployed. As soon as your service is activated, the method that is returned in the 'activated' state is executed.
Optional Method in OSGI Service and Life cycle of OSGi service




@Activate Annotation

  • In any method, if you add an 'activate' annotation, it will execute at the time of service activation.
@Deactivate annotation
  • In OSGi services, the @Deactivate annotation is used to mark a method as the deactivation method for the service. 
  • This method is called when the service is being deactivated by the OSGi container.
@Modified annotation
  • The @Modified annotation is used in Apache Felix. 
  • other OSGi frameworks to mark a method that should be called whenever the properties of the component change.
How to write OSGi service?

Step1:-

  • Write an interface named 'ServiceWithModelOsgi'.


Step2:-
  • Write a class named 'ServiceWithModelOsgiImpl'.

Note:-
  • "In any backend module, we can choose to call an OSGi service. It's important to note that without a backend module, we cannot call an OSGi service. While we typically use the '@Reference' annotation to call an OSGi service, when using a Sling model, we use the '@OSGiService' annotation."

Step3:-

  • Write a interface named 'ServiceWithModel'.


Step4:-
  • Write a class named 'ServiceWithModelImpl'.



Thanks
Matri

No comments:

Post a Comment

Servlet in AEM/Sling Servlet

  What is Servlet? A servlet is a Java class that runs on the server side where the application resides. It acts as a middle layer between t...