Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » BundleActivator registers a service implemented as singleton
BundleActivator registers a service implemented as singleton [message #104587] Thu, 31 January 2008 12:00 Go to next message
Eclipse User
Originally posted by: martin.testrot.d-velop.de

Hi,

as far as I know each time a bundle is started a new BundleActivator is
created. So if you start a bundle, stop it and start it again 2
Activator Instances are created. So here comes the question:

1) Wouldn't it be generally useful to implement the service
implementation as a singleton and register the same instance every time
the bundle is started. Otherwise you would end up with a lot of service
instances though you only need one (in my case I even have to guarantee
that there is only one service instance at a time). Or is it somehow
against the OSGI concepts to implement a service as singleton?

Here comes a little code snippet to illustrate my question:

public class Activator impements BundleActivator{

public void start (BundleContext pContext) throws Exception{
// reference to service singleton retrieved by Service.getInstance()
pContext.registerService
(ServiceInterface.class.getName(),
Service.getInstance(),null);
}
}

Thanks for your thoughts,
Martin
Re: BundleActivator registers a service implemented as singleton [message #104601 is a reply to message #104587] Fri, 01 February 2008 07:16 Go to previous messageGo to next message
Karsten Panier is currently offline Karsten Panier
Messages: 57
Registered: July 2009
Member
Hi Martin,

if the bundle is stopped, the service should be removed from the
registration.
Here is a code snippet of an Activator.

//Snip
private ServiceRegistration serviceReg

public void start(BundleContext context) throws Exception {
serviceReg = context.registerService(IService.class.getName(), new
Service(), new Hashtable<Object, Object>());
}

public void stop(BundleContext context) throws Exception {
serviceReg.unregister();
}
//Snap

I hope it helps.
Karsten
Re: BundleActivator registers a service implemented as singleton [message #104835 is a reply to message #104601] Thu, 07 February 2008 05:23 Go to previous messageGo to next message
Eclipse User
Originally posted by: martin.testrot.d-velop.de

Hello Karsten,

thanks for your answer. It made me read the OSGI Specification again,
but I am still a little confused about some things. Perhaps you (or
somebody else) can clarify them.

1) Are you sure that a bundle registering a service must explicitly
call aServiceRegistration.unregister() in the stop-method. Your last
answer says so but I found OSGI examples in the oscar documentation
saying the OSGI framework automatically unregisters the service when
stopping the bundle. Perhaps a oscar specific behavior?

2) For a bundle using a service of another bundle and holding refernces
to this service the situation is similiar. Is it necessary for the using
bundle to release the used service (Context.ungetService(aServiceRef) in
the stop-method or is this done automatically by the osgi framework. I
also found an oscar example saying ungetService is done automatically by
the osgi framework. Also a oscar specific behavior?

3) Are all objects instantiated by a bundle released and garbage
collected as soon as the bundle is stopped? So an object created in the
start-method of a bundle is not created multiple times if this bundle is
started, stopped and started again? I fear in my case there is one
object instance for every time I started the bundle, so I end up with
more than one instance. But perhaps this was due to other bundles still
holding references to service objects created by stopped bundles.

Thanks for everyone taking time to answer my questions,

Martin

Karsten Panier schrieb:
> Hi Martin,
>
> if the bundle is stopped, the service should be removed from the
> registration. Here is a code snippet of an Activator.
>
> //Snip
> private ServiceRegistration serviceReg
>
> public void start(BundleContext context) throws Exception {
> serviceReg = context.registerService(IService.class.getName(),
> new Service(), new Hashtable<Object, Object>());
> }
>
> public void stop(BundleContext context) throws Exception {
> serviceReg.unregister();
> }
> //Snap
>
> I hope it helps.
> Karsten
>
Re: BundleActivator registers a service implemented as singleton [message #104854 is a reply to message #104835] Sun, 10 February 2008 17:22 Go to previous message
Benedikt Arnold is currently offline Benedikt Arnold
Messages: 5
Registered: July 2009
Junior Member
> 1) Are you sure that a bundle registering a service must explicitly
> call aServiceRegistration.unregister() in the stop-method. Your last
> answer says so but I found OSGI examples in the oscar documentation
> saying the OSGI framework automatically unregisters the service when
> stopping the bundle. Perhaps a oscar specific behavior?

You are right. It is not necessary to manually unregister a service in the
stop method. The framework will unregister every service if a bundle is
stopped.

> 2) For a bundle using a service of another bundle and holding refernces
> to this service the situation is similiar. Is it necessary for the using
> bundle to release the used service (Context.ungetService(aServiceRef) in
> the stop-method or is this done automatically by the osgi framework. I
> also found an oscar example saying ungetService is done automatically by
> the osgi framework. Also a oscar specific behavior?


I'am not sure if you must unget a service in the stop-method, but I won't
think so. But in my oppinion it is better to unget the service immediate
after using it and get it again if you need it again. You must keep in
mind, that osgi is a very dynamic environment and you can't be sure when a
service will change. You should use a ServiceTracker to deal with such
dynamics.


> 3) Are all objects instantiated by a bundle released and garbage
> collected as soon as the bundle is stopped? So an object created in the
> start-method of a bundle is not created multiple times if this bundle is
> started, stopped and started again? I fear in my case there is one
> object instance for every time I started the bundle, so I end up with
> more than one instance. But perhaps this was due to other bundles still
> holding references to service objects created by stopped bundles.

There is no immediate garbage collection if a bundle is stopped. I don't
know if I understand your question but every time you call something like
"this.a = new MyObject();" in your start-method a new instance will be
created and you can't be sure if it is collected by the garbage collector
right after you stop your bundle.

Holding a reference to an object created by another bundle is bad smell in
my opinion. You should avoid it as much as possible by using the service
registry and ServiceTracker.

Feel free to reply if you have more questions.

Greetings,

Benedikt
Previous Topic:classpath cycle with pde build
Next Topic:force OSGi Bundles to start
Goto Forum:
  


Current Time: Wed Aug 20 18:32:00 EDT 2014

Powered by FUDForum. Page generated in 0.01571 seconds