Spring Batch with Quartz scheduler
Introduction
Spring Batch is one of the most widely used frameworks for implementing an offline batch process. The programming model of the Spring Batch framework has been leveraged as a Java Platform Standard – JSR 352.
The scheduling of a Spring Batch can be done using any of the schedulers available in the market – Quartz, Control-M etc. Often for a basic scheduling, we can set up a crontab.
Below are the steps required for integrating Quartz scheduler with Spring Batch.
Configuration
We will be adding 2 new dependencies in pom.xml i.e. spring-batch-admin-manager and spring-batch-admin-resources. In addition, override components from Spring Batch Admin
- override-ctx.xml in
META-INF/spring/batch/override
for setting any additional beans - web-context.xml in
META-INF/spring/batch/servlet/override
for the spring beans and Apache FreeMarker templates. - The jobs are configured in sample-batch-job.xml
The source code is available in GitHub
Implementing the Quartz Job interface
The JobService interface is used for monitoring and management of the batch jobs. For scheduling a job, we will provide an implementation in the application-context
with SimpleJobServiceFactoryBean. Refer the override-ctx.xml
Provide an implementation for Quartz Job interface using JobLauncherDetails.java
Scheduling
The Job is scheduled using the Quartz CronScheduleBuilder. The source code is available in GitHub
Persisting scheduled jobs
The Quartz service implementation is storing the information in the domain object BatchJobDataStore only in memory. Hence, the job information is not available once the JVM has been shutdown or restarted. If you have the requirement to persist state to a database, you will need to add custom logic when a job has been scheduled in the QuartzServiceImpl.java
. You will also need to load the jobs from the database during the start-up.