dev@glassfish.java.net

Re: Thoughts on Threadpool handling of context ClassLoader

From: Lloyd L Chambers <Lloyd.Chambers_at_Sun.COM>
Date: Fri, 27 Apr 2007 14:24:52 -0700

I think it would be better to decouple from PEMain by using
FeatureAvailability to expose the classloader.

Here is one way to do it with FeatureAvailability, whose raison
d^etre is to decouple dependencies, especially among multiple threads
and/or unrelated packages.

...
    public PEMain() {
        // Set the context class loader
        _loader = getClass().getClassLoader();
        Thread.currentThread().setContextClassLoader(_loader);
        FeatureAvailability.getInstance().registerFeature
( CONTEXT_CLASSLOADER_FEATURE, classloader ); ******
        AdminEventListenerRegistry.addShutdownEventListener(new
Shutdown());
        _instance = this;
    }

elsewhere:
...
      ClassLoader cl = FeatureAvailability.getInstance
().waitForFeature( ******
           CONTEXT_CLASSLOADER_FEATURE, "<calling site description>" );
...

You could use Switch.getSwitch(), but I think FeatureAvailability is
better for a multi-threaded startup, and introduces no compile-time
dependencies.

Lloyd

On Apr 27, 2007, at 2:16 PM, Ken Cavanaugh wrote:

> Lloyd L Chambers wrote:
>> One thought:
>>
>> Take a look at the AtStartup and LoadMBeanServer classes which are
>> used in their own threads from PEMain.main(). Similar threads can
>> be started later, say in PEMain.run().
>>
>> You could kick off a similar thread to initialize a thread pool.
>> You could also (optionally) make the classloader and/or thread pool
>> (s) available via FeatureAvailability:
>>
>> FeatureAvailability.getInstance().registerFeature
>> ( CONTEXT_ClASSLOADER_FEATURE, classloader );
>>
>> Later code can call FeatureAvailability.getInstance
>> ().waitForFeature( CONTEXT_ClASSLOADER_FEATURE, ... ) and will
>> block until it's ready.
>>
>> I'm not sure that's the right solution here, but I thought I'd
>> mention it.
>>
> I don't think a separate thread is needed. In fact, all I really
> need is to save a ClassLoader
> at startup that can be used later when the ThreadPool is created.
> It looks like PEMain
> already does this: the constructor is:
>
> public PEMain() {
> // Set the context class loader
> _loader = getClass().getClassLoader();
> Thread.currentThread().setContextClassLoader(_loader);
> AdminEventListenerRegistry.addShutdownEventListener(new
> Shutdown());
> _instance = this;
> }
>
> So clearly the _loader data member contains exactly what I want.
> Is adding an accessor
> method to PEMain to get _loader reasonable? Then I could simply use
>
> PEMain.getInstance().getDefaultContextClassLoader()
>
> to access what I need. This could be passed to the ThreadPoolImpl
> constructor
> (which requires an ORB change) in the
> S1ASThreadPoolManager.createThreadPools
> call.
>
> Thanks,
>
> Ken.
>