Quartz Scheduler Spring

Quartz Scheduler Spring

In previous tutorial you saw a simple example of Quartz scheduler with Spring application and we were not using full functionality of spring to control the jobs. In this tutorial you will see how to control Quartz scheduling and execution from spring context file. Main reason of using this advance configuration is, you don’t need to write any Quartz related java code and everything is configurable through spring context file.

  • Create maven project name: QuartzSpringScheduler as below is snap shot of final structure:

Quartz Scheduler Spring

  • pom.xml:

  • log4j2.xml: Will be using logging as well:

  • spring-context.xml: Here is all configuration related to Quartz scheduler as explained through the diagram:

Quartz Scheduler Spring

  • Cron-Schedule.properties: I have creates separate properties file from where you could control job run time:

If it is your first time configuration with Cron scheduler as you don’t understand Cron time please visit CronMaker to understand it better.

  • CronQuartzJob.java: This is sample class with one method which will be called every schedule time:

  • CronQuartzJobTestApp.java: Main class to run Spring application and I have included method name: registerShutdownHook to keep thread alive:

  • That’s it. To run this application right click CronQuartzJobTestApp.java –> Run As –> Java Application. As scheduler has been configured to run every 10 seconds so it will go to CronQuartzJob class and call its method: executeScheduleJob and will print logging information on the console as below:

Quartz Scheduler Spring

download Download Project:  QuartzSpringScheduler

Leave a Reply

Your email address will not be published. Required fields are marked *