BundleActivator registers a service implemented as singleton [message #104587] |
Thu, 31 January 2008 17:00 |
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 #104835 is a reply to message #104601] |
Thu, 07 February 2008 10:23 |
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
>
|
|
|
|
Powered by
FUDForum. Page generated in 0.02768 seconds