1 Reply Latest reply: Apr 3, 2012 2:40 AM by Steve Button-Oracle RSS

    EJB 3 Singleton Bean on weblogic 11

    706742
      App server weblogic 12c.
      In my project structre an ear and it includes ..ejb.jar and other jars.
      in ejb.jar, i have ejb beans and also
      a singleton Bean like

      @Startup
      @Singleton(mappedName="InitializeProject")
      public class InitializeProject implements InitializeProjectInterface{

      @PostConstruct
      public void mainss(){....}
      }
      whenever app server start or deploy this ear project, i want to enter mainss method.
      Re: Singleton EJBs in Weblogic
      above post ok but i dont want solve like that . is not good way.
      and why weblogic doesnt support @singleton bean .
      Pls, how can run init method on startup?
        • 1. Re: EJB 3 Singleton Bean on weblogic 11
          Steve Button-Oracle
          The Singleton EJB should be working fully in WLS 12c which is Java EE 6 compatible.

          It is not available on WebLogic 11, which your subject refers to ...

          It's a requirement of the Java EE 6 combined set of specifications that methods annotated with @PostConstruct be called immediately after the constructor has finished, and that beans annotated with @Singleton and @Startup are started when the server enters the ready state. Thus your mainss() method should be called.

          To verify this, I just created a very simple test that verifies it is working.

          The app is here if you want to try it yourself:

          http://dl.dropbox.com/u/15657493/test-ejb-startup.war

          -steve-
          /**
           *
           * @author sbutton
           */
          @Singleton
          @Startup
          public class TestSingletonBean {
              
              private String SOMEVAL = "IF YOU SEE THIS THE INIT METHOD HAS NOT BEEN CALLED";
              private ArrayList<String>initcalls = new ArrayList<String>();
              
              @PostConstruct
              public void init() {
                  Date now = new Date();
                  initcalls.add(now.toString());
                  System.out.printf("\n\n%s Entered method: %s\n\n", 
                          now.toString(), 
                          Thread.currentThread().getStackTrace()[1].toString());
                  SOMEVAL = "@PostConstruct has been called on " + now.toString();
              }
              
              public String whoAmI() {
                  return String.format("You called %s, init called on %s, SOMEVAL=\"%s\"", 
                          Thread.currentThread().getStackTrace()[1].toString(),
                          Arrays.toString(initcalls.toArray()),
                          SOMEVAL);
              }
          }
          Which spits out the following message when the server is started:
          Tue Apr 03 17:02:23 CST 2012 Entered method: sab.demo.TestSingletonBean.init(TestSingletonBean.java:30)
          Calling it from a servlet, the output from the whoAmI method shows:
          You called sab.demo.TestSingletonBean.whoAmI(TestSingletonBean.java:37), init called on [Tue Apr 03 17:02:23 CST 2012], SOMEVAL="@PostConstruct has been called on Tue Apr 03 17:02:23 CST 2012"
          If you continue to have problems then please post an update or open a support tar.

          -steve-