3 Replies Latest reply on Apr 25, 2013 4:47 AM by nmay

    how-to create Singleton instance at deployment time

    nmay

      Hi,

      Im trying to migrate an old application with a simple singleton class:

       

      public class MySingleton  {

       

       

        protected static MySingleton instance;

       

          private MySingleton() {

              System.out.println("############## MySingleton created !! ##########");

          }

       

       

          public static synchronized MySingleton   instance() {

              if (instance == null) {

                  instance = new MySingleton();

                  System.out.println("######### MySingleton instance: " + instance);

              } else {

                  System.out.println("######### MySingleton already created return instance: " + instance);

              }

              return instance;

          }

      }

       

      And I'm trying to initialize it in my  ear application during deployment time.

       

      But finally I  have two instances during deployment:

       

      19:51:14,389 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 16) JBAS011402: Starting Persistence Unit Service 'ear-1.0.ear#primary'

      19:51:14,587 INFO  [stdout] (MSC service thread 1-10) ################# Service01.start()

      19:51:14,588 INFO  [stdout] (MSC service thread 1-10) ############## MySingleton created !! ##########

      19:51:14,589 INFO  [stdout] (MSC service thread 1-10) ######### MySingleton instance: biz.j2ee.MySingleton@59426318

      19:51:14,591 INFO  [stdout] (MSC service thread 1-8) ############### Service02.start()  ##################

      19:51:14,592 INFO  [stdout] (MSC service thread 1-8) ############## MySingleton created !! ##########

      19:51:14,593 INFO  [stdout] (MSC service thread 1-8) ######### MySingleton instance: biz.j2ee.MySingleton@1cdd287c

      19:51:14,594 INFO  [stdout] (MSC service thread 1-8) ############### Service02 started & registered in jmx

       

      the same code working well in JBoss 7.1.1 so singleton created only ones:

       

      19:49:51,526 INFO  [org.jboss.as.jpa] (MSC service thread 1-13) JBAS011402: Starting Persistence Unit Service 'ear-1.0.ear#primary'

      19:49:51,528 INFO  [stdout] (MSC service thread 1-3) ################# Service01.start()

      19:49:51,528 INFO  [org.hibernate.ejb.Ejb3Configuration] (MSC service thread 1-13) HHH000204: Processing PersistenceUnitInfo [

                name: primary

                ...]

      19:49:51,529 INFO  [stdout] (MSC service thread 1-3) ############## MySingleton created !! ##########

      19:49:51,531 INFO  [stdout] (MSC service thread 1-3) ######### MySingleton instance: biz.j2ee.MySingleton@29ad8398

      19:49:51,533 INFO  [stdout] (MSC service thread 1-10) ############### Service02.start()  ##################

      19:49:51,534 INFO  [stdout] (MSC service thread 1-10) ######### MySingleton already created return instance: biz.j2ee.MySingleton@29ad8398

      9:49:51,535 INFO  [stdout] (MSC service thread 1-10) ############### Service02 started & registered in jmx

       

       

      I know that we have @Singleton annotation, but I am trying to port legacy code so I need  a singleton which I can share between all deployment units

      and it should be initialized at deployment time.

      It seems like EAP use different deployment threads for each sub-deployment unit.

       

      How  to write a real singleton in EAP ???