Schedulers are important for running the batch process in your application. There are plenty of Java scheduler frameworks available in the market. Among them, Quartz Scheduler is the most popular and most widely used by Java projects. This tutorial provides very simple example to write Quartz scheduler. When you complete reading this tutorial, you will be able to write your won quartz scheduler. If you have any questions, please write it in the comments section.
1. SetUp Quartz Scheduler
Get the quartz JSR files from here. Also you would need slf4j-api-1.7.5.jar to run the quartz examples. Quartz is internally using this API for the logging purposes. Add these files to the classpath and imprt to the eclipse environment. I assume that you have already created a Java project in eclipse.
2. Create A Quartz Job
As a first step, implement org.quartz.Job interface and write your own Job. You must override the method execute which will be invoked for the batch process. The below code snippet is the simple example for writing a Job.
FirstJob.java
package javabeat.net.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class FirstJob implements Job{ @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("This is my first quartz job!!"); } }3. Register Quartz Scheduler
This is the final step where we initialize the Quartz scheduler. The following are the steps to initialize this scheduler.
- Create scheduler factory which is required for getting the scheduler instance.
- Get the scheduler instance from the factory.
- Create the JobDetailImpl instance and register our FirstJob to that object.
- Create and set the values for trigger. Trigger is very important for setting the details about the frequecny of batch process.
- Start the scheduler
- Schedule the Job by linking our Job and trigger.
- Your Quartz scheduler is ready and running.
QuartzExample.java
package javabeat.net.quartz; import java.util.Date; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.impl.JobDetailImpl; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.triggers.SimpleTriggerImpl; public class QuartzExample { public static void main(String args[]) throws SchedulerException{ //Creating scheduler factory and scheduler SchedulerFactory factory = new StdSchedulerFactory(); Scheduler scheduler = factory.getScheduler(); //Creating Job and link to our Job class JobDetailImpl jobDetail = new JobDetailImpl(); jobDetail.setName("First Job"); jobDetail.setJobClass(FirstJob.class); //Creating schedule time with trigger SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl(); simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000)); simpleTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); simpleTrigger.setRepeatInterval(2000); simpleTrigger.setName("FirstTrigger"); //Start scheduler scheduler.start(); scheduler.scheduleJob(jobDetail,simpleTrigger); } }