com.github.siwenyan.cronjob.CronJobManager Maven / Gradle / Ivy
package com.github.siwenyan.cronjob;
import java.security.InvalidParameterException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.util.*;
public class CronJobManager {
public final static SimpleDateFormat formatTime = new SimpleDateFormat(
"HH:mm:ss");
public final static SimpleDateFormat formatDate = new SimpleDateFormat(
"yyyy-MM-dd");
private Map> cronJobs = new LinkedHashMap>();
private IWorker defaultWorker;
public CronJobManager() {
}
public CronJobManager(IWorker defaultWorker) {
this.defaultWorker = defaultWorker;
}
public IWorker getDefaultWorker() {
return defaultWorker;
}
public void setDefaultWorker(IWorker defaultWorker) {
this.defaultWorker = defaultWorker;
}
public void addTimerTask(IWorker worker, String cronJobName,
boolean available, boolean repeating,
Set repeatDaysOfWeek, Date scheduleDate, Date startTime,
T subject) {
if (worker == null) {
worker = this.defaultWorker;
}
if (worker == null) {
throw new InvalidParameterException("Parameter required: worker.");
}
if (cronJobName == null || cronJobName.trim().isEmpty()) {
throw new InvalidParameterException(
"Parameter required: cronJobName.");
} else {
cronJobName = cronJobName.trim();
}
if (cronJobs.containsKey(cronJobName)) {
throw new InvalidParameterException("Cron Job already exists: "
+ cronJobName + ".");
}
if (startTime == null) {
throw new InvalidParameterException(
"Parameter required: startTime.");
}
if (!repeating && scheduleDate == null) {
throw new InvalidParameterException(
"Parameter required: scheduleDate.");
}
if (subject == null) {
throw new InvalidParameterException("Parameter required: subject.");
}
CronJob cronJob = new CronJob(worker);
cronJob.setCronJobName(cronJobName);
cronJob.setAvailable(available);
cronJob.setRepeating(repeating);
cronJob.setRepeatOn(repeatDaysOfWeek, true);
cronJob.setScheduleDate(scheduleDate);
cronJob.setStartTime(startTime);
cronJob.setSubject(subject);
this.newCronJob(cronJob);
}
private void newCronJob(CronJob cronJob) {
if (cronJob != null) {
cronJobs.put(cronJob.getCronJobName(), cronJob);
cronJob.resetAlarm();
}
}
public String toString(String name) {
CronJob cronJob = this.cronJobs.get(name);
if (cronJob != null) {
return cronJob.toString() + "\r\n"
+ cronJob.getSubject().toString();
} else {
return "";
}
}
@Override
public String toString() {
String display = "";
for (String key : this.cronJobs.keySet()) {
display += "\r\n" + this.cronJobs.get(key).toString();
}
if (display.isEmpty()) {
display = "No schedule.";
}
return display;
}
public boolean contains(String name) {
return this.cronJobs.containsKey(name);
}
public void cancel(String name) {
CronJob cronJob = this.cronJobs.get(name);
if (cronJob != null) {
cronJob.setAvailable(false);
this.cronJobs.remove(name);
}
}
public static Set parseDaysOfWeek(String sRepeatOnDaysOfWeek) {
Set daysOfWeek = new HashSet();
if (sRepeatOnDaysOfWeek == null || sRepeatOnDaysOfWeek.isEmpty()) {
return daysOfWeek;
} else {
String[] sDaysOfWeek = sRepeatOnDaysOfWeek.split(":");
for (String sDayOfWeek : sDaysOfWeek) {
daysOfWeek.add(DayOfWeek.valueOf(sDayOfWeek));
}
}
return daysOfWeek;
}
public void cancelAll() {
for (String name : this.cronJobs.keySet()) {
cancel(name);
}
}
}