felix-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marcel Offermans <marcel.offerm...@luminis.nl>
Subject Re: DependencyManager lifecycle callbacks contract? Aka multiple init() invokes in service impl
Date Mon, 03 Jan 2011 10:09:11 GMT
On Jan 3, 2011, at 10:57 , Bram de Kruijff wrote:

> question/observation about the dependencymanager callbacks contract. I
> noticed that init() was being invoked more then once over the lifespan
> of my service instance because 1) I constructed it with an instance
> (insted of class/factory)

Indeed, if you construct the component and supply an already created instance, the dependency
manager will use that instance every time instead of constructing a new one itself every time
the components needs to be created. So if you create an instance yourself, make sure it can
handle multiple life cycles.

> and 2) I was adding dependencies in init,
> leading to duplicates, leading to multiple service dependecy
> callbacks.

If you add dependencies in init, you run the risk of your component getting deactivated immediately.
Since that can lead to complicated logic in your component, you should use setInstanceBound(true)
for such components, which ensures that if an instance was already created, it will not go
away again, not even when a required dependency is not available. The instance will just sit
there and wait for that dependency to show up again.

> 
>  // Eg. This will result in multiple service dependencies upon
> logservice (un)availibility as we will go throught init/destroy each
> cycle
>  public synchronized void init() {
>        ServiceDependency logServiceDependency =
> m_dependencyManager.createServiceDependency();
>        logServiceDependency.setService(LogService.class);
>        logServiceDependency.setRequired(true);
>        m_component.add(logServiceDependency);
>   }

The other solution would be to immediately add such a dependency instead of dynamically adding
it after the component is being activated.

By the way, why do you make LogService a required dependency? In general I think that's a
bad idea, since it will bring down your whole application if logging for some reason is not
available (if you update the log bundle).

> My (naive) assumption was that init() would be called once and only
> once an a service instance, but as it seems init/destroy is called
> each time a required service becomes (unavailable).

[...]

> Question is why and what is the contract? Obvisouly, the fact that
> this results in different behaviour depending on how one instantiates
> the Component is rather confusing :S

See above, it will only if *you* supply an already created instance. In that case, I cannot
assume I can create a new instance so I have to reuse the existing one. If you don't want
that, either supply the class or a custom factory so I can invoke it to create an instance.

Greetings, Marcel


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


Mime
View raw message