[wildfly-dev] WELD/CDI event clarifications

Martin Kouba mkouba at redhat.com
Mon Nov 12 10:40:09 EST 2018


Hi Eric,

Weld forum is indeed inaccessible. I don't know what the problem is but 
we'll try to make it work ASAP. Anyway, there are other channels 
mentioned on the weld site [1].

WRT your questions - I added few comments inline.

HTH

Martin

[1]
weld/user on Gitter
#weld-dev IRC channel hosted on freenode.net
  weld-dev mailing list - https://lists.jboss.org/mailman/listinfo/weld-dev



Dne 12. 11. 18 v 15:56 Eric B napsal(a):
> I apologize if this not the correct forum to post this question, but I 
> tried looking at the Weld forums and everything appears dormant (even 
> the link to the user's forum is 404 on the Weld site).  So I suspected 
> this would be the second best place to ask for some clarity.
> 
> I'm trying to understand the sequencing of CDI events vs bean 
> initialization.  I'm currently concentrating on CDI 1.2/Weld 2.3.5, but 
> figure that this information will be applicable to CDI2.0 as well.  
>   I've tried finding documentation online and posting on StackOverflow 
> and neither have produced any results.  Specifically, I am confused 
> about the sequencing of CDI events vs when/how bean methods are called.  
> Given what I have read, the following are the events that are triggered 
> by the CDI container:
> 
>   * BeforeBeanDiscovery
>   * ProcessAnnotatedType and ProcessSyntheticAnnotatedType
>   * AfterTypeDiscovery
>   * ProcessInjectionTarget and ProcessProducer
>   * ProcessInjectionPoint
>   * ProcessBeanAttributes
>   * ProcessBean, ProcessManagedBean, ProcessSessionBean,
>     ProcessProducerMethod and ProcessProducerField
>   * ProcessObserverMethod
>   * AfterBeanDiscovery
>   * AfterDeploymentValidation
>   * BeforeShutdown

These are container lifecycle events that can be observed on CDI 
extensions only and the contract is well described in the spec:
http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#initialization

> 
> Given these events, when are the events which produce 
> @Initialized(ApplicationScoped.class) 
> and @Destroyer(ApplicationScoped.class) triggered?  

@Initialized/@Destroyed(/@Predestroyed in 2.0) are regular events that 
can be observed by regular beans. For example 
@Initialized(ApplicationScoped.class) events are fired once the 
application context is initialized.

Now "when is @Initialized(ApplicationScoped.class) fired?" - this is a 
little bit more complicated. It depends on the target runtime (WildFly, 
Weld SE, Tomcat, ...) and the deployment structure (WAR, EJB, etc.).

E.g. in WildFly, for beans located in a WAR the event payload is 
ServletContext and is fired during 
ServletContextListener#contextInitialized() notification. For other 
types of archives (EJB etc.) the event payload is java.lang.Object and 
is fired after the Weld container is fully initialized.

In SE, the event is fired after the WeldContainer is initialized.

> In which part of the 
> lifecycle?  Are they triggered in the @PostConstruct equivalent of the 
> Scoping bean (what is the bean backing the ApplicationScope?  I cannot 
> find the implementation in the Weld core - am I even looking in the 
> right place?)?

I'm not sure what you mean here. A @PostConstruct callback is invoked 
after a contextual instance has been constructed and dependency 
injection on that instance has been completed, but before this instance 
is put into service.

So if you declare an @Initialized(ApplicationScoped.class) observer on a 
bean the observer is always notified after the @PostConstruct callback.

> 
> Which brings me to the next question - in which stage of the container 
> setup are the @PostConstruct methods on the beans called?  Are they 
> called in the `AfterDeploymentValidation` phase?  

No. @PostConstruct callbacks are invoked once a contextual instance is 
created. Contextual instances are created lazily, on demand. This means 
that @PostConstruct callbacks are called anytime after 
AfterDeploymentValidation event is fired but before the application 
shuts down.

> Or is it somewhat 
> asynchronous - in that the @PostConstruct of an ApplicationScoped bean 
> will be called only when initialized; and that could happen during any 
> of the stages?  From what I understand, the BeanManager.getReference() 
> is not allowed prior to the AfterDeploymentValidation event being fired, 
> which to me would imply that beans can only be instantiated (and 
> therefore @PostConstruct'ed) after the `AfterDeploymentValidation` phase.
> 
> Can anyone direct me towards some clarification of these points?  I've 
> tried reading the CDI 1.2 user guide 
> (https://docs.jboss.org/cdi/learn/userguide/CDI-user-guide.html), but 
> that hasn't provided the details I am looking for.  Additionally, I've 
> tried reading through the Weld code but can't find the answers I'm 
> looking for.
> 
> Thanks,
> 
> Eric
> 
> _______________________________________________
> wildfly-dev mailing list
> wildfly-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/wildfly-dev
> 

-- 
Martin Kouba
Senior Software Engineer
Red Hat, Czech Republic


More information about the wildfly-dev mailing list