<font size=3>We have a user scenario where we wishes to replace @Singleton
@Startup EJB beans with CDI beans. This was attempted by observing the
ApplicationScoped initialization event to trigger the replacement code.<br>
<br>
However, the event was fired only after the service invocation, which was
too late. Apparently OWB fires this event much earlier. </font>
<br><font size=3>The CDI spec states:</font>
<br>
<br><font size=2 face="Arial">The <i>application context </i>is provided
by a built-in context object for the built-in scope type</font>
<br><tt><font size=1>@ApplicationScoped</font></tt><font size=2 face="Arial">.
The application scope is active:</font>
<br><font size=2 face="Arial">• during the </font><tt><font size=1>service()
</font></tt><font size=2 face="Arial">method of any servlet in the web
application, during the </font><tt><font size=1>doFilter()</font></tt>
<br><font size=2 face="Arial">method of any servlet filter and when the
container calls any </font><tt><font size=1>ServletContextListener</font></tt><font size=2 face="Arial">,</font>
<br><tt><font size=1>HttpSessionListener</font></tt><font size=2 face="Arial">,
</font><tt><font size=1>AsyncListener </font></tt><font size=2 face="Arial">or
</font><tt><font size=1>ServletRequestListener</font></tt><font size=2 face="Arial">,</font>
<br><font size=2 face="Arial">• during any Java EE web service invocation,</font>
<br><font size=2 face="Arial">• during any remote method invocation of
any EJB, during any asynchronous method invocation</font>
<br><font size=2 face="Arial">of any EJB, during any call to an EJB timeout
method and during message delivery to any EJB</font>
<br><font size=2 face="Arial">message-driven bean,</font>
<br><font size=2 face="Arial">• when the disposer method or </font><tt><font size=1>@PreDestroy
</font></tt><font size=2 face="Arial">callback of any bean with any normal
scope other</font>
<br><font size=2 face="Arial">than </font><tt><font size=1>@ApplicationScoped
</font></tt><font size=2 face="Arial">is called, and</font>
<br><font size=2 face="Arial"><b>• during </b></font><tt><font size=1><b>@PostConstruct
</b></font></tt><font size=2 face="Arial"><b>callback of any bean.</b></font>
<br>
<br><font size=2 face="Arial">The application context is shared between
all servlet requests, web service invocations, EJB</font>
<br><font size=2 face="Arial">remote method invocations, EJB asynchronous
method invocations, EJB timeouts and message</font>
<br><font size=2 face="Arial">deliveries to message-driven beans that execute
within the same application. The application</font>
<br><font size=2 face="Arial">context is destroyed when the application
is shut down.</font>
<br>
<br><font size=2 face="Arial">An event with qualifier </font><tt><font size=1>@Initialized(ApplicationScoped.class)
</font></tt><font size=2 face="Arial">is fired when the application</font>
<br><font size=2 face="Arial">context is initialized and an event with
qualifier </font><tt><font size=1>@Destroyed(ApplicationScoped.class) </font></tt><font size=2 face="Arial">is
fired</font>
<br><font size=2 face="Arial">when the application is destroyed. The event
payload is:</font>
<br><font size=2 face="Arial">• the </font><tt><font size=1>ServletContext
</font></tt><font size=2 face="Arial">if the application is a web application
deployed to a Servlet container, or</font>
<br><font size=2 face="Arial">• any </font><tt><font size=1>java.lang.Object
</font></tt><font size=2 face="Arial">for other types of application.</font>
<br>
<br><font size=3>How do you interpret &quot;initialized&quot;? Does it
mean the event is fired only after the context is <b>active?</b></font>
<br>
<br><font size=3>Is it possible to make the event firing a bit earlier,
say around AfterDeploymentValidation?</font>
<br>
<br><font size=3>Below is the app to demonstrate the timing along with
the server.xml:</font>
<br>
<br><font size=3><br>
<br>
<br>
And here is the results of running it<br>
<br>
<br>
[AUDIT &nbsp; ] CWWKE0001I: The server defaultServer has been launched.</font>
<br><font size=3>[AUDIT &nbsp; ] CWWKE0100I: This product is licensed for
development, and limited production use. The full license terms can be
viewed here: </font><a href=https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/wasdev/license/base_ilan/ilan/16.0.0.2/lafiles/en.html><font size=3>https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/wasdev/license/base_ilan/ilan/16.0.0.2/lafiles/en.html</font></a>
<br><font size=3>[AUDIT &nbsp; ] CWWKZ0058I: Monitoring dropins for applications.
</font>
<br><font size=3><b>---&gt; ApplicationScopedExtension, AfterDeploymentValidation
event received, before looking up and touching @ApplicationScoped dummy
bean</b></font>
<br><font size=3><b>---&gt; ApplicationScopedDummyBean, @PostConstruct
init method called</b></font>
<br><font size=3><b>---&gt; ApplicationScopedExtension, after looking up
and touching @ApplicationScoped dummy bean</b></font>
<br><font size=3>[AUDIT &nbsp; ] CWWKT0016I: Web application available
(default_host): </font><a href="http://localhost:9080/70383.180.846.test-application/"><font size=3>http://localhost:9080/70383.180.846.test-application/</font></a>
<br><font size=3><b>---&gt; ApplicationScopedObserverBean, observed @Initialized(ApplicationScoped.class)
event.</b></font>
<br><font size=3>[AUDIT &nbsp; ] CWWKZ0001I: Application 70383.180.846.test-application
started in 1.165 seconds.</font>
<br><font size=3>[AUDIT &nbsp; ] CWWKF0012I: The server installed the following
features: [jsp-2.3, ejbLite-3.2, managedBeans-1.0, jsf-2.2, beanValidation-1.1,
servlet-3.1, ssl-1.0, jndi-1.0, jsonp-1.0, appSecurity-2.0, jdbc-4.1, jaxrs-2.0,
jaxrsClient-2.0, el-3.0, json-1.0, cdi-1.2, distributedMap-1.0, webProfile-7.0,
websocket-1.1, jpa-2.1].</font>
<br><font size=3>[AUDIT &nbsp; ] CWWKF0011I: The server defaultServer is
ready to run a smarter planet.<br>
</font>
<br><font size=2 face="sans-serif">As you can see, the </font><font size=3><b>ApplicationScopedDummyBean,
@PostConstruct init method called</b></font><font size=2 face="sans-serif">
proves that the applicationContext was active before the event was fired.
Why is this applicationscoped initialization event not fired earlier?</font>
<br>
<br><font size=2 face="sans-serif"><br>
Regards<br>
Benjamin<br>
Unless stated otherwise above:<br>
IBM United Kingdom Limited - Registered in England and Wales with number
741598. <br>
Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6
3AU<br>
<br>
Unless stated otherwise above:<br>
IBM United Kingdom Limited - Registered in England and Wales with number
741598. <br>
Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6
3AU<br>
</font>