Diff btw ServiceTracker, ServiceReference & ServiceReferences


I am new to OSGi and have been studying it for a while.

I want to know the key differences in usage and the benefits for <em>ServiceTracker, ServiceReference & ServiceReferences</em>.

I used an example with both ServiceTracker & ServiceReference and if the service isn't available then both return NULL. However if the service is available then both work just fine, but I couldn't observe what is the difference between two.

Somewhere I read <em>ServiceTracker is a programmatic way to acquire a reference to a service. i.e. write ServiceTracker code that "tracks" a reference to another service and let's you use it when it becomes available.</em> but in actual practice I couldn't see it how its done!

Can anyone plz explain it? Thank you.


I recommend that you use neither!

ServiceReference simply represents an entry in the OSGi service registry. You probably mean that you are accessing services using API calls like BundleContext.getServiceReference(). This is the lowest possible level of working with OSGi services, and as you have probably noticed it is quite complicated and it couples your code to the OSGi APIs.

ServiceTracker is very slightly higher level, it wraps around the lower level calls. The key advantage is that it notifies you when a service goes away, which is very important because services are dynamic. However ServiceTracker is still very low-level, and it still couples your code to OSGi APIs.

<strong>I strongly recommend avoiding both the lowest level API (based around ServiceReference) AND the ServiceTracker until you have gained a lot more experience with OSGi.</strong>

As an alternative I recommend learning Declarative Services. This gives you a programming model based on dependency injection and it decouples you from OSGi APIs. It is much much easier <strong>and</strong> safer! You can start by following the tutorial here: <a href="http://bndtools.org/tutorial.html" rel="nofollow">http://bndtools.org/tutorial.html</a>


I agree with what Neil said but I need to extend upon it to answer your comment: If you want to build your project with declarative services using Maven you have to use the maven-bundle-plugin and the maven-scr-plugin. The former uses <em>bnd</em> under the hood while the latter processes your DS annotations. Example:

<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>2.3.7</version> <extensions>true</extensions> <configuration> <instructions> <Bundle-Name>${project.name}</Bundle-Name> <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName> <Export-Package>${project.artifactId}.api</Export-Package> </instructions> </configuration> </plugin> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-scr-plugin</artifactId> <version>1.9.0</version> <executions> <execution> <id>generate-scr-scrdescriptor</id> <goals> <goal>scr</goal> </goals> </execution> </executions> </plugin>

In this example (which is in the build section) I direct the maven-bundle-plugin to export all the packages in my ${project.artifactId}.api directory (this is a convention in my OSGi projects) and the maven-scr-plugin generates xml cofiguration based on my Apache Felix declarative services annotations.

I suggest you should read about Apache Felix declarative services annotations and how they work because it will help you much in the long run.

I have an example OSGi application you can take a look at: <a href="https://github.com/BlackBelt-OSGi-Training-Material/basic-osgi-example" rel="nofollow">here</a>


  • Class instance change another instance
  • ajaxify multipart encoded form (upload forms)
  • Qt:Signals and Slots vs C++:Message Passing
  • Array filter in Swift3
  • Any way to use interfaces with wpf HierarchicalDataTemplate
  • Wpf RichTextBox wrapping problems
  • Seamless scrolling text
  • Embed multiple jpeg images into EXCEL programmatically?
  • GWT Toolkit: preprocessing files on client side
  • Can I call custom javascript from an R jupyter notebook
  • Linux over commit heuristic
  • Concise regex extract function in XSLT 2.0
  • How can I stop my python script when another python script is running?
  • Get rendered html code in Backing Component from Composite Component
  • record audio in HTML / js without Flash?
  • How to set `secure` and `httpOnly` for Plones `__ac` cookie?
  • How to select table rows/complete table?
  • Does the MySQL IN clause execute the subquery multiple times?
  • Should I be afraid to use UDP to make a client/server broadcast talk?
  • Is there a way to call library thread-local init/cleanup on thread creation/destruction?
  • Classic ASP URL Rewriting
  • Why can't I use non-integral types with switch [duplicate]
  • How do I translate LR(1) Parse into a Abstract syntax tree?
  • Get used tables from sql query [duplicate]
  • Efficient User-Agent Regex to find Safari in Python
  • Detecting # in Scheme list
  • Implement JwtBearer Authentication in NSwag SwaggerUi
  • Access user's phone number on iOS 7
  • Git describe fails to return most recent annotated tag
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • What's the purpose of QString?
  • onBackPressed() not being executed
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Java applet as stand-alone Windows application?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • When should I choose bucket sort over other sorting algorithms?
  • PHP: When would you need the self:: keyword?
  • Java static initializers and reflection
  • How to get Windows thread pool to call class member function?