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

    Weld SE application lifecycle detecting stop

    moritz l Newbie

      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