Breaking News
Home / Java / Java Tutorials / A typical ShutdownHook in Java

A typical ShutdownHook in Java

Java Shutdown Hook ExampleShutdown hooks are a very important feature of the JVM. They provide the capability to do resource cleanup or store application state when the JVM shuts down.

In a typical application there are lot of different type of resources like Database Connection Pool, Thread Pools, Job Schedulers etc. that are instantiated and need to be cleaned up on shutdown. Using the concept of shutdown hook this can be achieved very easily.<!>

The below shows an example of a typical shutdown hook. Depending on your application these resources and its cleanup would be different.

Source

  1. package com.infuze.server.util;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import org.quartz.JobExecutionContext;
  5. import org.quartz.Scheduler;
  6. import com.infuze.server.config.AppConfig;
  7. import com.livrona.comono.logger.Logger;
  8. import com.livrona.comono.logger.LoggerFactory;
  9. public class ServerShutdownHook extends Thread {
  10.         private static final Logger log = LoggerFactory.createLogger(ServerShutdownHook.class);
  11.         /**
  12.          * Start the Shutdown Hook
  13.          */
  14.         public void run() {
  15.                 log.info(“method : run()”);
  16.                 shutdownPinger();
  17.                 shutDownThreadPool();
  18.                 shutDownDatabasePool();
  19.                 shutdownScheduler();
  20.         }
  21.         private void shutDownDatabasePool() {
  22.                 log.info(“in : shutDownDatabasePool”);
  23.                 try {
  24.                         DataSourceManager.getInstance().shutdown();
  25.                 } catch (Exception e) {
  26.                         log.error(“Failed to shutdown the Database Pool : “ + e.getMessage(), e);
  27.                 }
  28.         }
  29.         private void shutDownThreadPool() {
  30.                 log.info(“Shutdown Thread Pool”);
  31.                 try {
  32.                         ThreadPoolManager.getInstance().shutdown();
  33.                 } catch (Exception e) {
  34.                         log.error(“Failed to shutdown the Thread Pool : “ + e.getMessage(), e);
  35.                 }
  36.         }
  37.         /**
  38.          * Shutdown Pinger Service
  39.          *
  40.          */
  41.         private void shutdownPinger() {
  42.                 log.info(“Shutdown Pinger Service”);
  43.                 try
  44.                 {
  45.                         PingerManager pinger = PingerManager.getInstance();
  46.                         pinger.shutdown();
  47.                 } catch (Exception e)
  48.                 {
  49.                         log.error(“Failed to shutdown the Pinger : “ + e.getMessage(), e);
  50.                 }
  51.         }
  52.         /**
  53.          * Shutdown the scheduler
  54.          */
  55.     private void shutdownScheduler() {
  56.         try {
  57.                 log.info(“Performing Job Resources CleanUp”);
  58. Scheduler scheduler = SchedulerManager.getInstance().getScheduler();
  59.         if (scheduler != null) {
  60.  List jobExeContextList = scheduler.getCurrentlyExecutingJobs();
  61.        log.info(“There are “ + jobExeContextList.size() + ” running jobs at shutdown.”);
  62.         Iterator it = jobExeContextList.iterator();
  63.         while (it.hasNext()) {
  64.         JobExecutionContext jobExeContext = (JobExecutionContext)
  65.         it.next();
  66.         BasicJob job = (BasicJob) jobExeContext.getJobInstance();
  67.         job.cleanup();
  68.    }
  69.         log.info(“Shutting down Quartz Scheduler…”);
  70.                 scheduler.shutdown(true);
  71.         } else {
  72.                 log.info(“Quartz Scheduler is not running”);
  73.         }
  74.    } catch (Exception e) {
  75.    log.info(“Error shutting down Quartz: “ + e.getMessage(), e);
  76.   }
  77.  }
  78. }
  79. <java>
  80. </p>
[pb_builder]

Check Also

Write a program to generate Fibonacci Numbers

In mathematics, the Fibonacci numbers are the numbers in the following sequence: 0,1,12,3,5,8,13,21,34,55,89,144 By definition, …

Advertisment ad adsense adlogger