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 TaskManager {
	private static Map<StringTaskHandlertaskHandlers = new HashMap<>();

Register that a task should be executed regularly at a specific interval.

Parameters:
name Unique name identifying the task
interval How frequently the task should be executed in seconds
task The task that should be executed regularly
	public static void registerRegularTask(String nameint intervalTask task) {
			throw new IllegalStateException("Attempted to register a task named " + name + " when a task with this name already exists.");
		}
		RegularTaskHandler handler = new RegularTaskHandler(nametaskinterval);
		.put(namehandler);
		handler.start();
	}

Register that a task should be executed regularly at a specific interval.

Parameters:
name Unique name identifying the task
daysOfMonth Which days of the month this task will run (null/empty means every day)
daysOfWeek Which days of the week this task will run (null/empty means every day)
hoursOfDay Which hours of the day this task will run (null/empty means every hour)
task The task that should be executed regularly
Throws:
java.io.IOException
	public static void registerScheduledTask(String nameint[]daysOfMonthint[] daysOfWeekint[] hoursOfDayTask taskSettings settingsthrows IOException {
			throw new IllegalStateException("Attempted to register a task named " + name + " when a task with this name already exists.");
		}
		ScheduledTaskHandler handler = new ScheduledTaskHandler(nametaskdaysOfMonthdaysOfWeekhoursOfDaysettings);
		.put(namehandler);
		handler.start();
	}
	public static void unregisterRegularTask(String namethrows InterruptedException {
		TaskHandler handler = null;
			handler = .remove(name);
			handler.shutdown();
			handler.join();
		}
	}

Called when either java process is being terminated, to clean up any Task's that are still scheduled to be executed.
	public static void shutdown() throws InterruptedException {
		for(TaskHandler handler : .values()) {
			handler.shutdown();
			handler.join();
		}
	}
New to GrepCode? Check out our FAQ X