Date startTime = null;;
Date endTime = null;
Date curTime = new Date();
int repeat=0;
long interval=0;
Trigger trigger = null;
JobDetail jobDetail = null;
while (rs.next()) {
curTask = rs.getString("TASK_ID");
taskJob = rs.getString("JOB_CLASS");
taskArgs = rs.getString("ARGUMENTS");
if (taskArgs == null)
{
taskArgs = "";
}
taskTrigger = rs.getString("TRIGGER_TYPE");
//verbose
logger.info("Task " + curTask);
logger.info("Task " + curTask + " - Job Class = " + taskJob);
logger.info("Task " + curTask + " - Trigger Type = " + taskTrigger);
logger.info("Task " + curTask + " - Arguments = " + taskArgs);
//validation
if (taskJob.length() == 0 || taskTrigger.length() == 0)
{
logger.error("You must provide both Job class and Trigger type for task "
+ curTask + "!");
continue;
}
//get the trigger setting
if (taskTrigger.compareToIgnoreCase("CronTrigger") == 0)
{
taskCron = rs.getString("TRIGGER_CRON");
if (null == taskCron) {
taskCron = DEF_CRONSTRING;
} else {
taskCron = taskCron.trim();
}
isSimpleTrigger = false;
//verbose
logger.info("Task " + curTask + " - Cron Value = " + taskCron);
}
else if (taskTrigger.compareToIgnoreCase("SimpleTrigger") == 0)
{
taskStartTime = rs.getDate("TRIGGER_START");
if (taskStartTime == null)
{
taskStartTime = new Date();
}
taskEndTime = rs.getDate("TRIGGER_END");
taskRepeat = rs.getInt("TRIGGER_REPEAT");
taskInterval = rs.getInt("TRIGGER_INTERVAL");
isSimpleTrigger = true;
//verbose
logger.info("Task " + curTask + " - Start Time = " + taskStartTime);
logger.info("Task " + curTask + " - End Time = " + taskEndTime);
logger.info("Task " + curTask + " - Repeat Count = " + taskRepeat);
logger.info("Task " + curTask + " - Interval = " + taskInterval);
if (taskEndTime != null &&
(curTime.after(taskEndTime) || !taskStartTime.before(taskEndTime)))
{
logger.warn("Task " + curTask + " is OBSOLETE! Ignored.");
continue;
}
}
else
{
logger.error("Invalid trigger type ("+ taskTrigger + ") for task " + curTask + "!");
continue;
}
//interpret parameters
try
{
jobClass = Class.forName(taskJob);
if (jobClass == null)
{
logger.error("Invalid job class (" + taskJob + ") for task " + curTask + "!");
continue;
}
}
catch (ClassNotFoundException e)
{
logger.error("Invalid job class (" + taskJob + ") for task " + curTask + "!");
continue;
}
catch (NoClassDefFoundError e)
{
//NOTE: NoClassDefFoundError does not seem to extend Exception
logger.error("Can not load class " + taskJob + ". Exception: " + e.toString());
continue;
}
catch (Exception e)
{
logger.error("Can not load class " + taskJob + ". Exception: " + e.toString());
continue;
}
if (isSimpleTrigger)
{
//delay the execution until the agent is properly started
startTime = new Date();
startTime.setTime(startTime.getTime()+EXECUTION_DELAY_MILLIS);
if (taskStartTime != null && taskStartTime.after(startTime)) {
startTime = taskStartTime;
}
endTime = taskEndTime;
if (taskRepeat < 0) {
repeat = SimpleTrigger.REPEAT_INDEFINITELY;
} else {
repeat = taskRepeat;
}
if (taskInterval < 0) {
interval = 0;
} else {
interval = taskInterval;
}
}
//create trigger
logger.info("Task " + curTask + " - Creating trigger...");
trigger = null;
if (isSimpleTrigger) {
try {
trigger = new SimpleTrigger(curTask + "_Trigger", null, startTime, endTime, repeat, interval);
} catch (Exception e) {
logger.error("Cannot create trigger for task " + curTask + "!");
logger.error("Exception: "+e.getMessage());
continue;
}
} else {
try {
trigger = new CronTrigger(curTask + "_Trigger", null, taskCron);
} catch (ParseException e) {
logger.error("Invalid cron epression (" + taskCron + ") for task " + curTask + "!");
continue;
}
}
trigger.getJobDataMap().put(SCHEDULER_ARG_STRING, this);
trigger.getJobDataMap().put(TASK_ARG_STRING, curTask);
trigger.getJobDataMap().put(ARGUMENT_ARG_STRING,taskArgs);
trigger.setGroup(taskGroup);
//create job
logger.info("Task " + curTask + " - Creating job...");
jobDetail = null;
try {