users@glassfish.java.net

Re: access glassfish thread pool

From: Ancoron Luciferis <ancoron.luciferis_at_googlemail.com>
Date: Fri, 28 Sep 2012 20:41:24 +0200

On 09/28/2012 07:56 PM, Laird Nelson wrote:
> On Fri, Sep 28, 2012 at 2:46 AM, Nicolas Beck <Nicolas-Beck_at_gmx.de
> <mailto:Nicolas-Beck_at_gmx.de>> wrote:
>
> I want to use the glassfish Threadpool since creating own threads
> within my jee application is not avoidable. Since glassfish API
> changed it is not possible to use spring frameworks
> GlassFishWorkManagerTaskExecut__or any more for this
> [http://forum.springsource.__org/showthread.php?85272-__GlassFishWorkManagerTaskExecut__or-spring-and-glassfish-v3-__exception
> <http://forum.springsource.org/showthread.php?85272-GlassFishWorkManagerTaskExecutor-spring-and-glassfish-v3-exception>]
> But there must be a way to have access to the thread pool. I can't
> understand why this is so restrictive?
>
>
> https://github.com/ljnelson/edugility-executorservice-ejb might help you
> out.
>
> Best,
> Laird
>
> --
> http://about.me/lairdnelson
>

Well, that implementation is basically unnecessary and effectively the
same as using your own EJB for doing the work.

This is, because in GlassFish (as in most JavaEE implementations) there
are two different things to be aware of when it comes to EJBs:

1.) threads
2.) instances

When using an EJB you're just using some (pooled) instance and not on
your own thread. This means, if your code relies on ThreadLocal or
similar thread-bound data that implementation is not going to work for
you reliably as the same EJB instance can be assigned to a different thread.

So, if you really need your own thread but are running inside a JavaEE
container it can only mean:

1.) your code is not prepared for JavaEE
2.) you're using some external stuff that doesn't know what JavaEE is

So, if the code of the logic is in your hands, then please refactor it.

If it is not (hence, option 2), then you could (almost) safely assume
that it'll work with your own thread pool.

As an alternative you could also try with a custom thread pool
configured in GlassFish:

http://javahowto.blogspot.de/2011/02/how-to-create-and-look-up-thread-pool.html

This way, you could have full control over thread creation/reuse and all
the stuff you probably need and still be able to access it easily via
injection.


Cheers,

        Ancoron