My Blog List

Friday, May 19, 2023

Multiple Implementations Of OSGI Service And how to call the multiple implementation of a OSGI Service Dynamically.

 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?

Suppose you have a OSGI Service and you have multiple implementation for that OSGI Service how you can do that and how you will reference it. It could be for a business scenario where you will have the multiple implementation of a OSGI Service. The people are against the multiple implementation of a service while i going through the forums. So depending on your use case you can give the multiple implementation of your service. The main question was when you have the multiple implementation of a service how you will invoke the particular service? that was the main question.

How?

  • We will create a interface and declare one method in it?
Create Two Implementation 

  • 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.
Now I know which implementation I want to call. But if I will be able to know at runtime which implementation I want to call how we can do that?

How to decide at runtime which service you want to call dynamically?

For this we will make a map of all the services and call the required service as per the requirement.
  • 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)
  • And when Path is (/bin/pages/osgicallernew?type=implementationsecond) 

    Second way to call the Multiple Implementation Of OSGI Service in a Servlet?

  • 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:-If you have given the name property in the OSGI Service's implementation than you have to call it like @Reference(target="(component.name=name defined)")

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

  1. How to give the multiple implementation of a OSGI Service.
  1. How to call the OSGI Service if you have more than one implementation.
  1. How to decide at runtime what OSGI Service you want to call?

If you have any doubt/clarification you can write a email on aemwheel@gmail.com.

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...