You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Alexandre Ribeiro <ho...@gmail.com> on 2013/02/15 10:55:21 UTC

Context vs servicetracker vs servicelistener

Hello

I would like to understand the difference between getting the service using
the context (start method) or by using the servicetracker.

By the way, servicelistener is usefull to be notified about changes in the
services and to avoid pulling for them. But what about service tracker?


Using context we can save it in a member variable and use it later to get
services. It seems that it makes the same of servicetracker. Could you
highlight me the real differences between using context and servicetracker
to get service references?



Thank you

Re: Context vs servicetracker vs servicelistener

Posted by Roland <wg...@ids.de>.
Hi,
I can confirm that race conditions can occur if you use the ServiceListener.
But this can also happen with the ServiceTracker! Sometimes, the
ServiceTracker can not catch a Service even though this service is
registered. To solve the problem I use waitForService() instead of
getService().

Regards
Roland



--
View this message in context: http://apache-felix.18485.x6.nabble.com/Context-vs-servicetracker-vs-servicelistener-tp5002076p5004269.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Context vs servicetracker vs servicelistener

Posted by Neil Bartlett <nj...@gmail.com>.
If you use BundleContext, you will get just an instantaneous snapshot of
the current services; you will not hear about any services registered or
unregistered after you got the snapshot.

If you use ServiceListener, you will only get events for services
registered or unregistered after you create the listener, but you will miss
the services that were already registered before that.

So if you use this low-level API, you need to both query BundleContext
*and* create a ServiceListener, and you need to do it very carefully
because of the potential for race conditions. This is in fact quite
complicated and difficult. ServiceTracker exists to make it much easier,
because it notifies you of services registered before you opened the
tracker, *and* services that are registered later on. Therefore I strongly
recommend ServiceTracker over the lower level context query +
ServiceListener approach.

However there is a higher level approach than even ServiceTracker, called
Declarative Services. This allow you to simply declare a field in your
class and have the service injected automatically. Always use the highest
level abstraction that you can get away with, and only drop down to the
lower levels if you need to do something unusual that is not supported by
the abstraction.

BTW you should not generally store a service in a member variable because
that service might go away very shortly afterwards. You should listen to
the service events and release your reference to the service when asked to.
ServiceTracker handles this for you; it's kind of a "smart pointer".

Neil


On Fri, Feb 15, 2013 at 9:55 AM, Alexandre Ribeiro
<ho...@gmail.com>wrote:

> Hello
>
> I would like to understand the difference between getting the service using
> the context (start method) or by using the servicetracker.
>
> By the way, servicelistener is usefull to be notified about changes in the
> services and to avoid pulling for them. But what about service tracker?
>
>
> Using context we can save it in a member variable and use it later to get
> services. It seems that it makes the same of servicetracker. Could you
> highlight me the real differences between using context and servicetracker
> to get service references?
>
>
>
> Thank you
>