An example of Java Shutdown Hook in J2EE   #


import java.util.Iterator; import java.util.List; import org.quartz.JobExecutionContext; import org.quartz.Scheduler; import com.livrona.server.config.AppConfig; import com.livrona.comono.logger.Logger; import com.livrona.comono.logger.LoggerFactory; public class ServerShutdownHook extends Thread { private static final Logger log = LoggerFactory.createLogger(ServerShutdownHook.class); /** * Start the Shutdown Hook */ public void run() { log.info("method : run()"); shutdownPinger(); shutDownThreadPool(); shutDownDatabasePool(); shutdownScheduler(); } private void shutDownDatabasePool() { log.info("in : shutDownDatabasePool"); try { DataSourceManager.getInstance().shutdown(); } catch (Exception e) { log.error("Failed to shutdown the Database Pool : " + e.getMessage(), e); } } private void shutDownThreadPool() { log.info("Shutdown Thread Pool"); try { ThreadPoolManager.getInstance().shutdown(); } catch (Exception e) { log.error("Failed to shutdown the Thread Pool : " + e.getMessage(), e); } } /** * Shutdown Pinger Service * */ private void shutdownPinger() { log.info("Shutdown Pinger Service"); try { PingerManager pinger = PingerManager.getInstance(); pinger.shutdown(); } catch (Exception e) { log.error("Failed to shutdown the Pinger : " + e.getMessage(), e); } } /** * Shutdown the scheduler */ private void shutdownScheduler() { try { log.info("Performing Job Resources CleanUp"); Scheduler scheduler = SchedulerManager.getInstance().getScheduler(); if (scheduler != null) { List jobExeContextList = scheduler.getCurrentlyExecutingJobs(); log.info("There are " + jobExeContextList.size() + " running jobs at shutdown."); Iterator it = jobExeContextList.iterator(); while (it.hasNext()) { JobExecutionContext jobExeContext = (JobExecutionContext) it.next(); BasicJob job = (BasicJob) jobExeContext.getJobInstance(); job.cleanup(); } log.info("Shutting down Quartz Scheduler…"); scheduler.shutdown(true); } else { log.info("Quartz Scheduler is not running"); } } catch (Exception e) { log.info("Error shutting down Quartz: " + e.getMessage(), e); } } }

Loading Please Wait...