mnlipp / osgi-getting-started

OSGi Getting Started for JDK Users
https://mnlipp.github.io/osgi-getting-started/
GNU General Public License v2.0
24 stars 9 forks source link

Comments on "Tracking a Service" #11

Open mnlipp opened 6 years ago

mnlipp commented 6 years ago

Please append any comments to this issue.

alexei-tovarisch commented 2 years ago

Could you provide some clarification on this sentence:

...But we cannot use getService() here because the new // service hasn't been added to the tracked services yet (we're in // the process of adding). We have to decide on our own.

Why do you say that in the addingService() method the service is not tracked and we cannot use getService().

In this book I find the opposite (chapter 4.8 page 93)

mnlipp commented 2 years ago

You have to read this with the argument list in mind:

... we cannot use ServiceTracker.getService() here ....

Neil Bartlett in his book uses context.getService(ref), a completely different method.

Now you might ask, why shouldn't I simply use the service that I can get from the service reference passed to addingService(ServiceReference ref) as shown in the book? Well, because in general, you can have several instances of the same service at the same time and you don't know if the one being added is the preferred one. ServiceTracker.getService() would return the preferred one, if we could call it. But as we are in an "adding" callback (and not in an "added" callback, which doesn't exist), the service being added is not taken into account by ServiceTracker.getService() yet.