5 Replies Latest reply on Jan 26, 2011 7:51 AM by Carlo de Wolf

    Singleton in clustered JBoss AS 6 Final

    Dan Osterrath Newbie

      Hello to everyone,


      I have a singleton session bean that handles some nightly database cleanup and import. This is done via @Singleton and @Schedule annotation. Unfortunately when deploying in a cluster this bean is executed on all nodes.


      package session;
      import java.net.InetAddress;
      import java.net.UnknownHostException;
      import java.util.Date;
      import javax.ejb.Schedule;
      import javax.ejb.Singleton;
      import org.jboss.ejb3.annotation.Clustered;
      public class MyScheduler {
          @Schedule(second = "*/20", minute = "*", hour = "*", persistent = false)
          public void printTime() {
              System.out.println(getHostName() + ": " + new Date());
          private String getHostName() {
              try {
                  InetAddress addr = InetAddress.getLocalHost();
                  return addr.getHostName();
              } catch (UnknownHostException e) {
              return "";

      This example prints the hostname and the current time on all nodes.


      That behaviour seems to fit the EJB 3.1 requirements but is quite useless for me (and probably others). So how can I implement an high available singleton in a cluster? If there is no way how can I block the other nodes singleton instances so that only one gets executed?

        • 1. Singleton in clustered JBoss AS 6 Final
          Ilya Sorokoumov Master

          I guess you can try this:

          As all the data in a singleton should be the same in all nodes. You can just use some flag inside you singleton and if this flag shows that action has been already executed today you can simply skip it on the other nodes.


          Does it work for you?

          • 2. Re: Singleton in clustered JBoss AS 6 Final
            Dan Osterrath Newbie

            Well, the flag had to be set before executing the action. Unfortunatelly checking and setting the flag can not be atomic over all nodes. So this might lead to a race condition that multiple instances enter the "synchronized" area. (Which in fact is not synchronized.)


            if (flag) {
               // <--- here the race condition might occur
               flag = true;
               flag = false;
            • 3. Re: Singleton in clustered JBoss AS 6 Final
              Ilya Sorokoumov Master
              Managing Concurrent Access in a Singleton Session Bean

              Singleton session beans are designed for concurrent access, situations in which many clients need to access a single instance of a session bean at the same time. A singleton’s client needs only a reference to a singleton in order to invoke any business methods exposed by the singleton and doesn’t need to worry about any other clients that may be simultaneously invoking business methods on the same singleton.

              When creating a singleton session bean, concurrent access to the singleton’s business methods can be controlled in two ways: container-managed concurrency and bean-managed concurrency.

              The javax.ejb.ConcurrencyManagement annotation is used to specify container-managed or bean-managed concurrency for the singleton. With @ConcurrencyManagement, a type attribute must be set to eitherjavax.ejb.ConcurrencyManagementType.CONTAINER or javax.ejb.ConcurrencyManagementType.BEAN. If no @ConcurrencyManagement annotation is present on the singleton implementation class, the EJB container default of container-managed concurrency is used.



              Take a look at this link for the details:


              • 4. Re: Singleton in clustered JBoss AS 6 Final
                Radim Hanus Expert

                the combination of both @Singleton and @Clustered doesn't make up a clustered singleton bean


                you need to set up a singleton service here is some backround for jboss-5.x should work in 6.0 as well

                the simplest way is to package bean above into a separate archive and deploy it into deploy-hasingleton or use HASingletonControler and implement custom jboss service

                • 5. Re: Singleton in clustered JBoss AS 6 Final
                  Carlo de Wolf Master

                  Keep in mind that state is not replicated across the cluster using these methods. Also to enable scaling there should really be a singleton instance available on each node.


                  I've created EJBTHREE-2234 as a placeholder for future reference.