Multiple implementation of a Sling Service | Community
Skip to main content
April 11, 2016
Solved

Multiple implementation of a Sling Service

  • April 11, 2016
  • 5 replies
  • 2344 views

Hi experts,

   I came across a  scenario through one of my colleagues. I believe you guys can help me find solution for this issue.

   What happens if there are multiple implementations for a service interface?  

For instance consider the following interface,

 

public interface CheckService { public String getDetails(); }

and the implementations classes as follows.

Implementation1:

import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Property; import org.apache.felix.scr.annotations.Service; @Component(enabled=true,immediate=true) @Service(value=CheckService.class) @Property(name="id",value="check1") public class CheckServiceImpl1 implements CheckService { @Override public String getDetails() { return "I am from Impl 1 - One"; } }

Implementation2:

import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Property; import org.apache.felix.scr.annotations.Service; @Component(enabled=true,immediate=true) @Service(value=CheckService.class) @Property(name="id",value="check2") public class CheckServiceImpl2 implements CheckService { @Override public String getDetails() { return "I am from Impl 2 - Two"; } }

When i tried to invoke the service from JSP, the output i got is "I am from Impl 1 - One". I am not sure why implementation class2 didnt get invoked. It would be really helpful if you experts could provide some thoughts.

Thanks,

Jai. 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Jitendra_S_Toma

Hi,

Set the OSGI service ranking. Higher Ranking service will be picked. Service ranking setting is like any other property.

Let me know if you need any other information.

Regards,

Jitendra

5 replies

Jitendra_S_Toma
Jitendra_S_TomaAccepted solution
Level 10
April 11, 2016

Hi,

Set the OSGI service ranking. Higher Ranking service will be picked. Service ranking setting is like any other property.

Let me know if you need any other information.

Regards,

Jitendra

April 11, 2016

Wow!! That is something new for me. Thank you Jitendra for the response

Jitendra_S_Toma
Level 10
April 11, 2016

Jayapal S wrote...

Wow!! That is something new for me. Thank you Jitendra for the response

 

You are welcome.

Yogesh_Upadhyay
Level 6
April 14, 2016

Hello Jitendra,

Just curious if this is what you actually want ? With service ranking, your other service will not get picked and it defeats purpose of design to interface.

Yogesh

April 15, 2016

Hi Yogesh,

   If we have two different implementations for a service interface then we can define property to each implementation as some id and then use sling script helper's getService method or we can use Felix resolution to invoke the implementation that we needed.

 

   But yes, your thoughts are indeed valid even i would love to listen Jithendra's response.