Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   This is free and unencumbered software released into the public domain.
  
   Anyone is free to copy, modify, publish, use, compile, sell, or
   distribute this software, either in source code form or as a compiled
   binary, for any purpose, commercial or non-commercial, and by any
   means.
  
   In jurisdictions that recognize copyright laws, the author or authors
  of this software dedicate any and all copyright interest in the
  software to the public domain. We make this dedication for the benefit
  of the public at large and to the detriment of our heirs and
  successors. We intend this dedication to be an overt act of
  relinquishment in perpetuity of all present and future rights to this
  software under copyright law.
 
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
  */
 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()) {
				}
				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) {
			//System.out.println(this.getName() + " Current day_of_month "+date.get(Calendar.DAY_OF_MONTH)+" is not in " + Arrays.toString(daysOfMonth));
			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) {
			//System.out.println(this.getName() + " Current day_of_week "+date.get(Calendar.DAY_OF_WEEK)+" is not in " + Arrays.toString(daysOfWeek));
			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) {
			//System.out.println(this.getName() + " Current hours_of_day "+date.get(Calendar.HOUR_OF_DAY)+" is not in " + Arrays.toString(hoursOfDay));
			return false;
		}
		// Don't run if we already run in this day/hour
			//System.out.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