4 Replies Latest reply on Aug 9, 2003 3:18 AM by raja05

    Bean class static initialization during deployment

    schwec

      I have a session bean which I am successfully deploying according to log output from the StatelessSessionContainer. One of the bean classes has a static init block which does not get executed during deployment. I was surprised because I thought that loading the class would trigger static initialization.

      The static init block is of the form:
      public class Component implements SessionBean
      {
      ...
      static {
      DataCache.init();
      }

      Just to make sure, I added verbose classloading and during the deployment of the bean I see:
      [Loaded com.my.package.name.ComponentBean]
      [Loaded com.my.package.name.ComponentHome]
      [Loaded com.my.package.name.Component]

      I am wondering why a static init block in Component doesn't run during deployment.

      Could anyone recommend options for a better place to run something in a sessionbean at deployment time.

      Thanks in advance.

        • 1. Re: Bean class static initialization during deployment
          jonlee

          Normally, you would initialise at the instantiation of your EJB, in the EJB lifecycle. This would involve either code in the ejbCreate() method, or invoking a method from ejbCreate().

          e.g.

          public void ejbCreate() throws javax.ejb.CreateException
          {
          doSomething()
          }

          Note that a stateless session bean instance is usually long running - once created, it can be used multiple times. Also, an instance may not be created at deployment. It is created when there is a need for an instance and there are no free instances in the pool.

          I haven't tried with a static initializer, but it would only work when an instance is created. I suppose I have misgivings about such an implementation because it is not wrapped by an EJB exception which may be problematic to signalling to a remote client if something goes wrong.

          Try performing a remote client execution of a stateless session bean method. This should force an EJB instance to be created. You could see if your initialization code works.

          • 2. Re: Bean class static initialization during deployment
            jonlee

            Sorry, my bad. I was thinking standard initialization blocks.

            I think that your problem has to do with container classloading. In any case, there have been long technical discussions on the Sun JDC about this. In theory, you are not supposed to use static initializations in EJBs.

            However, my test should still be valid. I'm not convinced that the initialization is absolutely tied to loading the class although the Fundamentals Vol. I states it in their simple example case. So try it and see.

            • 3. Re: Bean class static initialization during deployment
              jonlee

              Just tried it. The static initialization occurs the first time I make a call to the EJB. It doesn't mean you should do it. It just means you can do it.

              I'm not sure there is any way of forcing something to occur on deployment. I can't remember a container definition that says it must provide a signal when a component is deployed - such that it is standard across all EJB container implementations. Just as there is no stipulation on when a container must initialize an EJB class.

              Someone else might be able to provide some better information on the subject.

              • 4. Re: Bean class static initialization during deployment
                raja05

                I have had a similar situation and the setContext has helped me in this. I put the code that needs to be initialized in this method and it gets invoked when the bean is created.

                I believe the constructor followed by setcontext is called when the bean is initialized.