protected
void scheduleJob(final SchedulerJob aJob)
{
// Create a Job context (Quartz-Object).
final JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("data", aJob);
jobDataMap.put("spring-context", ctx);
jobDataMap.put("job-executer-service", jobEcecuterService);
// Create a Job execution detail (Quartz-Object).
final JobDetail jobDetail = new JobDetail(aJob.getId(),
null, DefaultSchedulerExecuter.class);
jobDetail.setJobDataMap(jobDataMap);
// Add the jod-definition to the scheduler.
try {
schedulerFactory.addJob(jobDetail, true);
// Create a Quartz trigger to start the job.
final CronTrigger trigger = new CronTrigger();
// Set the name to the id.
trigger.setName(aJob.getId());
trigger.setJobName(aJob.getId());
// Start now.
trigger.setStartTime(Calendar.getInstance().getTime());
// Schedule as specified in the cron expression.
trigger.setCronExpression(aJob.getCronExpression());
if(log.isInfoEnabled()) {
log.info("[SCHEDULER] - Scheduling Job: " +
aJob.getId() + " with executer " +
jobDetail.getJobClass().getCanonicalName());
}
// Schedule the new Job.
schedulerFactory.scheduleJob(trigger);
} catch (final Exception e) {
if(log.isDebugEnabled()) {
log.info("Error Scheduling Job " + aJob.getName() + ". Job deactivated.", e);
}
// Deactivate the Job, so it is not started automatically again.
aJob.setEnabled(false);
// Save the modified job.
try {
updateJob(aJob);
} catch(final Exception ex) {
throw new RuntimeException(ex);
}
}
}
protected
void unscheduleJob(final String aJobId)
{
try {
if(schedulerFactory.getJobDetail(
aJobId, "DEFAULT") != null) {
if(log.isInfoEnabled()) {
log.info("[SCHEDULER] - Unscheduling Job: " +
aJobId);
}
schedulerFactory.deleteJob(aJobId, "DEFAULT");
}
} catch (final SchedulerException e) {
throw new RuntimeException(
"Error unscheduling job with id " + aJobId, e);
}
}
|