Once I went to a interview and after introducing myself the interviewer asked me a question and the question was have you worked on OSGI Services? My answer was yes I have worked on OSGI Services than he asked me a question and the question was "How can you give the multiple implementation of a OSGI Service and how you can call it?" I said I know how we can create a OSGI Service and how we can call that OSGI Service in different-2 backend modules servlet and in OSGI Service itself but I did not came across this situation where we have to give the multiple implementation of a OSGI Service. Than he said okay I am done HR will let you know means my interview has been closed with in 3-5 minutes. After that I realized I should make something on this scenario so that anyone who is working on AEM will be able to answer this question.
By reading this article you will be able to know how to create and call multiple implementation of a OSGI Service by using Servlet only if you want to create and call multiple implementation of a OSGI Service by using Sling Models you can click here
How can we give the multiple implementation of a OSGI Service?
How?
- We will create a interface and declare one method in it?
- I have created two implementation class MyOSGIServiceImplOne and MyOSGIServiceImplTwo and interface(MyOSGIService) is same for both the implementations.
- We have to make the implementation class as a OSGI service by annotating it at class level and we have to give the custom properties
- We have to give the property in key value pair as we gave property ={"type=implementationfirst"} and property={"type=implementationsecond"}.
- To invoke these implementations we will be needing a Servlet. We can use the Sling model as well but we will use the Sling path type servlet and will invoke our services
- And it is giving response as expected
- Now we will call our OSGI Service from our Servlet
- And result is
- Always MyOSGIServiceImplOne is calling because is you have multiple implementation of a service so there is a ranking associated with the service and while calling that service will be called who will be registered latest.
- How can we explicitly call the particular OSGI Service? To call the particular OSGI service we have a attribute called target we have pass it inside the @Reference annotation and we have to give the name of the service like @Reference(target="(type=value of the OSGI Service)") In line no 23 we have made the changes .
- In this way we can explicitly call the different-2 implementation of a particular service.
- Now in this way we can call the particular implementation of a OSGI Service
- And response is We can decide in which order we want to call.
How to decide at runtime which service you want to call dynamically?
- First of all we will create a map of our OSGI Service private static final Map<String, MyOSGIService> myOsgiServiceMap=new HashMap<>();
- Create the reference for your OSGI Service @Reference(service = MyOSGIService.class,policy = ReferencePolicy.DYNAMIC,cardinality = ReferenceCardinality.MULTIPLE,bind = "bindMyOSGIService",unbind = "unbindMyOSGIService")
- MyOSGIService can have multiple implementation so the policy which should be attach with this is DYNAMIC Tag and it means this component will start when the reference is available or not. But if a new reference available it will take that rfeference.
- Since we have multiple implementation and multiple references of the services we have to define the cardinality of the services the cardinality of the service will be multiple it means there can be more than one service implementation attached to it.
- We are defining the bind method name and unbind method name of the service.
- Our bind method will be bindMyOSGIService and unbind method will be unbindMyOSGIService.
- We have to give the implementation for unbindMyOSGIService and bindMyOSGIService methods.
- In this way we can call the multiple implementation of a OSGI Service
- The response from the OSGI Service is (when path is /bin/pages/osgicallernew?type=implementationone)
- We can use the name property like
- In Line No 7 we have given the name property in Implementation class of OSGI Service in both the services.
In Line no 23 and 25 we have given the @Reference(target="(component.name=service name defined in the implementation service)")
Third way to call the Multiple Implementation Of OSGI Service in a Servlet?
In line no 23 and 25 we have to give the @Reference(target="(component.name=fully qualified path of implementation class of your OSGI Service)")
Note:-By using these annotations or by using the same way you will not be able to call the Multiple implementation of OSGI Service in Sling Model to know more about it click here
After reading this article you will be able to answer few questions
No comments:
Post a Comment