AEM OSGI Services

OSGI service stands for Open Service Gateway initiative. It is a java class that can register as an OSGI service.

We can create OSGI service with the help of @Component annotation from org.osgi.service.component.annotations package and register it as an OSGI service.

Deployed component on AEM instance gets handle by SCR (Sling Component Runtime). Bundles communicate with each other with the help of services.

Please follow below steps to create an OSGI service with the help of @Component annotation. Services can be consume using @Reference annotation in Servlet and other services. User @OSGiService annotation to inject service in sling model.

  1. As part of current implementation, we will be making changes to below highlighted files and folders.

2. Create below interface named as PracticeService.java and a public method getName()

3. Create below class PracticeServiceImp.java which implements PracticeService.java interface and implement getName() method at the same time as shown below.

Service property is require to register component as service.

4. As discussed above, we are going to consume service inside servlet with the help of @Reference component.

5. Let’s deploy code using below command as we made only Java related changes.
mvn clean install -PautoInstallBundle

6. Open System console components to verify OSGI service installation on AEM instance showing status as active.

7. Hit servlet URL as http://localhost:4502/content/practice/us/en/_jcr_content.txt in browser to check output:

Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.

0