>>> Singleton + Never released + Last started <<<

Discussions

EJB design: >>> Singleton + Never released + Last started <<<

  1. Hi,

    I need to create a bean that :

    1 - will be a singleton
    2 - Must never been released
    3 - Must start after the EJB server is ready and all other
    EJB are deployed.

    I do not know how to do this !
    I m searching for a design strategy. What bean I can use, ...

    I really need some help

    Thanks a lot for your help

    Christophe
  2. Hi,

    One easy way to solve all ur problem is by having a static class called SingletonEJB in following fashion

    public class SingletonEJB
    {
        private EJBBean ejbInstance = null;

        public SingletonEJB()
        {
           Context ctx = InitialContext.getInitialContext();
           EJBBeanHome ejbBeanHome = ctx.lookUp("SomeName");
           ejbInstance = ejbBeanHome.create();
        }
       
        public static EJBBean getInstance()
        {
           return ejbInstance;
        }
    }


    Now u can load this class at start up by specifying in server properties file. This class will hold the reference for ur singleton EJBBean.....

    U can access the instance by calling SingletonEJB.getInstance() in any of ur client code...

    Hope this solves ur problem

    Gopesh Desai
    Cognizant Techonology Solutions
  3. I don't mean to critisize this last piece of code, but what's that??? It won't even compile. becuase static method will try to return instance variable. Making allwoance for typo - that is, ejbInstance should be declared static, it's still not clear where it is initialized. This public constructor, where is it called? And what prevents this bean instance from being released?
  4. public class SingletonEJB
    {
        private static EJBBean ejbInstance = null;

     /*this is protected so it can't be called outside this class and still be subclassed.*/
        protected SingletonEJB()
        { }
        
        public static synchronized EJBBean getInstance()
        {
           if (ejbInstance == null)
           {
             Context ctx = InitialContext.getInitialContext();
             EJBBeanHome ejbBeanHome = ctx.lookUp("SomeName");
             ejbInstance = ejbBeanHome.create();
           }
           return ejbInstance;
        }
    }


    this version should compile and work (I didn't try to compile it but its a pretty simple singleton). Don't know if this will necessarily solve your problem without a more detailed understanding of your specific requirements, but hopefully it gets you thinking along the correct lines.

    Cheers,
    Amir
  5. Sorry for the two replies, just thought I should mention that this is not a bean but a simple singleton class and that its actually a VM singleton rather than a machine or system singleton so if your using a distributed app server(multiple app server instances for load balanceing for example) or an app server that allows you to have multiple VMs in one app server (such as GemstoneJ) this type of singleton will not work. There are other things you can do, but they are more complicated, if you want to get into a more detailed discussion we can, but I don't have time to post too much more now

    Cheers,
    Amir
  6. You can implement this as an RMI object (pinned to a specific box if you want a cluster-wide singleton). This is probably the most portable.

    Another option is to use stateless session bean and deploy it with max-beans-in-pool set to 1 - all accesses to it will be serialized by the container and it will act as a singleton. There is cheating involved, because the bean remembers it's state between invocations, and there is risk that container may decide to discard the instance.
  7. Why not just use a stateful session bean ? This way , there will not be any risk of the bean being released ..