4 Replies Latest reply on Mar 2, 2011 2:53 PM by piotrekde

    @Service annotated bean lifecycle methods

    piotrekde Newbie

      Hi,

       

      I use org.jboss.ejb3.annotation.Service annotated bean as singleton. I'd like to perform some bean initialization before it will be used by other components (e.g. instantiate some local POJO fields by calling constructors). As it is stated in documentation:

       

      http://docs.jboss.org/ejb3/app-server/reference/build/reference/en/html/jboss_extensions.html

       

      I implemented:

      public void start() throws Exception
      

      and

      void create() throws Exception 
      

      methods, but none of them is called upon @Service bean is created (no entries in logs).

       

      I've read documentation couple of times, and now I am not sure if this methods are also applicable for @Service beans or maybe only for @Managment.

      What are the correct annotations/method signatures to do such @Service bean initialization?

       

      Thanks in advance,

      Piotr

        • 1. @Service annotated bean lifecycle methods
          jaikiran pai Master

          Can you post more info, including the logs, the actual code and a bit about your packaging? Also, which version of JBoss AS is this?

          • 2. Re: @Service annotated bean lifecycle methods
            piotrekde Newbie

            Thanks Jaikiran for the answer. I tried to simplify this issue as most as it is possible. So here is my service bean:

             

            @Service
            public class RemoteTrafficManagerBean implements RemoteTrafficManager {
            
                public void start() throws Exception {
                    System.out.println("Starting service bean");
                    clearStatistics();
                }
            
                public void create() throws Exception {
                    System.out.println("Creating service bean");
                }
            
                (... some other methods ...)
            
                public void someBusinessMethod() {
                 (...)
                }
            }
            

             

            Logs about starting/creating this bean are otputed to the standard out to make sure that it is not logging issue. RemoteTrafficManager is injected into another session bean using @EJB annotation. Also, someBusinessMethod is called from this bean. I am sure that RemoteTrafficManagerBean was instantiated, because some additional logging is flushed into console from its business methods (they do not work appropriately because initialization has not been done).

             

            I 've put logging from all levels into file:

             

            msc@nitrogen:~> cat jboss\ service\ logs |grep 'Starting service bean'

            msc@nitrogen:~> cat jboss\ service\ logs |grep 'Creating service bean'

             

            But there is no entry about starting/creating RemoteTrafficManagerBean. I use JBoss 5.0.0 with HornetQ.

            • 3. Re: @Service annotated bean lifecycle methods
              jaikiran pai Master

              piotrekde wrote:

               

              I use JBoss 5.0.0 with HornetQ.

              Please do not use JBoss AS 5.0.0. Instead use JBoss AS 5.1.0 and then apply our latest EJB3 plugin http://www.jboss.org/ejb3/ejb3plugin.html on top of it.

               

              There have been numerous fixes after JBoss AS 5.0.0 (including changes in this @Service area).

              • 4. @Service annotated bean lifecycle methods
                piotrekde Newbie

                Thanks for answer Jaikiran,

                 

                Unfortunately I cannot switch JBoss version (politics...). Anyway, I've solved this issue by moving initialization code into @Service constructor. Of course it is not possible in all cases (order of injection / initialization), however in my case it works fine.

                 

                Thanks,

                Piotr.