-
1. Re: In which order does Weld destroy @ApplicationScoped beans?
mkouba May 16, 2016 2:45 AM (in response to chrisjr)Hi Chris, you're right that the ordering is not guaranteed, in other words beans are destroyed in no particular order. The "dependency graph" is a nice idea but it wouldn't work for non-inject cases (
BeanManager.getReference()
,Instance.get()
) and unmanaged contextual instances (or at least would bring inadequate complexity). Also the spec does not define this. However, feel free to create a feature request (either for Weld or CDI, or both ;-). -
2. Re: In which order does Weld destroy @ApplicationScoped beans?
chrisjr May 16, 2016 4:00 AM (in response to mkouba)Thanks, perhaps future CDI could support something like EJB's @DependsOn annotation?
-
3. Re: In which order does Weld destroy @ApplicationScoped beans?
mkouba May 16, 2016 4:08 AM (in response to chrisjr)From what I've heard EJB's @DependsOn is also problematic. Maybe CDI could introduce a new container lifecycle event fired before contexts are being destroyed? In fact,
BeforeShutdown
should be rather calledAfterShutdown
. But I'm afraid we're too late to the party (renaming is not possible due to backward compatibility). -
5. Re: In which order does Weld destroy @ApplicationScoped beans?
chrisjr May 16, 2016 6:18 AM (in response to mkouba)Hmm, I'm not sure how useful a container lifecycle event would be to me. Aren't these events inaccessible from ordinary CDI managed beans, which are the beans which I need to shutdown gracefully?
-
6. Re: In which order does Weld destroy @ApplicationScoped beans?
mkouba May 16, 2016 6:39 AM (in response to chrisjr)You're right that it might be more practical to fire a regular event, similar to
@Destroyed(X.class)
. -
7. Re: In which order does Weld destroy @ApplicationScoped beans?
chrisjr May 16, 2016 6:58 AM (in response to mkouba)FWIW, I have managed to persuade my weld-servlet application to observe the BeforeShutdown event:
public class ShutdownExtension implements Extension { private static final Logger LOG = LoggerFactory.getLogger(ShutdownExtension.class); public void onShutdown( @Observes BeforeShutdown beforeShutdown, BeanManager beanManager ) { LOG.info("*** SHUTTING DOWN ***"); beanManager.fireEvent(new Shutdown()); } }
where Shutdown is my own event object that my beans can observe. However, I've also needed to create a services file:
WEB-INF/classes/META-INF/services/javax.enterprise.inject.spi.Extension
This approach does seem overly complicated!
(This is just demonstrating that I can observe container events; as you've already mentioned, the BeforeShutdown event itself is being fired after WELD has begun to destroy my beans).