One of the popular features in the Seam 2 "container" is startup components (which would be startup beans in CDI terminology). I'm trying to determine the best way to approach this feature and where it should live (i.e., Weld X and/or Seam 3, hence the x-post).<div>
<br></div><div>While it's true that EJB 3.1 supports startup beans, it unnecessarily links them to:<div><br></div><div>- enterprise beans (my, where have we heard this before? cough transactions cough)</div><div>- singletons</div>
<div><br></div><div>Seam 2 supports startup components that are instantiated (as in @PostConstruct gets invoked) when the scope to which the component is bound is activated. Supported scopes include application and session. I don't see any reason why we can't support all (or most) scopes in Weld X/Seam 3.</div>
<div><br></div><div>At first glance, you might think about initializing @ApplicationScoped @Startup beans in an AfterDeploymentValidation observer (the application-scope is active at that point) [1] (also see note). That's certainly one way to go, though perhaps jumping the gun as some other extensions may still report a deployment error. It also doesn't address the remaining scopes.</div>
<div><br></div><div>The better place to do eager initialization is perhaps the Seam 3 Servlet module. This module bridges the Servlet context events to the CDI event bus, providing the opportunity to initialize components bound to the relevant scopes. However, my concern is that if those events fire before the CDI implementation has started the scope, it's going to result in a ContextNotActiveException (Nik, would you be able to provide insight into whether this ordering issue has been address?).</div>
<div><br></div><div>I'd also like to entertain the idea of having a transient startup scope (@ApplicationStartupScoped?). Most of the time developers employ a application-scoped startup bean, it's doing something like seeding a database, creating directories or some other routine that's a one time deal. It seems like a waste to have these beans hang around long after their job is done. In fact, I anticipate it becoming the most commonly-used scope for startup beans.</div>
<div><br></div><div>After considering the options, it seems to me that long-term, this feature would be more robust if it were part of the CDI specification. (Even the JSF managed bean container supports eager bean initialization for application-scoped beans). Or perhaps I'm missing a very straightforward way to address it.</div>
<div><br></div><div>I'm interested in hearing your suggestions. After I get feedback about which project/module should tackle this problem, I'll create a JIRA for it.</div><div><div><div><br></div><div>-Dan</div><div>
<br></div><div>[1] <a href="http://gist.github.com/635719">http://gist.github.com/635719</a></div><div><br></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>Note: There's another issue. BeanManager#getReference() may only return a proxy and not instantiate the bean, meaning @PostConstruct is not yet called. In the linked gist, I worked around this problem by invoking toString() on the reference.</div>
</div><div><br></div><div>I'll mention that Resin has addressed @Startup support for managed beans: <a href="http://caucho.com/resin-4.0/examples/ioc-binding/viewfile?file=WEB-INF/classes/example/MyStartupBean.java">http://caucho.com/resin-4.0/examples/ioc-binding/viewfile?file=WEB-INF/classes/example/MyStartupBean.java</a></div>
<div><br>-- <br>Dan Allen<br>Principal Software Engineer, Red Hat | Author of Seam in Action<br>Registered Linux User #231597<br><br><a href="http://mojavelinux.com" target="_blank">http://mojavelinux.com</a><br><a href="http://mojavelinux.com/seaminaction" target="_blank">http://mojavelinux.com/seaminaction</a><br>
<a href="http://www.google.com/profiles/dan.j.allen" target="_blank">http://www.google.com/profiles/dan.j.allen</a><br>
</div></div></div></div>