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);
}
}
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
that's hideous. wouldn't it make more sense to expose the correct lifecycle events?
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.
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.
Hi,
I guess the event might be postInitialization instead of postInitialize :)
Vincent
Im trying the example of the Observer but I see no results. My Code is:
@Stateless @Name() public class InitiatorBean implements InitiatorLocal { @Logger private Log log; @Observer() public void initialize(){ log.info(); System.out.println(); } }
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
Vincent - good point! I updated the post.
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.
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.
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.
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"); } }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.
@In(create=true) AsynchronousMailProcessor asynchronousMailProcessor;
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?