felix-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Richard S. Hall" <he...@ungoverned.org>
Subject Re: Question regarding time of class loading and OSGi-threading tips
Date Fri, 21 Jan 2011 00:57:45 GMT
On 1/20/11 6:02 PM, Per-Erik Svensson wrote:
> Hi,
> I would like to know when a class is loaded. I guess it is a bit different
> depending on things like usage of static members and the like but... Say
> java.util.Logger. I have a bundle that only consumes services. When the
> bundle is started it is running a few background threads. When the bundle is
> stopped, the background threads are cancelled. When they are cancelled, they
> will post a task on the event dispatch thread (I'm using swing workers) and,
> since they were cancelled, they will log just that ("Background thread
> interrupted" or similar is written to log).
> Now, everything worked fine up until the edt-task was about to log. At that
> point, felix threw an exception. Sadly, I don't have the stack trace atm and
> I'm not sure I can reproduce. I'm just curious if anyone knows what this can
> be. The offending line was calling
> java.util.Logger.getLogger(getClass().getName()).log(Level.INFO, "BG-thread
> cancelled) //or similar
> And remember that the bundle jar-file has been removed (with the help of
> FileInstall) from the system. It might linger in the bundle cache (I don't
> know how long it stays there) but the bundle is stopped and (presumably)
> uninstalled.
> I just removed the logger code to make it work. However, I'm suspecting that
> the cause of the error is either
>     1. getClass() must have a class loader and that is gone because the
>     bundle is uninstalled.

If you have a thread executing on the class, then the class loader isn't 
gone since the thread has a reference to the class and the class has a 
reference to its class loader.

>     2. The static Logger isn't loaded yet or uses classes that aren't loaded
>     yet and thus felix informs me that it cant find the class.

Something like this could be.

> So, (a) when is a class loaded, (b) is there any good "pattern" for dealing
> with disposing threads in OSGi, and (c) am I completely off here? :)

For (a) it just depends. They are loaded on demand by the JVM. In some 
cases loading a class will cause other class loads for dependent types 
necessary for defining the class. For types used in methods, they will 
be loaded on demand (although I'm not sure if this is mandated).

For (b), the best you can do is stop your threads in your activator 
stop() method. Your bundle will not be uninstalled until after it 
returns from its stop() method. In Felix, we don't hold locks for class 
loading, so you can end up with situations were threads will have issues 
loading classes from a dependent bundle, but they won't have problems 
loading classes from themselves as long as they are stopped in their 
activator stop() method.

-> richard

> Ok, (d) would it help writing "MyClass.class.getName()"?
> Regards,
> Per-Erik Svensson

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

View raw message