Scheduling jobs in Java without using third party APIs (uses Timer and TimerTask classes)

This is a simple tutorial that shows how easily (and without depending on third party APIs) you can write a simple scheduler in Java. Java comes with build in capability for scheduling using java.util.Timer class and java.util.TimerTask class.

package com.kushal.tools;
/**
 * @Author Kushal Paudyal
 * Scheduling a task using Java in-house scheduler
 * Created : 2011/04/28
 * Last Modified: 2011/04/28
 */
import java.util.Timer;
import java.util.TimerTask;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;

public final class SchedulerUsingJavaUtil extends TimerTask {
	private final static long FREQUENCY_ONE_DAY = 1000 * 60 * 60 * 24;

	private final static int ONE_DAY = 0;
	private final static int HOUR_AM = 10;
	private final static int MINUTES = 52;

	/**
	 * Construct and use a TimerTask and Timer.
	 */
	public static void main(String[] arguments) {
		TimerTask scheduledTask = new SchedulerUsingJavaUtil();
		Timer timer = new Timer();
		/**
		 * Schedules the specified task for repeated fixed-rate execution,
		 * beginning at the specified time. Subsequent executions take place at
		 * approximately regular intervals, separated by the specified period.
		 * In fixed-rate execution, each execution is scheduled relative to the
		 * scheduled execution time of the initial execution. If an execution is
		 * delayed for any reason (such as garbage collection or other
		 * background activity), two or more executions will occur in rapid
		 * succession to "catch up." In the long run, the frequency of execution
		 * will be exactly the reciprocal of the specified period (assuming the
		 * system clock underlying Object.wait(long) is accurate).
		 *
		 * Fixed-rate execution is appropriate for recurring activities that are
		 * sensitive to absolute time, such as ringing a chime every hour on the
		 * hour, or running scheduled maintenance every day at a particular
		 * time. It is also appropriate for recurring activities where the total
		 * time to perform a fixed number of executions is important, such as a
		 * countdown timer that ticks once every second for ten seconds.
		 * Finally, fixed-rate execution is appropriate for scheduling multiple
		 * repeating timer tasks that must remain synchronized with respect to
		 * one another.
		 *
		 *
		 * Parameters:
		 * task - task to be scheduled.
		 * firstTime - First time at which task is to be executed.
		 * period - time in milliseconds between successive task executions.
		 * Throws: IllegalArgumentException - if
		 * time.getTime() is negative. IllegalStateException - if task was
		 * already scheduled or cancelled, timer was cancelled, or timer thread
		 * terminated.
		 */
		timer.scheduleAtFixedRate(scheduledTask, getFirstRunTime(),FREQUENCY_ONE_DAY);
	}

	/**
	 * Implements TimerTask's abstract run() method.
	 */
	public void run() {
		System.out.println("Doing some task..."+new Date());
	}

	/**
	 * Create a time when scheduler needs to run first
	 */
	private static Date getFirstRunTime() {
		/**
		 * Get Today's Calendar
		 */
		Calendar tomorrow = new GregorianCalendar();
		/**
		 * Add one day to get tomorrow's calendar
		 */
		tomorrow.add(Calendar.DATE, ONE_DAY);
		/**
		 * Set the scheduled time for tomorrow.
		 */
		Calendar firstRunTime = new GregorianCalendar(
				tomorrow.get(Calendar.YEAR),
				tomorrow.get(Calendar.MONTH),
				tomorrow.get(Calendar.DATE),
				HOUR_AM,
				MINUTES);

		return firstRunTime.getTime();
	}
}

[My Video Promotion]
Tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.