How we can create and call the multiple implementations of a OSGI Service using Sling Models?
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
- 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
How AEM Engine allocates the Service ID to the OSGI Services?
How you can estimate the Service id?
- MyOSGIServiceOne
- MyOSGIServiceSecond.
- MyOSGIServiceOne
- MyOSGIServiceSecond
- MyOSGIServiceOne comes first and Service Id is 8435 .
- MyOSGIServiceSecond comes last and Service Id is 8436 .
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
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.
Way 2-->
How can we call the two OSGI Service From Sling Model if OSGI Service interface have multiple implementation?
Sling Model in this we have added the filter inside the @OSGIService annotation like @OSGIService(filter="(component.name=fully qualified path of the service)")
Way 3-->
How can we call the two OSGI Service From Sling Model if OSGI Service interface have multiple implementation?
- We have interface for OSGI Service
No comments:
Post a Comment