Note: Updated the post to reflect code from Quartz version 2.2.*
As mentioned in the previous post, a Job Store is used by the quartz scheduler to store information about itself, and a JDBCJobStore is a way of maintaining the quartz job details over a database via the JDBC. While the use of RAMJobStore indicates volatile storage of the quartz job details, a JDBCJobStore ensures that the information on the quartz jobs, triggers, calendars etc are available any time in case the system has a downtime and then can be rescheduled once the system is up.
A JDBCJobStore requires some database tables to be present in the data source defined for use of quartz. The sql queries for creating and populating the required tables is available under the docs/dbTables folder of the quartz distribution. The following example uses the MySQL database to schedule a job using the JDBCJobStore.
The file quartz.properties is used by the program to define the quartz properties –
Sample Program – file quartz.properties
org.quartz.scheduler.instanceName = PRINT_SCHEDULER org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 4 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true #specify the jobstore used org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.useProperties = false #The datasource for the jobstore that is to be used org.quartz.jobStore.dataSource = myDS #quartz table prefixes in the database org.quartz.jobStore.tablePrefix = qrtz_ org.quartz.jobStore.misfireThreshold = 60000 org.quartz.jobStore.isClustered = true org.quartz.scheduler.instanceId = PRINT_SCHEDULER #The details of the datasource specified previously org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost:3307/blog_test org.quartz.dataSource.myDS.user = root org.quartz.dataSource.myDS.password = root org.quartz.dataSource.myDS.maxConnections = 20
Sample program PrintScheduler.java
import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import ramstore.PrintStatefulJob; import java.io.*; import java.util.Properties; import static org.quartz.JobBuilder.newJob; import static org.quartz.SimpleScheduleBuilder.simpleSchedule; public class PrintScheduler { private Scheduler scheduler; public PrintScheduler(String instanceId) { try { scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); } catch (Exception e) { e.printStackTrace(); } } public void schedule() throws SchedulerException { JobDetail job = newJob(PrintStatefulJob.class).withIdentity("printjob", "printjobgroup").build(); job.getJobDataMap().put("count",0); Trigger trigger = TriggerBuilder.newTrigger().withIdentity("printTrigger", "printtriggergroup") .startNow().withSchedule(simpleSchedule().withIntervalInMilliseconds(100l).repeatForever()).build(); scheduler.scheduleJob(job, trigger); } public void stopScheduler() throws SchedulerException { scheduler.shutdown(); } public static void main(String[] args) { PrintScheduler printScheduler = new PrintScheduler(args[0]); try { // printScheduler.schedule(); Thread.sleep(60000l); printScheduler.stopScheduler(); } catch (Exception e) { e.printStackTrace(); } } }
PrintStatefulJob.java –
package ramstore; import org.quartz.*; @PersistJobDataAfterExecution public class PrintStatefulJob implements Job{ @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap(); Integer count = jobDataMap.getInt("count"); count++; System.out.println("Printing count : "+count); jobDataMap.put("count",count); } }
On successful fire of job, it can be observed that the job details and data blob details are updated in the tables of the database.
The job thus stored in the database can be rescheduled in case there is any need. This post deals with rescheduling of jobs from the JDBCJobStore
Code @ Github – https://github.com/vageeshhoskere/blog/tree/master/quartz