if you're using
ContainerInitializedthen I suppose you're also using
org.jboss.weld.environment.se.StartMainto initialize the Weld container and start the application? If so then
ContainerShutdownis the right way to get notification when the container shuts down. However, I'm not really sure about the object
mainyou're calling inside the observer method. When using
StartMainyou should not perform shutdown manually - a shutdown hook is registered automatically by default. You should only care about Weld shutdown if using programmatic bootstrap API. Note that it's also possible to disable the shutdown hook feature completely by using a system property:
Also when looking at the stack trace - the ISE is thrown because the Weld container did not manage to initialize itself completely but the application shutdown is already in progress (Weld just attempts to register its own shutdown hook).
yeas i am using "StartMain". I think the problem is that in my case the method "start(@Observes ContainerInitialized event)" never returns because "main" runs until stopped. And i guess due to this stop is never called. On the other hand weld closes the application as soon as start method has returned (i tried or put main.start() in another thred).
I need to run main.stop() because it waits until all worked has fished (with timeout).
So the question where to put main.start()? So that on the one hand the initialization of weld can be completed but on the other hand weld does not close it self after initialization. Or to put the question the other way around: How to implement a weld se application that runs forever (like a server/service)?
I'm sorry I still don't understand what
main.run()represents? Anyway, you should never start the Weld container inside a ContainerInitialized observer method - this observer is notified when the container is already being initialized! It would be helpful if you could share your code (or minimal reproducer). If you want to implement a Weld SE application that runs in a loop then you should implement a custom main method, start the Weld container using programmatic bootstrap API and implement some kind of infinite loop (or start a non-daemon thread).
just think of main.run() as a never stopping method until someone presses "ctrl-c" (if hangup support is enabled) or someone calls main.stop() (from another thread).
I think the use case to run a never stopping service is not unusual?! Probably it is better to write an own main method and control the container?
Yes, write your own main method. Never implement an infinite loop in a ContainerInitialized observer method otherwise the Weld container initialization never ends!
Thanks, i will do so,
But thismeans that org.jboss.weld.environment.se.StartMain is not usable for service like (never ending) applications? And it means that instantly after container initialization container shutdown is executed?! There is no "container is running" phase (for StartMain) right?
Well, you could start a non-daemon thread (e.g. Swing GUI) in the observer method - in such case the JVM will not stop when
Thanks for this tip, i will try both ways (non-deamon thread and own main method)