11 Replies Latest reply on Feb 28, 2008 9:00 PM by monkeyden

    something like @Startup

    marx3

      I want to extend database schema on application startup. Those changes are specific for database thus not supported by Hibernate. So i plan to execute sql script via JDBC.
      In Seam there are such scripts, but they need to set create/drop in persistence.xml, which is not good idea.


      So the question is: how to execute some code on application startup? Is @startup annotation good for it? As it's bean annotation, which method will be executed? Should I use @PostConstruct annotated method or sth. else?

        • 1. Re: something like @Startup
          damianharvey.damianharvey.gmail.com

          If you combine a class with @Startup and a method with @Create you'll get a method executing at startup. Sounds like what you're after.


          Cheers,


          Damian.

          • 2. Re: something like @Startup
            nickarls

            If you plan to use seam components then placing an observer on org.jboss.seam.postInitialization might be a good idea. If you pick the connection directly from JNDI then an application scoped bean with @Startup should do...

            • 3. Re: something like @Startup
              marx3

              What type of component should I use?
              Seam documentation says: the @Startup annotation, which may be applied to any application or session scoped component.
              But I don't need this component later, so declaring it as application or session component isn't good idea.
              Maybe I should simply declare servlet with load-on-startup=1 ?

              • 4. Re: something like @Startup
                nickarls

                Well, one app scoped bean isn't usually that harmful to have around (especially since your wouldn't keep that many resources).
                If you want to get rid of it, you could probably have it remove itself from the context once its done...

                • 5. Re: something like @Startup
                  marx3

                  I made such code, but during startup runSQL() method isn't executed


                  @Stateful
                  @Scope(ScopeType.APPLICATION)
                  @Startup
                  public class UpdaterManagerBean implements UpdaterManager {
                       @Logger
                       private Log log;
                  
                       @Remove
                       @Destroy
                       public void destroy() {
                       }
                  
                       @Create
                       @Override
                       public void runSQL(){
                            log.info("###########################################");
                       }
                  }


                  • 6. Re: something like @Startup
                    taccart.thierry.accart.name

                    I'm not sure, but maybe should you have a look at this post from Pete Muir:


                    Doing something at startup with Seam (...)
                    The easiest way is to observe the org.jboss.seam.postInitialization event (which occurs right after Seam finishes initializing):
                    Read more on  http://in.relation.to


                    • 7. Re: something like @Startup
                      nickarls

                      Keeping it simple, try


                      
                      @Name("bootstrapper")
                      
                      public class UpdateManagerPOJO {
                      
                        @Logger
                      
                        private Log log;
                      
                      
                        @Observer("org.jboss.seam.postInitialization")
                      
                        public void runSQL() {
                      
                          log.info("dropping db");
                      
                        }
                      
                      
                      }
                      
                      


                      • 8. Re: something like @Startup
                        marx3

                        ok, observer did the trick, thank you all :)

                        • 9. Re: something like @Startup
                          jeff87

                          I too need to do something similar to this but am having trouble. This works:


                          @Name("startupObserver")
                          public class StartupObserver {
                          
                               @Logger
                               private Log log;     
                               
                          
                          
                               /**
                                * This is called by seam after initialization
                                * has finished.
                                */
                               @Observer("org.jboss.seam.postInitialization")
                               public void observe() {
                                    log.info("About to load system properties");
                          
                               }
                          
                          
                          }
                          



                          but this fails and using @In instead of @PersistenceContext fails too:


                          @Name("startupObserver")
                          public class StartupObserver {
                          
                               @Logger
                               private Log log;     
                               
                               @PersistenceContext
                               private EntityManager entityManager;
                          
                               /**
                                * This is called by seam after initialization
                                * has finished.
                                */
                               @Observer("org.jboss.seam.postInitialization")
                               public void observe() {
                                    log.info("About to load system properties");
                                    
                               }
                          
                          }
                          


                          with the message:
                          Exception sending context initialized event to listener instance of class org.jboss.seam.servlet.SeamListener
                          java.lang.RuntimeException: Could not create Component: startupObserver


                          If the app is initialized, shouldn't the entityManager be available? I'm using Seam 2.0.1 if that matters.

                          • 10. Re: something like @Startup
                            jeff87

                            Nevermind, throwing in a @Stateless worked.

                            • 11. Re: something like @Startup
                              monkeyden

                              Correct me if I'm wrong but, if you don't want it to be an EJB, you can use:


                              @Startup(depends={"entityManager"})