Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Creative commons Attribution-NonCommercial license. http://creativecommons.org/licenses/by-nc/2.5/au/deed.en_GB NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
  
  package base.task;
  
 
 
 public class ScheduledTaskHandler extends TaskHandler {

Which days of the month to run this task. empty list means task can run on every day of month.
 
 	private int[] daysOfMonth;

Which days of the week to run this task. empty list means task can run on every of the week.
 
 	private int[] daysOfWeek;

Which hours of the day to run this task. Empty list means task can run every hour.
 
 	private int[] hoursOfDay;

The last day this task was run on
 
 	private int lastDayOfYear = -1;

The last hour this task was run on
 
 	private int lastHour = -1;
 
 	private Settings settings = null;

Initialise a scheduled task. Includes logic to prevent a task from being executed more than once within an hour.

Parameters:
taskName
task
daysOfMonth Days of month this task may run, or null for every day.
daysOfWeek Days of week this task may run, or null for every day.
hoursOfDay Hours of the day this task may run, or null for every hour.
settings If not null, a setting will be set to track when the task was last run, this prevents the task being accidentally run more than once post JVM restart.
Throws:
java.io.IOException
 
 	public ScheduledTaskHandler(String taskNameTask taskint[] daysOfMonthint[] daysOfWeekint[] hoursOfDaySettings settingsthrows IOException {
 		super(taskNametask);
 		this. = daysOfMonth;
 		this. = daysOfWeek;
 		this. = hoursOfDay;
 		this. = settings;
 
 		if(this. == null) {
 			this. = new int[]{};
 		}
 		if(this. == null) {
 			this. = new int[]{};
 		}
 		if(this. == null) {
 			this. = new int[]{};
 		}
 
 		if(settings != null) {
 			String stat = settings.get("task."+taskName+".last_run"null);
 			if(stat != null && stat.contains("|")) {
 				String[] parts = stat.split("\\|");
 				 = Integer.parseInt(parts[0]);
 				 = Integer.parseInt(parts[1]);
 			}
 		}
 	}
 
 	public void run() {
 
 		try {
 			sleep(5 * 1000); // Don't do anything instantly
 		} catch(InterruptedException e) {
 		}
 
 		while(!) {
 			try {
 				if(runNow()) {
 					.run();
 				}
 				sleep(5 * 60 * 1000); // Sleep for 5 minutes
 			} catch(InterruptedException e) {
 			} catch(Exception e) {
 				try { sleep(3 * 1000); } catch (InterruptedException ex) {}
 			}
 		}
 
 	}

Returns true if:
  • this is an hour that the scheduled task should run
  • the task has not already run during this hour
This code is a bit of a hack to get something up and running quickly. It works, it is inefficient and could be improved.

	private boolean runNow() throws IOException {
		Calendar date = new GregorianCalendar();
		// If daysOfMonth contains days and this day is not one of them, go back to sleep.
		boolean dayOfMonthMatch = false;
		for(int day : ) {
			if(date.get(.) == day) {
				dayOfMonthMatch = true;
				break;
			}
		}
		if(. > 0 && !dayOfMonthMatch) {
			..println(this.getName() + " Current day_of_month "+date.get(.)+" is not in " + Arrays.toString());
			return false;
		}
		// If daysOfWeek contains days and this day is not one of them, go back to sleep.
		boolean dayOfWeekMatch = false;
		for(int day : ) {
			if(date.get(.) == day) {
				dayOfWeekMatch = true;
				break;
			}
		}
		if(. > 0 && !dayOfWeekMatch) {
			..println(this.getName() + " Current day_of_week "+date.get(.)+" is not in " + Arrays.toString());
			return false;
		}
		// If hoursOfDay contains hours and this hour is not one of them, go back to sleep.
		boolean hourOfDayMatch = false;
		for(int hour : ) {
			if(date.get(.) == hour) {
				hourOfDayMatch = true;
				break;
			}
		}
		if(. > 0 && !hourOfDayMatch) {
			..println(this.getName() + " Current hours_of_day "+date.get(.)+" is not in " + Arrays.toString());
			return false;
		}
		// Don't run if we already run in this day/hour
			..println(this.getName() + " already ran in this hour on this day of the week.");
			return false;
		}
		if( != null) {
			// If we can't persist the last run setting, we throw an exception. We don't want
			// execution of a task if we can't track when it was run. Otherwise tasks may
			// accidentally run twice.
			.set("task."+getName()+".last_run" + "|" + );
		}
		return true;
	}
New to GrepCode? Check out our FAQ X