Hi João,

The reason is that in OSGi you get the actual object vs a proxy to the
object...so when you get the service you are getting a strong
reference to the actual object. Even though you have unregistered the
service from the OSGi registry you are still keeping the object alive
in the JVM by referring to it via a field in your class.

Beginners best practice is to not store references to services and always use:

ServiceReference refs = context.getServiceReference(JL.class.getName());
JL serv1 = (JL) context.getService(refs);
if ( serv1 != null ) serv1.read();
context.ungetService(refs);

In each call to read. But this get's verbose pretty quickly, so a
better pattern is to use org.osgi.util.tracker.ServiceTracker and
org.osgi.util.tracker.ServiceTrackerCustomizer to track the services
as they come and go. This way you can get an event via the customiser
to tell you when the service has been removed - to allow you to tidy
up your reference in your class.

But again this can get pretty fiddly with non trivial dependency
relationships (i.e. A depends on (B and C)). For this case you might
consider using a IOC framework like Declarative Services or iPOJO
which handle some of the service lifecycle issues in OSGi without the
need to use trackers.

Regards,

Dave

2009/4/3 João Ferreira <[email protected]>:
> Hello
>
> I was doing some experimentations with OSGi services, more precisely what
> happens with bad construction of services. For example i have the following
> bundles:
>
>   * JLService (only contains the interface JL):
>         o public interface JL {public boolean read(); }
>   * JL1, implementation of JLService that returns false on read method
>   * JL2, implementation of JLService that returns true on read
>   * JF, a bundle that consumes the JLService and gives a shell command
>     to invoke JL.read()
>
> I am implementing the registering/discovery of services the worse possible
> way.
> On JL1 and JL2 start():
>   ref = context.registerService(pt.thesis.JL.service.JL.class.getName(), new
> JL1(), null); /*in JL2 is JL2()*/
> On JL1 and JL2 stop():
>   ref.unregister();
>
> On JF start():
>       refs = context.getServiceReference(JL.class.getName());
>       serv1 = (JL) context.getService(refs);
> On JF stop():
>      context.ungetService(refs);
>
> i start the bundles and JF is binded to JL1. if i stop JL1 and call the
> command to invoke JL.read() why don't i get a runtime exception or
> something?
>
> Hope i explained my problem well
> Thanks in advance
>
> João Ferreira
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>



-- 
-------------------------------------------------------------------------------------

Paremus Limited. Registered in England. Registration No. 4181472

Registered Office: 22-24 Broad Street, Wokingham, Berks RG40 1BA

Postal Address: 107-111 Fleet Street, London, EC4A 2AB

The information transmitted is intended only for the person(s) or
entity to which it is addressed and may contain confidential and/or
privileged material. Any review, retransmission, dissemination or
other use of, or taking of any action in reliance upon, this
information by persons or entities other than the intended recipient
is prohibited.

If you received this in error, please contact the sender and delete
the material from any computer.

-------------------------------------------------------------------------------------

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to