-
1. Re: Weld shutdown hook question: maybe interrupt() container thread?
ljnelson Feb 17, 2017 7:49 PM (in response to ljnelson)Hmm; actually a related question.
From looking more at the
WeldContainer.java
file, I see that the (single) shutdown hook that runs (a) spins through the list of running containers and (b) callsshutdown()
on each one. That's fine.So obviously on normal JVM exits even though this hook is in place and will run, it will do nothing, because all the containers will no longer be running. Fine.
Assume this time I'm blocked in a method like this:
private final void block(@Observes @Initialized(ApplicationScoped.class) @Priority(PLATFORM_AFTER) final Object event) throws InterruptedException {
Thread.currentThread().join();
System.out.println("*** done joining?!");
}
...and assume that I'm not defining my own shutdown hook, and I'm not disabling the Weld one, so in other words just regular Weld behavior.
What I'm seeing when I hit CTRL-C (while the thread (and thus process) is blocked) is obviously the Weld shutdown hook runs, and presumably shutdown() is called because the JVM exits. I never see *** done joining?! which is good because I wouldn't expect to. That's fine.
But a portable extension of mine is not getting notified of the BeforeShutdown event, or actually any other event (e.g. @BeforeDestroyed(ApplicationScoped.class) etc.). Shouldn't this happen as a consequence of shutdown(), regardless of how it is invoked (i.e. normally or via a shutdown hook)? Line 265 seems to indicate that at least one event is fired, and I'm guessing that BeforeShutdown should be fired somewhere as a result of line 271.
But, this is all during a shutdown hook, invoked from a separate thread (the shutdown hook thread), so who knows? What if anything can I rely on in Weld here? Are events "turned off" if shutdown is initiated by Weld's shutdown hook?
-
2. Re: Weld shutdown hook question: maybe interrupt() container thread?
ljnelson Feb 17, 2017 8:54 PM (in response to ljnelson)Ah, I think I know what's going on. The Weld shutdown hook in this case is not running, because it never has a chance to be added. A bean's observer method that is observing @Initialized(ApplicationScoped.class) Object payload is invoked during WeldContainer.complete(), and therefore before the Weld shutdown hook is installed. Therefore, CTRL-C (SIGINT) signals are captured by the JVM and no shutdown hook is in place, so the VM just exits hard.
It seems like if the Weld shutdown hook were added before the container.complete() call, then it would properly shut down the container in the use case above.
-
3. Re: Weld shutdown hook question: maybe interrupt() container thread?
mkouba Feb 20, 2017 3:01 AM (in response to ljnelson)Hi Laird,
First of all, you should never do
Thread.currentThread().join()
during Weld bootstrap or shutdown. It effectively blocks the current thread forever, so the bootstrap or shutdown process will never complete correctly. Also registering a shutdown hook before the bootstrap completes sounds like a bad idea to me. Instead of discussions like this, wouldn't it make more sense to clearly define the original use case and attempt to find a proper solution? -
4. Re: Weld shutdown hook question: maybe interrupt() container thread?
ljnelson Feb 20, 2017 5:52 PM (in response to mkouba) -
5. Re: Weld shutdown hook question: maybe interrupt() container thread?
mkouba Feb 21, 2017 2:50 AM (in response to ljnelson)It seems like if the Weld shutdown hook were added before the container.complete() call, then it would properly shut down the container in the use case above.
Actually, this is a good point.
ContainerInitialized
should be fired after the container is initialized and shutdown hook is registered. So that it could be used as an entry point for an application.