11 Replies Latest reply on Dec 14, 2004 10:37 AM by xavierpayne2

    how to use singleton

    hu jinquan Newbie

      I have a problem with singleton in cluster environment .
      first ,there is a singleton mbean ,and I use jmx-console to test it , the singleton works well like the jboss cluster document said,the mbean's MasterNode has the same value with mbean-singletonController's MasterNode,and turn "true" or "false" at the same time.and in the cluster there is only one MasterNode that its value is "true".

      the problem is ,each JVM has a singletMBean instance and had been register. when I use

      MBeanServer server=MBeanServerFactory.findMBeanServer();
      ObjectName name = new ObjectNam("jboss.singlton:service=Singleton");
      Object obj = server.getAttribute(name, "Client");

      to get an Object(obj), it will not always get the same one that get from SingletonMBean .Because there are two SingletonMBean .
      So now I am confused about what is a singleton.
      I had thought that if a mbean is deployed as singleton ,it has the same instance in all JVMs ,or just the MasterNode JVM has the singleton's instance and other has not the instance of singleton.
      I am confused about what service the singlet mbean supports .and how to use it .
      It is very important to me. my lot of work based on it .
      anticipates anyone's help.
      thank you in advance!!!!

        • 1. Re: how to use singleton
          raz Newbie

          I'm experiencing the same confusion . Did you find an answer for this ?

          • 2. Re: how to use singleton
            James Adams Novice

            My understanding is that the JBoss HASingleton MBeans do in fact behave as real singletons (just one instance per JVM) when you run them on a single node of a cluster. At least they are working well in that capacity for me. I fortunately do not have the problem of the original poster in that my singleton MBeans are only being registered on the master node, which is what I want. Unfortunately I cannot access these singleton MBeans from any other node, and hence my application can't cluster yet. I am trying to use the RMIAdaptor/InvokerAdaptorService, after fruitless attempts with other approaches, but still having no joy.

            If you are considering going with a singleton in your design/architecture, and you want to cluster it, then I would strongly urge you to avoid it if at all possible. I have had no end of frustration with this. I had no choice but to use this approach since I was committed to it by the architect of my project and hence developed a large body of code which depends on it. What little documentation there is on this topic is confusing and incomplete (something I can say about most if not all JBoss documentation from my experiences with it over the last 8 months). If you don't have a support contract then you will have a hard time getting anyone to give you any helpful information on this topic on this forum, as it seems that there are few if any other developers who have tried this with any success. Perhaps you really do get what you pay for, if you're lucky -- but then again I'm not sure that this would be any easier with WebLogic or WebSphere.


            --James

            • 3. Re: how to use singleton
              Ron Burdo Newbie

              Hello,

              What JBoss version do you use? I consider using JBoss singleton as well over JBoss 3.2.5 .

              Thanks

              • 4. Re: how to use singleton
                James Adams Novice

                I'm using version 3.2.5 on Solaris and Linux machines.

                Once I finally have everything worked out I will post a forum message or Wiki entry outlining all of the steps I needed to take to get this working. Hopefully it will help other developers who want to use Singletons in a clustered JBoss application avoid the headaches I've gone through.

                My advice: buy a support contract or perhaps a few hours of consulting if like me you go this route and don't know what you're doing. I imagine that it would be well worth the expense in terms of time saved, if it's within your budget. (Of course if we had that kind of cash available we might not be using a free application server in the first place.)


                --James

                • 5. Re: how to use singleton
                  raz Newbie

                  Have tou read the singleton discussion on TheServerSide.com ?

                  • 6. Re: how to use singleton
                    hu jinquan Newbie

                    I have solve this problem.

                    I had thought that the singleton mbean is like the singleton instance .

                    the truth is ,only the master node run the method (startSinglet()) of singletonmbean , when the the master node turned off, the method(stopSinglet())will be invoke ant then another master node will be choose from the other jboss server ,then the method (start Singleton ()) will be invoke by the master node had just been chosed.

                    so ,just the master node run the startSinglton() service .

                    the service you want it to be singleton can be invoke within startSingleton();

                    • 7. Re: how to use singleton
                      James Adams Novice

                       

                      "lac_raz" wrote:
                      Have tou read the singleton discussion on TheServerSide.com ?


                      Thanks for making me aware of this discussion. I *really* wish I had seen it before going down this path with Singletons. My original idea was to use an EntityBean instead of a Singleton, and from looking at this discussion that looks to be the concensus. Unfortunately as a new employee (and relative newbie to EJBs and JBoss) I didn't have the authority to make the design decision. And now that I've coded my application around Singletons I need to make them work, or somehow convince my manager to let me have the time to change course, which is not likely.

                      I do (finally) have my application working well using Singletons, but it seems that a failure of the master node of my cluster will cause the Singletons to be created fresh on the new server, thereby losing the data needed by the application. Hopefully I can work this out, but really it seems that an Entity Bean approach is a much better idea.


                      --James

                      • 8. Re: how to use singleton
                        Jimmy Wilson Apprentice

                         

                        "lac_raz" wrote:
                        Have tou read the singleton discussion on TheServerSide.com ?


                        "monocongo" wrote:
                        Thanks for making me aware of this discussion.


                        Could either of you provide a link to the aforementioned discussion? Many referenced discussions come up when searching for "singletons" on the ServiceSide site.

                        • 10. Re: how to use singleton
                          James Adams Novice

                           

                          "monocongo" wrote:
                          Once I finally have everything worked out I will post a forum message or Wiki entry outlining all of the steps I needed to take to get this working. Hopefully it will help other developers who want to use Singletons in a clustered JBoss application avoid the headaches I've gone through.


                          This is now available under the title "Clustered HASingleton How-To".


                          --James

                          • 11. Re: how to use singleton
                            xavierpayne2 Newbie

                             

                            Hopefully I can work this out, but really it seems that an Entity Bean approach is a much better idea.


                            I don't know if it's too late to reply to this or not... but you could always go the hybrid route... Which is... Use your singletons the way you are now but anytime a method is called which causes a change in the state of your service you store that state in an entity bean. whenever the service loads it would first check for the existence of a state entity and if it doesn't find one it could truely start fresh. If it does find one it could load the state thats been saved in the entity.

                            For performance reasons you could always keep the state in the service as well. Basically you'd have an HA Singleton with cached reads (no sense getting it from the entity since the singleton service is the only thing modifying it) but you'd want all of your writes updating cache and the entity (so if the server goes down the new singleton can load the state).