Most of you already be knowing about Quartz scheduler but just for information Quartz scheduler is a job scheduling library that can be integrated into a wide variety of enterprise class Java applications. If you want to know more about how to implement the Quatrz scheduler in your application then you can check Quartz implementation link. Here in this article we are focusing on Quartz scheduler shutdown process, as this is where most of developers miss out details.
Issues faced due to improper implementation of Quartz Scheduler Shutdown is servers will not go down in graceful or sometime force shutdown’s also and will casue servers to go to incompatible state and Application process will get hung till we manually kill the process from back end and restart again.
To over come issues like this Quartz have provided a quartz scheduler shutdown hook plugin which catches the event of the JVM terminating, and calls shutdown on the scheduler.
Sample configuration needs to be added to quartz.properties for ShutdownHookPlugin are
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin org.quartz.plugin.shutdownhook.cleanShutdown = true
But it is recommended to use below configurations in quartz.properties so that the JVM will not hang while waiting for the Quartz scheduler threads to close themselves during any recycle processes.
org.quartz.threadPool.makeThreadsDaemons=true org.quartz.scheduler.makeSchedulerThreadDaemon=true org.quartz.scheduler.interruptJobsOnShutdown=true org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin org.quartz.plugin.shutdownhook.cleanShutdown = true
We need to mark the scheduler threads daemon as a daemon thread is a thread that does not prevent the JVM from exiting when the program finishes but the thread is still running which in turn does not interfere with the JVM while shutting down. An example for a daemon thread is the garbage collection.
Can be set to “true” to have the threads in the pool created as daemon threads. Default is “false”.
A boolean value (‘true’ or ‘false’) that specifies whether the main thread of the scheduler should be a daemon thread or not.
Logs and Outputs:
When you have successfully implemented Quartz shutdown hook plugin you shall see below initialization logs while starting the JVM:
INFO: Quartz Scheduler v.2.2.1 created. Feb 19, 2016 6:23:34 AM org.quartz.plugins.management.ShutdownHookPlugin initialize INFO: Registering Quartz shutdown hook.
While shutting down JVM:
Feb 18, 2016 6:32:06 AM org.quartz.plugins.management.ShutdownHookPlugin$1 run INFO: Shutting down Quartz...