Quartz Scheduler – Scheduling Job in Java via Quartz Scheduler
Quartz scheduler to help Java application to scheduler a job/task to run at a specified date and time.
Library
commons-logging-1.1.1.jar
log4j-1.2.16.jar
quartz-2.2.1.jar
slf4j-api-1.6.6.jar
slf4j-log4j12-1.6.6.jar
Note To deploy Quartz on Application server like JBoss, oracle or weblogic, you may need addition Quartz dependency, read this guide.
Quartz Job
Quartz job is defined what you want to run
package com.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; public class QuartzJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { JobKey jobKey = context.getJobDetail().getKey(); System.out.println("Quartz " + "Job Key " + jobKey); } }
Quartz Trigger
Quartz trigger is defined when the Quartz will run your above Quartz’s job
There are 2 types of triggers in Quartz 2
SimpleTrigger – Run every 10 seconds.
Trigger trigger = newTrigger().withIdentity("TriggerName", "Group1") .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10).repeatForever()).build();
CronTrigger – Run every 10 seconds
Trigger trigger = newTrigger() .withIdentity("TriggerName", "Group2") .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();
Scheduler
Scheduler class links both “Job” and “Trigger” together and execute it.
Scheduler scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); scheduler.scheduleJob(job, trigger);
Recommended Article
Full Example
Quartz 2 full examples with SimpleTrigger and CronTrigger.
package com.quartz; import static org.quartz.JobBuilder.newJob; import static org.quartz.TriggerBuilder.newTrigger; import org.quartz.CronScheduleBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleScheduleBuilder; import org.quartz.Trigger; import org.quartz.impl.StdSchedulerFactory; public class QuartzSchedulerDemo { public static void SimpleTriggerExample() throws SchedulerException { JobDetail job = newJob(QuartzJob.class).withIdentity( "SimpleQuartzTrigger", "Group1").build(); Trigger trigger = newTrigger() .withIdentity("TriggerName", "Group1") .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10).repeatForever()) .build(); Scheduler sched = new StdSchedulerFactory().getScheduler(); sched.scheduleJob(job, trigger); sched.start(); } public static void CronTriggerExample() throws SchedulerException { JobDetail job = newJob(QuartzJob.class).withIdentity( "CronQuartzTrigger", "Group2").build(); Trigger trigger = newTrigger() .withIdentity("TriggerName", "Group2") .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) .build(); Scheduler scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); scheduler.scheduleJob(job, trigger); } public static void main(String[] args) throws SchedulerException { SimpleTriggerExample(); CronTriggerExample(); } }
Do Read
- AJAX implementation in Java web application using JQuery
- Gridview in Java Web Applications using jQuery DataTable plugin