Sitemap
Haresh Gulvani

Haresh Gulvani

Programmer, Tech Enthusiast, Learner, Dreamer

Java Scheduler : Timer, Scheduler Executor & Quartz 

How to execute a job after some interval in java?

5 min readFeb 13, 2014

A job may used in standalone or more complex application like ERP or e-Commerce application where developers need to send email or messages daily or weekly or monthly.

Java gives developers a lot of options to put scheduler in their projects which can achieve the above functionality.

I just describe 3 major Scheduler library which can easily implement in java projects.

  • Quartz
  • Timer
  • Scheduler Executor / Java Concurrent
  1. What is Quartz?

Quartz is a richly featured, open source job scheduling library that can be integrated within virtually any Java application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may execute virtually anything you may program them to do. The Quartz Scheduler includes many enterprise-class features, such as support for JTA transactions and clustering.

Here is an example of Quartz Scheduler :

Prerequisite :

  1. Netbeans IDE
  2. JDK & JRE 1.6+
  3. Quartz libs download from http://quartz-scheduler.org/

Note : All the below examples will be created as a simple java application. you can also download the test application at the end of this blog.

Project Structure :

Press enter or click to view image in full size

QuartzTest.java

/* * To change this template, choose Tools | Templates * and open the template in the editor. */
package com.test.quartz;

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;

/** * * @author aayug */
public class QuartzTest {

public static void main(String[] args) throws SchedulerException { JobDetail job = JobBuilder.newJob(Task.class) .withIdentity("TestJob", "Task1").build(); Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("TestJobTrigger", "Task1") .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) .build(); Scheduler scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); scheduler.scheduleJob(job, trigger); }
}

Task.java

/* * To change this template, choose Tools | Templates * and open the template in the editor. */
package com.test.quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/** * * @author */
public class Task implements Job
{ public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Say Hello To Quartz in every 5 Seconds!"); }
}

Just run the QuartzTest.java file and it will start the scheduler and run the Task class in every 5 seconds.

Output :

Say Hello To Quartz in every 5 Seconds!
Say Hello To Quartz in every 5 Seconds!
Say Hello To Quartz in every 5 Seconds!

2. What is Java Timer?

JDK Timer is a simple scheduler for a specified task for repeated fixed-delay execution. To use this, you have to extends the TimerTask abstract class, override the run() method with your scheduler function.

Here is an example of JDK Timer :

Prerequisite :

  1. Netbeans IDE
  2. JDK & JRE 1.6+

Create a simple java application and two classes in the package just like the below project structure.

Get Haresh Gulvani’s stories in your inbox

Join Medium for free to get updates from this writer.

Project Structure :

Press enter or click to view image in full size

TimerTest.java

/* * To change this template, choose Tools | Templates * and open the template in the editor. */
package com.test.timer;

import java.util.Timer;
import java.util.TimerTask;

/** * * @author aayug */
public class TimerTest {

public static void main( String[] args ) { TimerTask task = new Task(); Timer timer = new Timer(); timer.schedule(task, 1000,5000); }
}

Task.java

/* * To change this template, choose Tools | Templates * and open the template in the editor. */
package com.test.timer;

import java.util.TimerTask;

/** * * @author aayug */
public class Task extends TimerTask {

@Override public void run() { System.out.println("Say Hello To Java Timer in every 5 Seconds!"); }

}

Just run the TimerTest.java file and it will start the scheduler and run the Task class in every 5 seconds.

Output :

Say Hello To Java Timer in every 5 Seconds!
Say Hello To Java Timer in every 5 Seconds!
Say Hello To Java Timer in every 5 Seconds!

3. What is Scheduler Executor in java.util.concurrent?

An Executor that can schedule commands to run after a given delay, or to execute periodically. This class is preferable to Timer when multiple worker threads are needed, or when the additional flexibility or capabilities of ThreadPoolExecutor (which this class extends) are required.

The schedule methods create tasks with various delays and return a task object that can be used to cancel or check execution. The scheduleAtFixedRate and scheduleWithFixedDelay methods create and execute tasks that run periodically until cancelled. Commands submitted using the execute method are scheduled with a requested delay of zero.

Here is an example of Java ScheduledExecutorService :

Prerequisite :

  1. Netbeans IDE
  2. JDK & JRE 1.6+

Create a simple java application and two classes in the package just like the below project structure.

Project Structure :

Press enter or click to view image in full size

ScheduledExecutorTest.java

/* * To change this template, choose Tools | Templates * and open the template in the editor. */
package com.test.Schedular;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/** * * @author aayug */
public class ScheduledExecutorTest {

private static ScheduledExecutorService scheduler; public static void main(String[] args) { scheduler = Executors.newSingleThreadScheduledExecutor(); scheduler.scheduleAtFixedRate(new Task(), 0, 5, TimeUnit.SECONDS); // Schedule to run every minute. }
}

Task.java

/* * To change this template, choose Tools | Templates * and open the template in the editor. */
package com.test.Schedular;

/** * * @author aayug */
public class Task implements Runnable {

@Override public void run() { System.out.println("Say Hello To Java Concurrent ScheduleExecuterService in every 5 Seconds!"); }

}

Just run the ScheduledExecutorTest.java file and it will start the scheduler and run the Task class in every 5 seconds.

Output :

Say Hello To Java Concurrent ScheduleExecuterService in every 5 Seconds!
Say Hello To Java Concurrent ScheduleExecuterService in every 5 Seconds!
Say Hello To Java Concurrent ScheduleExecuterService in every 5 Seconds!

Download all the above test application : Download

Note : this is my first blog / article on Medium, so may be the formatting will be not upto the mark. apologies for that.

References :

http://quartz-scheduler.org/

www.mkyong.com/

https://netbeans.org/

https://google.com

--

--

Haresh Gulvani
Haresh Gulvani

Written by Haresh Gulvani

Programmer, Tech Enthusiast, Learner, Dreamer