Today we will be discussing about Quartz Scheduler in Java. For that we are going to need a couple of Quartz jars. But, As I will be developing the project using Maven Build tools, I am not downloading it.
So, let's start with creating a Maven project for Quartz.
Step 1 : Create a Maven Project.
a. Set up Maven in your eclipse. If you don't know how to set up Maven in Eclipse, have a look at this tutorial - Setup Maven in Eclipse .
b. Create a new Maven Project with whatever name you want. If you don't know how to create a
Maven project in Eclipse, visit this link - Create a simple Maven project
Now, your maven project is ready to implement a Quartz Scheduler. But, for that, we need to add a couple of dependencies in our pom.xml.
Step 2: Add dependencies for Quartz in pom.xml.
Add the following dependencies in your pom.xml.
1) The class which is a Job. This class implements Job interface.
And
2) The main class which contains public static void main(String args[]) and calls the Job every 1 second.
Step 3: Create Job Class. We are here trying to create a Job which prints a simple string , e.g - "My First Quartz scheduler".
This execute method is actually a method from Job Interface which we have implemented in our own way.
Step 4: Create a Main Class. This class will call the Job to execute after every 1 second.
This is the code fragment where we mention the frequency of the scheduler to call the Job -
Now, let's do maven compile & run our code.
The code will print the String "My FIrst Quartz Scheduler" in the console every 1 second like this -
My First Quartz Scheduler
My First Quartz Scheduler
My First Quartz Scheduler
My First Quartz Scheduler
So, let's start with creating a Maven project for Quartz.
Step 1 : Create a Maven Project.
a. Set up Maven in your eclipse. If you don't know how to set up Maven in Eclipse, have a look at this tutorial - Setup Maven in Eclipse .
b. Create a new Maven Project with whatever name you want. If you don't know how to create a
Maven project in Eclipse, visit this link - Create a simple Maven project
Now, your maven project is ready to implement a Quartz Scheduler. But, for that, we need to add a couple of dependencies in our pom.xml.
Step 2: Add dependencies for Quartz in pom.xml.
Add the following dependencies in your pom.xml.
Now, we need to create two classes.org.quartz-scheduler quartz 2.2.1 org.quartz-scheduler quartz-jobs 2.2.1
1) The class which is a Job. This class implements Job interface.
And
2) The main class which contains public static void main(String args[]) and calls the Job every 1 second.
Step 3: Create Job Class. We are here trying to create a Job which prints a simple string , e.g - "My First Quartz scheduler".
import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class MyQuartzJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("My First Quartz Scheduler"); } }
This execute method is actually a method from Job Interface which we have implemented in our own way.
Step 4: Create a Main Class. This class will call the Job to execute after every 1 second.
import org.quartz.CronScheduleBuilder; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class MainClass { public static void main(String[] args) throws SchedulerException { JobDetail job = JobBuilder.newJob(MyQuartzJob.class)//mention the Job Class Name here .build(); //create schedule builder CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/1 * * * * ?"); //create trigger which the schedule Builder Trigger trigger = TriggerBuilder .newTrigger() .withSchedule(scheduleBuilder) .build(); //create scheduler Scheduler scheduler = new StdSchedulerFactory().getScheduler(); // start your scheduler scheduler.start(); // let the scheduler call the Job using trigger scheduler.scheduleJob(job, trigger); } }
This is the code fragment where we mention the frequency of the scheduler to call the Job -
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/1 * * * * ?");("0/1 * * * * ?") is the cron expression for the frequency. For creating the schedules like this or may for customizing this, you can refer to this site - cronmaker.com.
Now, let's do maven compile & run our code.
The code will print the String "My FIrst Quartz Scheduler" in the console every 1 second like this -
My First Quartz Scheduler
My First Quartz Scheduler
My First Quartz Scheduler
My First Quartz Scheduler
......
Download the sample project in .zip here