[weld-dev] ApplicationScoped event fires on service invocation

Benjamin Confino BENJAMIC at uk.ibm.com
Fri Aug 19 08:17:26 EDT 2016


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.

However, the event was fired only after the service invocation, which was 
too late. Apparently OWB fires this event much earlier. 
The CDI spec states:

The application context is provided by a built-in context object for the 
built-in scope type
@ApplicationScoped. The application scope is active:
• during the service() method of any servlet in the web application, 
during the doFilter()
method of any servlet filter and when the container calls any 
ServletContextListener,
HttpSessionListener, AsyncListener or ServletRequestListener,
• during any Java EE web service invocation,
• during any remote method invocation of any EJB, during any asynchronous 
method invocation
of any EJB, during any call to an EJB timeout method and during message 
delivery to any EJB
message-driven bean,
• when the disposer method or @PreDestroy callback of any bean with any 
normal scope other
than @ApplicationScoped is called, and
• during @PostConstruct callback of any bean.

The application context is shared between all servlet requests, web 
service invocations, EJB
remote method invocations, EJB asynchronous method invocations, EJB 
timeouts and message
deliveries to message-driven beans that execute within the same 
application. The application
context is destroyed when the application is shut down.

An event with qualifier @Initialized(ApplicationScoped.class) is fired 
when the application
context is initialized and an event with qualifier 
@Destroyed(ApplicationScoped.class) is fired
when the application is destroyed. The event payload is:
• the ServletContext if the application is a web application deployed to a 
Servlet container, or
• any java.lang.Object for other types of application.

How do you interpret "initialized"? Does it mean the event is fired only 
after the context is active?

Is it possible to make the event firing a bit earlier, say around 
AfterDeploymentValidation?

Below is the app to demonstrate the timing along with the server.xml:




And here is the results of running it


[AUDIT   ] CWWKE0001I: The server defaultServer has been launched.
[AUDIT   ] CWWKE0100I: This product is licensed for development, and 
limited production use. The full license terms can be viewed here: 
https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/wasdev/license/base_ilan/ilan/16.0.0.2/lafiles/en.html
[AUDIT   ] CWWKZ0058I: Monitoring dropins for applications. 
---> ApplicationScopedExtension, AfterDeploymentValidation event received, 
before looking up and touching @ApplicationScoped dummy bean
---> ApplicationScopedDummyBean, @PostConstruct init method called
---> ApplicationScopedExtension, after looking up and touching 
@ApplicationScoped dummy bean
[AUDIT   ] CWWKT0016I: Web application available (default_host): 
http://localhost:9080/70383.180.846.test-application/
---> ApplicationScopedObserverBean, observed 
@Initialized(ApplicationScoped.class) event.
[AUDIT   ] CWWKZ0001I: Application 70383.180.846.test-application started 
in 1.165 seconds.
[AUDIT   ] 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].
[AUDIT   ] CWWKF0011I: The server defaultServer is ready to run a smarter 
planet.

As you can see, the ApplicationScopedDummyBean, @PostConstruct init method 
called proves that the applicationContext was active before the event was 
fired. Why is this applicationscoped initialization event not fired 
earlier?


Regards
Benjamin
Unless stated otherwise above:
IBM United Kingdom Limited - Registered in England and Wales with number 
741598. 
Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU

Unless stated otherwise above:
IBM United Kingdom Limited - Registered in England and Wales with number 
741598. 
Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/weld-dev/attachments/20160819/daa0b632/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test-application.war
Type: application/octet-stream
Size: 5489 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/weld-dev/attachments/20160819/daa0b632/attachment-0002.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test-application-source.zip
Type: application/zip
Size: 4092 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/weld-dev/attachments/20160819/daa0b632/attachment-0001.zip 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: server.xml
Type: application/octet-stream
Size: 599 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/weld-dev/attachments/20160819/daa0b632/attachment-0003.obj 


More information about the weld-dev mailing list