8 Replies Latest reply on Oct 21, 2011 8:31 AM by b4nd0ler0

    Invocation of startSingleton()

      Hi,

      I have read a lot of accessible documentation regarding HAsingletons, however, I probably still do not understand one basic thing (when the startSingleton() method is invoked).

      I tried to do the HASingleton example shown in chapter 11.2 of:
      http://www.jboss.org/file-access/default/members/jbossas/freezone/docs/Clustering_Guide/5/pdf/Clustering_Guide.pdf

      I thought the startSingleton() method is supposed to be invoked automatically when the application is deployed. Is this correct? In my case it is not so.

      Or am I supposed to call it from the JMX console?

      Thanks for an answer for such a basic question.

      Adam

        • 1. Re: Invocation of startSingleton()
          brian.stansberry

          It should be invoked automatically when the application is deployed *on the master node*, which typically would be the first node started in the cluster.

          You should never have to call it from the JMX console.

          • 2. Re: Invocation of startSingleton()

            Thanks for quick response.

            Well, then I suppose I must have some mistake in my configuration files.

            Info:
            Version: JBOSS 5.0.0 GA

            My singleton class:

            public class StartupService2 implements StartupService2MBean {
            
             Logger log = Logger.getLogger(StartupService2.class);
             private boolean isMasterNode;
            
             public String getName() {
             return "com.xitee.cfd.be:service=StartupService";
             }
            
             public boolean isMasterNode() {
             return isMasterNode;
             }
            
             public void startSingleton() {
             isMasterNode = true;
             log.info("starting testing-singleton");
             }
            
             public void stopSingleton() {
             isMasterNode = false;
             log.info("stopping testing-singleton");
             }
            }



            My MBean interface:
            public interface StartupService2MBean {
            
             public void startSingleton();
            
             public void stopSingleton();
            
             public boolean isMasterNode();
            }


            My jboss-service.xml:
            <server>
             <mbean code="com.xitee.cfd.be2.StartupService2"
             name="StartupTestingCFD2">
             </mbean>
            
             <mbean code="org.jboss.ha.singleton.HASingletonController"
             name="HASingletonController">
             <depends optional-attribute-name="ClusterPartition"
             proxy-type="attribute">jboss:service=${jboss.partition.name:DefaultPartition}</depends>
            
             <attribute name="TargetName">StartupTestingCFD2</attribute>
             <attribute name="TargetStartMethod">startSingleton</attribute>
             <attribute name="TargetStopMethod">stopSingleton</attribute>
             </mbean>
            
            </server>


            After deploying the application (into deploy directory) I would expect "starting testing-singleton" string to appear in the server.log file, however this does not happen.

            Could you please give me any startup hint, how to solve this? Do you need some more information from my side?

            Adam

            • 3. Re: Invocation of startSingleton()
              brian.stansberry

              Hmm, simple question, complicated answer from me.

              First, I'll cut to the bottom line. Instead of deploying via a -service.xml, use a -jboss-beans.xml:

              <?xml version="1.0" encoding="UTF-8"?>
              
              <deployment xmlns="urn:jboss:bean-deployer:2.0">
              
               <bean name="StartupTestingCFD2" class="com.xitee.cfd.be2.StartupService2">
              
               <annotation>@org.jboss.aop.microcontainer.aspects.jmx.JMX(name="test:service=StartupTestingCFD2", exposedInterface=org.jboss.ha.singleton.examples.HASingletonMBeanExampleMBean.class, registerDirectly=true)</annotation>
              
               </bean>
              
               <bean name="HASingletonController"
               class="org.jboss.ha.singleton.HASingletonController">
              
               <annotation>@org.jboss.aop.microcontainer.aspects.jmx.JMX(name="test:service=HASingletonController", exposedInterface=org.jboss.ha.singleton.HASingletonControllerMBean.class, registerDirectly=true)</annotation>
              
              
               <property name="HAPartition"><inject bean="HAPartition"/></property>
               <property name="target"><inject bean="StartupTestingCFD2"/></property>
               <property name="targetStartMethod">startSingleton</property>
               <property name="targetStopMethod">stopSingleton</property>
              
               </bean>
              
              </deployment>
              


              Now, the details.

              First, deploying via a -service.xml should work. But in actually deploying an example to test it before posting here I found a bug: https://jira.jboss.org/jira/browse/JBAS-6435 . Hopefully that will be fixed for 5.0.1 which is due out soon.

              Now, let's assume JBAS-6435 were fixed and you could use a -service.xml. It would look like this:

              <server>
              
               <mbean code="com.xitee.cfd.be2.StartupService2"
               name="test:service=StartupTestingCFD2">
               </mbean>
              
               <mbean code="org.jboss.ha.singleton.HASingletonController"
               name="test:service=HASingletonController">
              
               <attribute name="HAPartition"><inject bean="HAPartition"/></attribute>
              
               <attribute name="TargetName">test:service=StartupTestingCFD22</attribute>
               <attribute name="TargetStartMethod">startSingleton</attribute>
               <attribute name="TargetStopMethod">stopSingleton</attribute>
               </mbean>
              
              </server>


              There were two problems with your XML:

              1) The values for the "name" attribute on your mbean elements were invalid. The value needs to be a legal JMX ObjectName.

              2) I need to update the AS 5 docs need to be to specify how to dependency inject the HAPartition, i.e.

              <attribute name="HAPartition"><inject bean="HAPartition"/></attribute>


              instead of

              <depends optional-attribute-name="ClusterPartition"
               proxy-type="attribute">jboss:service=${jboss.partition.name:DefaultPartition}</depends>


              • 4. Re: Invocation of startSingleton()

                Excellent, thank you very much Brian, it works:) If there's something I can do for you in return regarding this issue let me know.

                Adam

                • 5. Re: Invocation of startSingleton()
                  ablevine1

                  I am using 5.01 and I can get a singleton MBean to deploy using the -service.xml configuration you describe above.
                  I need to be able to store state in my MBean so I changed my MBean class to extend from org.jboss.ha.singleton.HASingletonSupport, but then I get the following error when deploying

                  2009-05-19 18:25:52,611 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController] (main) Error installing to Real: name=vfszip:/usr/local/jboss-5.0.1.GA/server/batch/deploy/batch.sar/ state=PreReal mode=Manual requiredState=
                  Real
                  org.jboss.deployers.spi.DeploymentException: Error deploying: squaretrade:service=Semaphore
                   at org.jboss.deployers.spi.DeploymentException.rethrowAsDeploymentException(DeploymentException.java:49)
                   at org.jboss.system.deployers.ServiceDeployer.deploy(ServiceDeployer.java:118)
                   at org.jboss.system.deployers.ServiceDeployer.deploy(ServiceDeployer.java:46)
                   at org.jboss.deployers.spi.deployer.helpers.AbstractSimpleRealDeployer.internalDeploy(AbstractSimpleRealDeployer.java:62)
                   at org.jboss.deployers.spi.deployer.helpers.AbstractRealDeployer.deploy(AbstractRealDeployer.java:50)
                   at org.jboss.deployers.plugins.deployers.DeployerWrapper.deploy(DeployerWrapper.java:171)
                   at org.jboss.deployers.plugins.deployers.DeployersImpl.doDeploy(DeployersImpl.java:1439)
                   at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1157)
                   at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1178)
                   at org.jboss.deployers.plugins.deployers.DeployersImpl.install(DeployersImpl.java:1098)
                   at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:348)
                   at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:1598)
                   at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:934)
                   at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1062)
                   at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:984)
                   at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:822)
                   at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:553)
                   at org.jboss.deployers.plugins.deployers.DeployersImpl.process(DeployersImpl.java:781)
                   at org.jboss.deployers.plugins.main.MainDeployerImpl.process(MainDeployerImpl.java:698)
                   at org.jboss.system.server.profileservice.ProfileServiceBootstrap.loadProfile(ProfileServiceBootstrap.java:304)
                   at org.jboss.system.server.profileservice.ProfileServiceBootstrap.start(ProfileServiceBootstrap.java:205)
                   at org.jboss.bootstrap.AbstractServerImpl.start(AbstractServerImpl.java:405)
                   at org.jboss.Main.boot(Main.java:209)
                   at org.jboss.Main$1.run(Main.java:547)
                   at java.lang.Thread.run(Thread.java:619)
                  Caused by: java.lang.IllegalStateException: HAPartition property must be set before starting HAServiceMBeanSupport
                   at org.jboss.ha.jmx.AbstractHAServiceMBeanSupport.startService(AbstractHAServiceMBeanSupport.java:175)
                   at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:376)
                   at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:322)
                   at sun.reflect.GeneratedMethodAccessor107.invoke(Unknown Source)
                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                   at java.lang.reflect.Method.invoke(Method.java:597)
                   at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:157)
                   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:96)
                   at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
                   at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
                   at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:668)
                   at org.jboss.system.microcontainer.ServiceProxy.invoke(ServiceProxy.java:189)
                   at $Proxy36.start(Unknown Source)
                   at org.jboss.system.microcontainer.StartStopLifecycleAction.installAction(StartStopLifecycleAction.java:42)
                   at org.jboss.system.microcontainer.StartStopLifecycleAction.installAction(StartStopLifecycleAction.java:37)
                   at org.jboss.dependency.plugins.action.SimpleControllerContextAction.simpleInstallAction(SimpleControllerContextAction.java:62)
                   at org.jboss.dependency.plugins.action.AccessControllerContextAction.install(AccessControllerContextAction.java:71)
                   at org.jboss.dependency.plugins.AbstractControllerContextActions.install(AbstractControllerContextActions.java:51)
                   at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:348)
                   at org.jboss.system.microcontainer.ServiceControllerContext.install(ServiceControllerContext.java:286)
                   at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:1598)
                   at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:934)
                   at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1062)
                   at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:984)
                   at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:822)
                   at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:553)
                   at org.jboss.system.ServiceController.doChange(ServiceController.java:688)
                   at org.jboss.system.ServiceController.start(ServiceController.java:460)
                   at org.jboss.system.deployers.ServiceDeployer.start(ServiceDeployer.java:163)
                   at org.jboss.system.deployers.ServiceDeployer.deploy(ServiceDeployer.java:99)
                   ... 23 more
                  


                  The reason I am extending org.jboss.ha.singleton.HASingletonSupport is to be able to access the setDistributedState and getDistributedState methods. Is there some better/newer way to achieve this functionality that will not fail deployment?

                  • 6. Re: Invocation of startSingleton()
                    ablevine1

                    After further investigation it looks like the HAPartition is never being set. If I override the method in setHAPartiton in org.jboss.ha.jmx.AbstractHAServiceMBeanSupport like so:

                     public void setHAPartition(HAPartition partition)
                     {
                     log.info("GOT PARTITION: " + partition);
                     super.setHAPartition(partition);
                     }
                    


                    I never see my "GOT PARTITION" statement appear in the log.
                    Since the partition is not being set org.jboss.ha.jmx.AbstractHAServiceMBeanSupport.startService fails because it throws an exception if the HAPArtition has not been set.
                    Is there something I can add to my config to make sure that the HAPartition is set?

                    Here is my config:
                    <server>
                    
                     <!-- This MBean is an example of a clustered singleton -->
                     <mbean code="com.squaretrade.semaphore.SemaphoreService"
                     name="squaretrade:service=Semaphore">
                     </mbean>
                    
                     <!-- This HASingletonController manages the cluster Singleton -->
                     <mbean code="org.jboss.ha.singleton.HASingletonController"
                     name="squaretrade:service=SemaphoreServiceProxy">
                     <attribute name="HAPartition"><inject bean="HAPartition"/></attribute>
                     <attribute name="TargetName">squaretrade:service=Semaphore</attribute>
                     <!-- Methods to invoke when become master / stop being master -->
                     <attribute name="TargetStartMethod">startSingleton</attribute>
                     <attribute name="TargetStopMethod">stopSingleton</attribute>
                     </mbean>
                    
                    </server>
                    

                    I tried adding
                     <depends>HAPartition</depends>
                    

                    to the controller but that did not seem to make any difference

                    • 7. Re: Invocation of startSingleton()

                      Seems to be corrected in 5.1.0GA (works with

                      <attribute name="HAPartition"><inject bean="HAPartition"/></attribute>)


                      • 8. Re: Invocation of startSingleton()
                        b4nd0ler0

                        Mate, you're a star, this bastardo problem had me going around in circles for a while, mainly due to the abundant obsolete documentation on JBoss 5 out there.

                         

                        Gracias y adios.