-
1. Re: Equivalent of Seam @Startup Annotation in CDI or Weld
nimo22 Sep 8, 2010 5:04 AM (in response to ryokota)Use
@PostConstruct public void init(){ ..}
for beans (in your case your applicationscoped-bean).
-
2. Re: Equivalent of Seam @Startup Annotation in CDI or Weld
fortega Apr 15, 2011 3:53 PM (in response to ryokota)You can observe an event which is triggered when the application is initialized:
public class BeanToBeInitializedAtStartup {
public void onStartup(@Observes @Initialized WebApplication webApplication){
//do nothing, bean will be initialized when application is started
}
}If you have multiple beans which need to be started up: I wrote some code which makes it a lot cleaner: http://blog.fortega.be/2011/04/seam-3-weld-alternative-for-startup.html
-
3. Re: Equivalent of Seam @Startup Annotation in CDI or Weld
yangju.richard.yang.pearson.com May 3, 2011 3:04 PM (in response to ryokota)I am trying to follow your code. But I could not find @Initialized where is it?
Thanks.
-
4. Re: Equivalent of Seam @Startup Annotation in CDI or Weld
yangju.richard.yang.pearson.com May 3, 2011 3:38 PM (in response to ryokota)Note I am not using seam face module. What are other options? Do we have something like ApplicationInitialized event? I deploy ear.
-
5. Re: Equivalent of Seam @Startup Annotation in CDI or Weld
yangju.richard.yang.pearson.com May 3, 2011 4:38 PM (in response to ryokota)OK, I added seam servlet 3.0.0.Final. But the onStartup never got called even I followed your example literally.
-
6. Re: Equivalent of Seam @Startup Annotation in CDI or Weld
marx3 May 4, 2011 10:01 AM (in response to ryokota)Observing startup events is unreliable, some of them are fired, and the others not (try PostConstructApplicationEvent, AfterDeploymentValidation, AfterBeanDiscovery). And they are fired in the middle of application startup, so for example it's random if injection will work.
The most reliable method is to use:@Startup @Singleton public class StartupService { @PostConstruct public void onStartup() { (...)
In such a bean you can use injection because it's created at the very end of startup.
It's not most elegant way because in fact above bean is Application Scoped bean, while for startup work it should be
run and forget
scenario - no need to keep such a bean forever in memory. So if there is more classes which need to be run at startup, the best is to fire your own event in above bean to emulate event model. -
7. Re: Equivalent of Seam @Startup Annotation in CDI or Weld
yangju.richard.yang.pearson.com May 4, 2011 11:03 AM (in response to ryokota)I got your code working by moving the StatupHelper class to the war from a jar in the ear. The Observer is invoked. I also had to turn off the jboss war deployment isolation. It is weird that the event cannot be propagated to the ear from the war, even the war is inside the ear.
We are currently facing two major issues with weld 1.1.
First, the Bean sometimes cannot be started even we marked it as
@Singleton
@Startup
It did start most of the time, I would say 8 out of 10 times (with nothing changed.Second problem, with jboss AS6 Final, weld 1.1 version, it sometimes (intermittent) does not inject all the dependencies, if we use @EJB to inject a session bean. This failure occurs more often if there more dependencies to inject. If you only have one or two ejbs to inject, it rarely fails to inject.
It is very frustrating because weld seems behave randomly and we could not find any useful info about this behavior.
Anyway, by using your example, at least we can fix the first problem. I also see there is another solution for this startup problem:
https://gist.github.com/635719I tried but could not get it totally working.
If you have more info, please share with me.
Thanks.
-
8. Re: Equivalent of Seam @Startup Annotation in CDI or Weld
marx3 May 5, 2011 1:54 AM (in response to ryokota)Currently ears are totally screwed up, it has serious bug which make such packaging totally unusable
-
9. Re: Equivalent of Seam @Startup Annotation in CDI or Weld
jharting May 9, 2011 7:44 AM (in response to ryokota)The ear problem can be worked around by configuring deployment isolation (as Richard mentioned). Follow the instructions in the JIRA issue
Regarding the dependencies not being injected randomly, perhaps you are experiencing JBAS-8896.