Note: Updated the post to reflect code from Quartz version 2.2.*
Job Stores in Quartz scheduler – Job Store is used by the Quartz itself to store the details about the Scheduler, Triggers and Calendar etc. It is important to note that the Job Store instance is not to be used directly but must be left to Quartz itself. The two important JobStore types available are – RAMJobStore and JDBCJobStore
RAMJobStore – As the name indicates using this job store indicates that Quartz store all its scheduler related data in the RAM itself. This method of job store is useful such that, the storing of data in the RAM enables fast retrieval and update of data. But the downside of this method is that it is volatile as the data stored is not permanent and is prone to loss.
This can be specified in the properties file for quartz as –
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
Sample Program – file quartz.properties
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore org.quartz.scheduler.instanceName = PRINT_SCHEDULER org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 2
PrintRamScheduler.java-
import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import static org.quartz.JobBuilder.newJob; import static org.quartz.SimpleScheduleBuilder.simpleSchedule; public class PrintRamScheduler { private Scheduler scheduler; public PrintRamScheduler() { try { //create scheduler factory scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); } catch (Exception e) { e.printStackTrace(); } } public void schedule() throws SchedulerException { //create a job JobDetail job = newJob(PrintStatefulJob.class).withIdentity("printjob", "printjobgroup").build(); //put a count variable that we can keep incrementing job.getJobDataMap().put("count",0); //create a trigger Trigger trigger = TriggerBuilder.newTrigger().withIdentity("printTrigger", "printtriggergroup") .startNow().withSchedule(simpleSchedule().withIntervalInMilliseconds(100l).repeatForever()).build(); //schedule the job scheduler.scheduleJob(job, trigger); } public void stopScheduler() throws SchedulerException { //scheduler shutdown scheduler.shutdown(); } public static void main(String[] args) { PrintRamScheduler printScheduler = new PrintRamScheduler(); try { printScheduler.schedule(); Thread.sleep(60000l); printScheduler.stopScheduler(); } catch (Exception e) { e.printStackTrace(); } }
The same can also be implemented without using a properties file but using the following code
Properties prop = new Properties(); prop.setProperty("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore"); prop.setProperty("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool"); prop.setProperty("org.quartz.threadPool.threadCount", "4"); SchedulerFactory schdFact = new StdSchedulerFactory(prop);
PrintStatefulJob.java –
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); } }
Code @ Github – https://github.com/vageeshhoskere/blog/tree/master/quartz/src/main/java/ramstore