9 Replies Latest reply on Dec 11, 2015 5:45 AM by dermoritz

    Weld SE application lifecycle detecting stop

    dermoritz

      I have created an app using weld se. The application provides some service and therefor it runs until stopped (ctrl-c).

       

       

      The documentation says much about starting and bootstrapping but nothing about shutting down or how to implement lifecycle.

      So starting my application works fine:

       

       

          public void start(@Observes ContainerInitialized event) throws Exception {

              log.info("starting");

              main.run(); //runs until stopped

          }

       

       

      The problem is how to get notified on stop i thought `@Observes ContainerShutdown event` could help but it doesn't:

       

       

          public void stop(@Observes ContainerShutdown event) throws Exception {

              log.info("stopping");

              main.stop();

          }

       

       

      This method is never called. Probably due to the fact that is in same thread as start and start never ends. On the other hand: the application shuts down as soon as start returns, so putting main.run() in another thread does not work.

      "main" is of Type camel.Main it has it's own "hangup support" (detects ctrl-c). If i set enableHangupSupport() camel is shutting down fine but weld throws:

       

      Exception in thread "main" java.lang.IllegalStateException: Shutdown in progress
        at java.lang.ApplicationShutdownHooks.add(Unknown Source)
        at java.lang.Runtime.addShutdownHook(Unknown Source)
        at org.jboss.weld.environment.se.WeldContainer.initialize(WeldContainer.java:150)
        at org.jboss.weld.environment.se.Weld.initialize(Weld.java:562)
        at org.jboss.weld.environment.se.StartMain.go(StartMain.java:44)
        at org.jboss.weld.environment.se.StartMain.main(StartMain.java:53)

       

      So how to correctly implement the lifecycle?

      I need to call main.stop() on ctrl-c or i need to prevent this exception.

       

      Thanks in advance

        • 1. Re: Weld SE application lifecycle detecting stop
          mkouba

          Hi,

          if you're using ContainerInitialized then I suppose you're also using org.jboss.weld.environment.se.StartMain to initialize the Weld container and start the application? If so then ContainerShutdown is the right way to get notification when the container shuts down. However, I'm not really sure about the object main you're calling inside the observer method. When using StartMain you 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: -Dorg.jboss.weld.se.shutdownHook=false

          • 2. Re: Weld SE application lifecycle detecting stop
            mkouba

            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).

            • 3. Re: Weld SE application lifecycle detecting stop
              dermoritz

              Thanks,

               

              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)?

              • 4. Re: Weld SE application lifecycle detecting stop
                mkouba

                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).

                • 5. Re: Weld SE application lifecycle detecting stop
                  dermoritz

                  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?

                  • 6. Re: Weld SE application lifecycle detecting stop
                    mkouba

                    Yes, write your own main method. Never implement an infinite loop in a ContainerInitialized observer method otherwise the Weld container initialization never ends!

                    • 7. Re: Weld SE application lifecycle detecting stop
                      dermoritz

                      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?

                      • 8. Re: Weld SE application lifecycle detecting stop
                        mkouba

                        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 StartMain.main(String[]) completes.

                        • 9. Re: Weld SE application lifecycle detecting stop
                          dermoritz

                          Thanks for this tip, i will try both ways (non-deamon thread and own main method)