[weld-dev] ApplicationScoped event fires on service invocation

Martin Kouba mkouba at redhat.com
Mon Aug 22 04:22:52 EDT 2016


Dne 21.8.2016 v 12:32 Mark Struberg napsal(a):
> But the ServletContextEvent is available much earlier.
> I think this might rather be an integration issue.

ServletContext might be available earlier but Weld is using standard 
portable ServletContextListener API to be notified. In any case, the 
client code should not rely on the timing here as it's not exactly defined.

>
>
> In OWB we don't fire the @Intialized for the ApplicationContext inside the ApplicationContext implementation but as part of the integration code, means TomEE, Geronimo, SAP, etc.
>
> The interplay and boot order between EJB @Startup @Singleton and the CDI container is imo not really well defined. We just did what we think is best. Afair that means that in TomEE we fire the @Initialized _before_ we do @Startup but _after_ all the metadata parsing and EJB and CDI Extension handling was done.

I would rather say this is completely undefined. Java EE spec does not 
say anything about cooperation and startup order of various 
technologies. That's why for example injection into JPA entity listeners 
is problematic (and impossible to implement 100% correctly).

>
>
> LieGrue,
> strub
>
>
>
>
>
>> On Friday, 19 August 2016, 15:37, Martin Kouba <mkouba at redhat.com> wrote:
>>> Hi Benjanmin,
>>
>> for a web application the payload of
>> @Initialized(ApplicationScoped.class) must be the relevant
>> ServletContext. That's why Weld is waiting for a webapp to be ready
>> (implemented as
>> javax.servlet.ServletContextListener.contextInitialized(ServletContextEvent)).
>>
>> Martin
>>
>> Dne 19.8.2016 v 14:17 Benjamin Confino napsal(a):
>>>  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
>>>
>>>
>>>  _______________________________________________
>>>  weld-dev mailing list
>>>  weld-dev at lists.jboss.org
>>>  https://lists.jboss.org/mailman/listinfo/weld-dev
>>>
>>
>> --
>> Martin Kouba
>> Software Engineer
>> Red Hat, Czech Republic
>>
>> _______________________________________________
>> weld-dev mailing list
>> weld-dev at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/weld-dev
>>

-- 
Martin Kouba
Software Engineer
Red Hat, Czech Republic


More information about the weld-dev mailing list