Announcement Announcement Module
Collapse
No announcement yet.
How to shut down the application context properly Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to shut down the application context properly

    Hi!

    I found this web page: http://static.springsource.org/sprin...nce/beans.html

    There is the following instruction, on that page:

    (...) So to register a shutdown hook that enables the graceful shutdown of the relevant Spring IoC container, you simply need to call the registerShutdownHook() method that is declared on the AbstractApplicationContext class (...)

    It is being said to use the registerShutdownHook() method, in order to gracefully shutdown the Spring IoC container, but I am not calling that method. I am merely doing this:

    Code:
    public class SpringIoCContainerInitializer {
        private static final AbstractApplicationContext CONTEXT;
        private static final EssentialServiceThatMustBeInitialized ESSENTIAL_SERVICE;
        
        static {
            try{
                CONTEXT = new ClassPathXmlApplicationContext("spring-app-context.xml", SpringIoCContainerInitializer.class);
            } catch (Exception e) {
                throw new ExceptionInInitializerError(e);
            }
            
            try{
                ESSENTIAL_SERVICE = new EssentialServiceThatMustBeInitialized(CONTEXT);
            } catch (Exception e) {
                // Here I am calling the close() method, because an unexpected exception was thrown when instantiating ESSENTIAL_SERVICE.
                // ESSENTIAL_SERVICE is so essential that, if some problem happened during its intitialization,
                // then the spring beans on the IoC Container won't work correctly.
                // In this situation, the Spring IoC Container should not stay started.
                // That's why I am trying to shut down the container, calling the close() method.
                // Am I doing something inappropriate?
                CONTEXT.close();
                throw new ExceptionInInitializerError(e);
            }
            
        }
        
        private SpringIoCContainerInitializer() {}
    
        public static void main(String argz[]) {
            System.out.println(">>> SpringIoCContainer initialized successfully! <<<");
        }
        
        // more code...
    }
    I am just calling the close() method, from org.springframework.context.support.AbstractApplic ationContext class, and it seems to be working fine, exactly as I want. Am I doing something inappropriate? Am I not doing something that I should be?

    Thanks, in advance!

  • #2
    I suggest a read on the JVM and how shutdownhooks work.

    Basically if your application runs and you close/quit the application (i.e. kill the vm) you are not able to call close yourself, which might lead to dangling connections, unfreed resources etc. a shutdown hook registers itself with the JVM. When the JVM shutsdown it also informs all registered shutdown hooks so they can clean up.

    Comment


    • #3
      I suggest a read on the JVM and how shutdownhooks work.

      Basically if your application runs and you close/quit the application (i.e. kill the vm) you are not able to call close yourself, which might lead to dangling connections, unfreed resources etc. a shutdown hook registers itself with the JVM. When the JVM shutsdown it also informs all registered shutdown hooks so they can clean up.
      Okay, thanks for replying. I am now calling registerShutdownHook() method, see the code below.

      Code:
      public class SpringIoCContainerInitializer {
          private static final AbstractApplicationContext CONTEXT;
          private static final EssentialServiceThatMustBeInitialized ESSENTIAL_SERVICE;
          
          static {
              try{
                  CONTEXT = new ClassPathXmlApplicationContext("spring-app-context.xml", SpringIoCContainerInitializer.class);
                  CONTEXT.registerShutdownHook();
              } catch (Exception e) {
                  throw new ExceptionInInitializerError(e);
              }
              
              try{
                  ESSENTIAL_SERVICE = new EssentialServiceThatMustBeInitialized(CONTEXT);
              } catch (Exception e) {
                  // Here I am calling the close() method, because an unexpected exception was thrown when instantiating ESSENTIAL_SERVICE.
                  // ESSENTIAL_SERVICE is so essential that, if some problem happened during its intitialization,
                  // then the spring beans on the IoC Container won't work correctly.
                  // In this situation, the Spring IoC Container should not stay started.
                  // That's why I am trying to shut down the container, calling the close() method.
                  // Am I doing something inappropriate?
                  CONTEXT.close();
                  throw new ExceptionInInitializerError(e);
              }
              
          }
          
          private SpringIoCContainerInitializer() {}
      
          public static void main(String argz[]) {
              System.out.println(">>> SpringIoCContainer initialized successfully! <<<");
          }
          
          // more code...
      }
      But what I am trying to do is shutting down the application context programmatically (through the code), when an exceptional event happens. The exceptional event in my case is: problem when initializing ESSENTIAL_SERVICE. Please, see the code above and check if calling close() method that way is appropriate.

      Comment


      • #4
        I probably wasn't clear...

        A shutdown hook is for those situations that you DON'T have control. In your case you do have control at least at startup. Now how do you want to shutdown when someone closes/kills the application?

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          I probably wasn't clear...

          A shutdown hook is for those situations that you DON'T have control. In your case you do have control at least at startup. Now how do you want to shutdown when someone closes/kills the application?
          I think when someone kills the application suddenly, using, for example, a simple Ctrl+C, then the ApplicationContext will be gracefully shut down only if registerShutdownHook() method was called . So, I think calling registerShutdownHook() is always recommended, because doing so the container will destroy the lifecycle events for beans properly (I found this information here: http://www.springindepth.com/book/in...oc-events.html), when the application is killed/stopped.

          I also think when we explicitly call the close() method, then the application will also be gracefully shut down only if registerShutdownHook() method was called. So, I even ask myself "why not calling registerShutdownHook() always, as this method is always good?"

          I am so worried about close() method because I do not know if this method is the method I should call. For example, I notice there are other methods: destroy(), doClose(), and destroyBeans(). I am just calling close() because, reading the API documentation, it seems that calling this method is sufficient. There is no need to call any other method. At least that's my conclusion. About destroyBeans() method specifically, I suppose this method is called during the shutting down only if we previously called registerShutdownHook(). Am I right?

          Thank you!

          Comment


          • #6
            Close is enough to call you don't need the others. Also close will also work if you don't register the shutdown hook.. Basically the shutdown hook also calls close...

            Comment

            Working...
            X