Doing something at startup with Seam

Posted by    |       Seam

One of the most common forum questions is How do I make Seam call an action method at startup?.

The easiest way is to observe the org.jboss.seam.postInitialization event (which occurs right after Seam finishes initializing):

@Name("observeInitialization")
public class ObserveInitialization {

   @In EntityManager entityManager;

   @Observer("org.jboss.seam.postInitialization")
   public void observe() {
      // Do your initialization here
   }
}

However, as this event occurs /before/ JSF finishes initializing, if you want to use Seam Mail at startup, you should schedule it to happen a short time after initialization:

@Name("asynchronousMailProcessor")
public class AsynchronousMailProcessor {

   @Asynchronous
   public void sendMail(@Duration long duration) {
      Renderer.instance().render("/mail.xhtml");
   }
}
@Name("observeInitialization")
public class ObserveInitialization {

   @In AsynchronousMailProcessor asynchronousMailProcessor;

   @Observer("org.jboss.seam.postInitialization")
   public void observe() {
      // Schedule a send for 30s time, by which time JSF will be initialized
      asynchronousMailProcessor.sendMail(30 * 1000l);
   }
}

Back to top