0

I am rather new to Java and I need to construct a way inside of my application to handle a SIGTERM signal so that my application can gracefully shutdown. This means it finishes what it is currently working on and does not accept new work. The application will shutdown after the in-progress work is completed successfully.

If I am correct, I need to use addShutdownHook but I am unclear as to where this will be implemented.

Do I keep inside the main Java class or do I create a separate Java class? If the latter, how do I call that method in the main Java class?

Does anyone have an example of implementing an addShutdownHook and where they both put it and called it?

Sorry for the rudimentary question but this is all new to me.

  • Please search before you post. http://stackoverflow.com/questions/8722826/when-do-i-need-to-call-this-method-runtime-getruntime-addshutdownhook http://www.c4learn.com/java/java-addshutdownhook/ – u8sand Nov 16 '15 at 14:57
  • I read through that post but that does not answer my question. How do I ensure all current work is executed before the application shuts down? – user2081925 Nov 16 '15 at 15:00
  • If that's your question then you should say so, because what you've written is a duplicate of the question referenced by u8sand above – Software Engineer Nov 16 '15 at 15:03
  • Sorry. I thought I did. I need to ensure all outstanding work is executed before the application terminates. Does that need to be included in the addShutdownHook method? – user2081925 Nov 16 '15 at 15:05
  • @user2081925 **how** depends on what particular work you're doing, it is the hook that gets processed and the hook that defines what to do when it receives SIGTERM. You could, for instance, flush the current state of your application to a file to be loaded up later. – u8sand Nov 16 '15 at 15:08
  • In my particular case, we have documents being parsed in a loader. When the SIGTERM is received, I need to configure the Loader to finish with the current document, not accept any further documents, clean up and terminate. Is this all defined in the addShutdownHook? – user2081925 Nov 16 '15 at 15:24

0 Answers0