My Blog List

Tuesday, May 23, 2023

How to create and call the Multiple Implementation of OSGI Service in Sling Models

How we can create and call the multiple implementations of a OSGI Service using Sling Models?







In last article we have seen how can we create and call the multiple implementation of a OSGI Service by using the Servlet. In this article you will try to understand and I will try to explain how we can crate and call the multiple implementation of a OSGI Service while you are using the Sling Model. For you understanding I would like to tell there is a difference between while you want create and call the multiple implementation of a OSGI Service via Servlet and when you want to create and call the multiple implementation of a OSGI Service by using Sling Model. These both are very important questions number of times number of candidates were not able to answer these two questions.

Let us understand how to create and call the multiple implementations of a OSGI Service by using Sling Model.

  • We have to create the interface of a OSGI Service 
  • Created two implementation classes for this OSGI Service

  • Create a interface for the Sling Model 

  • Give the implementation for the Sling Model basically it is a Sling model from where we will call our OSGI Service 

  • Create a dummy component(dummy means we will have only a component and in that component's dialog we no need to keep any authoring field). And edit the component's html file and write the code to call the Sling Model's method and sling model's method will internally call the OSGI Service.
  • My component name is author and .html file will be author.html 

  • In author.html file we have write code to call  the sling model 

  • By using data-sly-use we can initiate our model and in my Sling Model method name is getResponse(). So we are calling our method like model.reponse()(reference variable for model interface ) .
  • Next step is I have enabled the author component from the template. And I have created a page from that template and I have drag and drop the author component in the page like 

  • After dragging and dropping the component we are getting the response as shown below 

  • First of all we have to understand which implementation class is returning the String "Implementation One is Calling". As we have two implementation of a OSGI Service  MyOSGIServiceOne and MyOSGIServiceSecond and "Implementation One is Calling" is returned by MyOSGIServiceOne.
  • I have called it number of times but it is calling only MyOSGIServiceOne but it is not calling MyOSGIServiceTwo. To understand this concept we have to understand the concept of Service ID.

What is Service ID and on which basis AEM Engine call the OSGI Service if we have two implementation?

The when we deploy any OSGI Service into the AEM via Maven command so AEM Engine associates a Service ID with each OSGI Service Implementation. If you want to see the service id of your OSGI Service than you have to open the system console using (http://localhost:4502/system/console/bundles) and search for your project's bundle and click on it and search for your service implementation from  name like 

MyOSGIServiceOne has 8435 Service ID and MyOSGIServiceSecond has 8436 Service ID and the OSGI Service have lesser service id(8435) and this is the reason when we are calling the OSGI Service from the Sling Model it is calling only MyOSGIServiceOne. Ultimately the OSGI Service will have lesser Service Id It will invoke on priority. And second questions that should came in our mind how AEM allocates the Service Id to the OSGI Services?

How AEM Engine allocates the Service ID to the OSGI Services?

There is different-different answer from the different-2 developers/leads/Architects for the same question. But as per my research and proof I would like to say that on the basis of name of the OSGI Services mean on alphabetically order AEM Engine assigns the service ID to the OSGI Service.

How you can estimate the Service id?

As you knows I have two implementation classes for the OSGI Service.
  • MyOSGIServiceOne
  • MyOSGIServiceSecond.
If you will sort these two words alphabetically you will be able to know the word MyOSGIServiceOne will come first and MyOSGIServiceTwo will come last. And as per the assumption if word is coming first the service id will be allocated first or if word is coming in last the service id will be allocated in last. click here to sort  
In this screenshot you are able to see the sort order of two words MyOSGIServiceOne and MyOSGIServiceSecond I have used online tool to check. And I got the result in Sorting Order like
  • MyOSGIServiceOne
  • MyOSGIServiceSecond
And in same way 
  • MyOSGIServiceOne comes first and Service Id is 8435 .
  • MyOSGIServiceSecond comes last and Service Id is 8436 .
Note:-The OSGI Service will have lesser service id will be invoked on priority.

Way 1-->

How can you call the method of a particular OSGI Service implementation class?

To call the method of particular OSGI Service class we have to use @ServiceRanking(RankingNuimber) annotation. We can define the ranking of a OSGI Service. In which OSGI Service the Ranking Number will be higher it will be called first as per the working of @ServiceRanking annotation. In this scenario we will call the OSGI Service with filter we will use the @OSGIService annotation.

Note:-@ServiceRanking annotation is the class level annotation. Means it will come on the top of class.


Before we make any changes the result we were getting is 

Our MyOSGIServiceOne is calling again because it has lowest Service Id.

Let us define the Service Ranking for both the OSGI Services


In Line no 8 we have give the service ranking 1000 by using @ServiceRanking(1000). 

In Line no 8 we gave the service ranking 1001 by using @ServiceRanking(1001).
This is interface for the Sling Model.

This is Sling Model's implementation .


This is Sightly written to invoke the Sling Model.

And according to the annotation's working the OSGI service having higher service ranking it will be invoking on priority. And we are getting the same response when we are calling. 


By using this implementation you will be able to any one of the OSGI Service on priority out of two services( MyOSGIServiceOne and MyOSGIServiceSecond). Now i want to call both the services . How can we do that?

Way 2-->

How can we call the two OSGI Service From Sling Model if OSGI Service interface have multiple implementation?

In this way we will use 
Two Implementation and we will remove the @ServiceRanking annotation from both the services.



Sling Model's Interface


Sling Model in this we have added the filter inside the @OSGIService annotation like @OSGIService(filter="(component.name=fully qualified path of the service)")
In Line no 11 we have made the changes

And HTL/Sightly would be 

The Output/response from the OSGI Service would be it should call the MyOSGIServiceSecond because we have written the @OSGiService(filter="(component.name=com.adobe.aem.core.services.impl.MyOSGIServiceSecond)") in line no 11 in Sling Model.

Response is as expected.

Way 3-->

How can we call the two OSGI Service From Sling Model if OSGI Service interface have multiple implementation?


Now we will see third way how can we call the multiple implementation of a OSGI Service using name meta data property.
  • We have interface for OSGI Service 
  • We have two implementation classes for the OSGI Service.  
    At line no 7 we have added the name meta data property using name="implementationone" 
    We have added the name meta data property in line no 7.
  • Interface for Sling Model  

  • Implementation for the Sling Model 
    We have called using line no 11 in Sling Model using @OSGiService(filter="(component.name=implementationtwo)")
  • HTL/Sightly 
  • And Output/Response from the OSGI Service is 

These are the three ways how can we create and call the Multiple implementation of a OSGI Service using Sling Model.

No comments:

Post a Comment

Headless In AEM

Headless In AEM  By reading this article we will be able to understand headless approach. Many time we went to interview and maximum time th...