-------- Original Message  --------
Subject: Re: Can the thread context classloader issue be solved at all?
From: Richard S. Hall <[email protected]>
To: [email protected]
Date: 15.04.2010 23:47

> On 4/15/10 5:24 PM, Dan H wrote:
>> Libraries such as Hibernate and StAX that use
>> Thread.getContextClassLoader() do not work (well) in Felix / Karaf.
>>  From what I've read it's because the OSGi Alliance never got around
>> to specifying how this method should behave in an OSGi environment.
>>
>> I was wondering if it is possible in theory to create a bundle that
>> provides a "correct" thread context classloader for all other bundles?
>> I imagine it would involve writing a custom delegating classloader and
>> a bundle listener that basically just make all exported classes from
>> any bundle visible to the thread context classloader. However, I don't
>> see a way to reliably set the context classloader since the main
>> thread is managed by Felix.
>>    
> 
> There is no reliable way to do this since there are underlying JRE
> issues that cause memory leaks.
> 
> Such an approach as you suggest is implemented by Equinox and its
> "context finder" class loader, but they uncovered these various issues
> and this is why it has never been spec'ed by OSGi, not because they
> never got around to it.
> 
> -> richard
> 

Thanks for the clarification, I hope the powers that be can find a way
to solve this issue in the near future. I wouldn't mind at all if Java
just deprecated the whole getContextClassLoader backdoor (wishful
thinking, I know).

For now I'm just running things that require the context classloader in
a separate thread. Having to wrap everthing in Callable/Runnable is not
really a problem, yet.

--------------------------------------------------------------------
ExecutorService threadPool = Executors.newSingleThreadExecutor(new
ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setContextClassLoader(
                        MyBundleThatImportsHibernate.class.getClassLoader());
                return t;
        }
});

//then

threadPool.submit(someCallable).get();
                
--------------------------------------------------------------------

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

Reply via email to