4 Replies Latest reply on Jun 27, 2012 4:39 PM by sgilda

    Is there a bean implementation that is like a "Stateful Singleton Bean"?

    madchedar0 Newbie

      I'm migrating code to JBoss 7.1.1... and I have some services which used to be singleton services.  Basically there is just one instance of the service floating around in the Application Server and it is holding/updating a state that other parts of the system might ask for.  Let's call it "ObjectManager"


      I would like to have the ObjectManager be created at startup of the Application Server.  It should be a singleton.  It should be stateful.


      @Singletons are by definition Stateless so that's out.

      @Stateful is good but you can't specify that the ObjectManager be created on start up can you?


      Are there any acceptable solutions developers use to have a "Stateful Singleton" in their system?


      Here's one idea I had:


      Maybe you create an "ObjectManagerInitializer" bean that is a singleton... and all it does is create one ObjectManager bean?  Is that a good approach?



      import javax.annotation.PostConstruct;
      import javax.annotation.PreDestroy;
      import javax.ejb.EJB;
      import javax.ejb.Singleton;
      import javax.ejb.Startup;
      import org.apache.log4j.Logger;
      public class ObjectManagerInitializer {
          ObjectManagerLocal dpm; // local view of the ObjectManager bean
          public ObjectManagerInitializer(){} 



      And ObjectManager would look something like the following:



      public class ObjectManager
              implements MessageListener, Serializable, DisplayPersistenceManagerLocal,
              DisplayPersistenceManagerRemote {
         // code


      What do you guys think?  Is there a better way to do this?