Help

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);
   }
}
14 comments:
 
23. Oct 2007, 20:12 CET | Link
Mike

So the injection of the asynchronousMailProcessor bean will postpone the observer execution until after JSF is initialized? Besides the injection I didn't see any other differences. Just want to make sure I read it right.

Cheers

ReplyQuote
 
23. Oct 2007, 20:34 CET | Link
pete23
Schedule a send for 30s time, by which time JSF will be initialized

that's hideous. wouldn't it make more sense to expose the correct lifecycle events?

 
24. Oct 2007, 15:08 CET | Link
Ivan

I wish I knew earlier, spent a lot of time messing with @Startup.. although I've actually managed to get what I wanted with it.

 
25. Oct 2007, 05:17 CET | Link

Mike, yes and notice the function call inside observe().

Pete23 - yes, I agree, that would be nicer. Unfortunately, AFAICS, JSF doesn't provide a hook for this purpose, but I'm going to consider options.

 
29. Oct 2007, 23:59 CET | Link
Vincent Wiencek | vwiencek(AT)gmail.com

Hi,

I guess the event might be postInitialization instead of postInitialize :)

Vincent

 
08. Nov 2007, 01:14 CET | Link
Javier

Im trying the example of the Observer but I see no results. My Code is:

@Stateless @Name(initializator) public class InitiatorBean implements InitiatorLocal { @Logger private Log log; @Observer(org.jboss.seam.postInitialization) public void initialize(){ log.info(INITIALIZATION DONE); System.out.println(INIT---------------------------------------------------------); } }

And the Interface:

@Local public interface InitiatorLocal { void initialize(); }

And I see nothing in the output neither in the log. Can someone help me?

Thanks

 
18. Nov 2007, 03:07 CET | Link

Vincent - good point! I updated the post.

 
07. Feb 2008, 01:52 CET | Link

Does this also work with Seam 1.2.1 GA? My app is running on the 4.2.0.CP01 JBoss stack

I'm having trouble observing this event. I tried @Observe and when i failed to get this to run, i tried to map it in the components.xml as an event instead.

It would be great if I could get this to work, very useful :)

Thanks.

 
07. Feb 2008, 03:30 CET | Link

Looking at the source i see that it is not supported in 1.2.1 GA. But I got it to work by adding the changes that were made in revision 1.168. This now works fine with the @Observer, but not with the components.xml events element way.

 
18. Feb 2008, 18:29 CET | Link
Thierry

Hi

Note: f you are deploying multiple EAR using SEAM, with JBOSS default config, you'll get one postInitialization for each SEAM initialization, because by default in JBoss, in deploy/ear-deployer.xml the Isolated attribute is set to false.

Changing it to true makes the postInitialization be called only once for the EAR. The drawback is changing this setting updates JBoss for all deployments.

 
01. May 2008, 08:21 CET | Link
Zahid Maqbool | zahid.maqbool(AT)gmail.com

Hi my code works fine but If i add

@In JbpmContext

it throws an error saying JbpmContext can only be used ina transaction

Here is my code:

@Name("startupTesting")
public class StartupTesting {

        @Logger
        private Log log;        
        
        @In JbpmContext jbpmContext;


        /**
         * This is called by seam after initialization
         * has finished.
         */
        @Observer("org.jboss.seam.postInitialization")
        public void observe() 
        {
                try
                {
                        log.info("Trying to load Test Jbpm Process Instance");
                        jbpmContext.loadProcessInstance(1l);
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                        log.info("Operation Failed");
                }
                log.info("About to load system properties");

        }
}
 
01. May 2008, 08:31 CET | Link
Zahid Maqbool

Ok I just solved it by adding @Transactional Annotation.

But anyways beleive my guys I really dont know how to thank you people. I mean you guys are wonderful who help out people. Perhaps I should also start actively participate in these kind of discussions. Once again all of you thanks very much for your support.

 
23. Jan 2009, 12:41 CET | Link
Carey Foushee
I had to add the create=true for the AsynchronousMailProcessor for it to work for me.

@In(create=true) AsynchronousMailProcessor asynchronousMailProcessor;


 
21. Aug 2009, 19:23 CET | Link
Shervin Asgari | shervin(AT)asgari.no

Hi Zahid. I have the same problem, but it didn't solve by using @Transactional. I get an error saying I cannot have auto flush. I have tried to put

jbpmContext.getSession().setFlushMode(FlushMode.MANUAL) but I still get the same exception. I have even tried removing @In JbpmContext and using jbpmConfiguration.getInstance().createJbpmContext(); but that doesnt work either.

How did you do it?

Post Comment