users@glassfish.java.net

Re: Making a singleton bean

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Sun, 13 Apr 2008 14:11:41 +0200

In EJB3.1 there is going to be special kind of stateless session bean
with @Singleton annotation. In 3.0, however, there is no dedicated
support for this, but I had similar problem and this is how I resolved
this: I've created regular stateless session bean and created
interceptor. That interceptor is using Java build in synchronization,
so it works only when entire application is running inside single JVM,
but that would be easy to make it work in distributed environment
using, for example, database locking capabilities.
OK, here is how does my SyncInterceptor look like:
/**
 * @author witoldsz
 */
public class SyncInterceptor {
    @AroundInvoke
    Object synchronize(InvocationContext iCtx) throws Exception {
        Class lock = iCtx.getTarget().getClass();
        synchronized (lock) {
            return iCtx.proceed();
        }
    }
}

And now, whenever I need my session bean to be executed no more than
once at any given time I annotate it with that SyncInterceptor and it
works just fine. Any concurrent call is queued by interceptor.

Regards,
Witold Szczerba


2008/4/13, glassfish_at_javadesktop.org <glassfish_at_javadesktop.org>:
> Dear All,
> I would like to create singleton stateless session beans with glassfish. Can anyone tell me the procedure for the above??
> [Message sent by forum member 'rebin' (rebin)]
>
> http://forums.java.net/jive/thread.jspa?messageID=268947
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>