Search code examples
osgiservice-referenceblueprint

How do i reconnect when an OSGI service with an higher ranking comes availible?


I have two osgi service which implements the same interface with a different quality-of-service. One has a default ranking of 0 and the other one has a ranking of 3.

I am searching for a blueprint configuration where the default service acts as a fallback. So every other bundle should use the best available service (highest ranking) and must fallback to the default one if the better service disappears.

That is working right now.

The missing part is to automatically reconnect to the better service, when it comes online again. Is there a way to do it by configuring just the services without changing the consumers (implement ServiceTrackers an so on)?


Solution

  • I think this is not directly possible as a simple blueprint <reference>.

    You can use the <reference-listener> element though.

     <reference-listener bind-method=”bind” unbind-method=”unbind”>
       <bean class=“MyListenerClass”/>        
      </reference-listener>
    

    It allows to be called back when references change and react on these. So you can for example create a proxy for your service that makes sure the right service is called. This is not very pretty code though. So like BJ suggested declarative services may provide a cleaner solution for this case.