use of org.quartz.Calendar in project pentaho-platform by pentaho.
the class QuartzScheduler method setJobTrigger.
private void setJobTrigger(Scheduler scheduler, Job job, Trigger trigger) throws SchedulerException, org.quartz.SchedulerException {
QuartzJobKey jobKey = QuartzJobKey.parse(job.getJobId());
String groupName = jobKey.getUserName();
if (trigger instanceof SimpleTrigger) {
SimpleTrigger simpleTrigger = (SimpleTrigger) trigger;
SimpleJobTrigger simpleJobTrigger = new SimpleJobTrigger();
simpleJobTrigger.setStartTime(simpleTrigger.getStartTime());
simpleJobTrigger.setEndTime(simpleTrigger.getEndTime());
simpleJobTrigger.setUiPassParam((String) job.getJobParams().get(RESERVEDMAPKEY_UIPASSPARAM));
long interval = simpleTrigger.getRepeatInterval();
if (interval > 0) {
interval /= 1000;
}
simpleJobTrigger.setRepeatInterval(interval);
simpleJobTrigger.setRepeatCount(simpleTrigger.getRepeatCount());
job.setJobTrigger(simpleJobTrigger);
} else if (trigger instanceof CronTrigger) {
CronTrigger cronTrigger = (CronTrigger) trigger;
ComplexJobTrigger complexJobTrigger = createComplexTrigger(cronTrigger.getCronExpression());
complexJobTrigger.setUiPassParam((String) job.getJobParams().get(RESERVEDMAPKEY_UIPASSPARAM));
complexJobTrigger.setCronString(((CronTrigger) trigger).getCronExpression());
job.setJobTrigger(complexJobTrigger);
if (trigger.getCalendarName() != null) {
Calendar calendar = scheduler.getCalendar(trigger.getCalendarName());
if (calendar instanceof QuartzSchedulerAvailability) {
QuartzSchedulerAvailability quartzSchedulerAvailability = (QuartzSchedulerAvailability) calendar;
complexJobTrigger.setStartTime(quartzSchedulerAvailability.getStartTime());
complexJobTrigger.setEndTime(quartzSchedulerAvailability.getEndTime());
}
}
complexJobTrigger.setCronString(((CronTrigger) trigger).getCronExpression());
}
int triggerState = scheduler.getTriggerState(job.getJobId(), groupName);
switch(triggerState) {
case Trigger.STATE_NORMAL:
job.setState(JobState.NORMAL);
break;
case Trigger.STATE_BLOCKED:
job.setState(JobState.BLOCKED);
break;
case Trigger.STATE_COMPLETE:
job.setState(JobState.COMPLETE);
break;
case Trigger.STATE_ERROR:
job.setState(JobState.ERROR);
break;
case Trigger.STATE_PAUSED:
job.setState(JobState.PAUSED);
break;
default:
job.setState(JobState.UNKNOWN);
break;
}
job.setJobName(QuartzJobKey.parse(job.getJobId()).getJobName());
job.setNextRun(trigger.getNextFireTime());
job.setLastRun(trigger.getPreviousFireTime());
}
use of org.quartz.Calendar in project pentaho-platform by pentaho.
the class QuartzScheduler method updateJob.
@Override
public void updateJob(String jobId, Map<String, Serializable> jobParams, IJobTrigger trigger) throws SchedulerException {
QuartzJobKey jobKey = QuartzJobKey.parse(jobId);
Trigger quartzTrigger = createQuartzTrigger(trigger, jobKey);
quartzTrigger.setJobName(jobId);
quartzTrigger.setJobGroup(jobKey.getUserName());
Calendar triggerCalendar = quartzTrigger instanceof CronTrigger ? createQuartzCalendar((ComplexJobTrigger) trigger) : null;
try {
Scheduler scheduler = getQuartzScheduler();
// int triggerState = scheduler.getTriggerState(jobId, jobKey.getUserName());
// if (triggerState != Trigger.STATE_PAUSED) {
// scheduler.pauseTrigger(jobId, jobKey.getUserName());
// }
JobDetail origJobDetail = scheduler.getJobDetail(jobId, jobKey.getUserName());
if (origJobDetail.getJobDataMap().containsKey(RESERVEDMAPKEY_ACTIONCLASS)) {
jobParams.put(RESERVEDMAPKEY_ACTIONCLASS, origJobDetail.getJobDataMap().get(RESERVEDMAPKEY_ACTIONCLASS).toString());
} else if (origJobDetail.getJobDataMap().containsKey(RESERVEDMAPKEY_ACTIONID)) {
jobParams.put(RESERVEDMAPKEY_ACTIONID, origJobDetail.getJobDataMap().get(RESERVEDMAPKEY_ACTIONID).toString());
}
if (origJobDetail.getJobDataMap().containsKey(RESERVEDMAPKEY_STREAMPROVIDER)) {
jobParams.put(RESERVEDMAPKEY_STREAMPROVIDER, (Serializable) origJobDetail.getJobDataMap().get(RESERVEDMAPKEY_STREAMPROVIDER));
}
if (origJobDetail.getJobDataMap().containsKey(RESERVEDMAPKEY_UIPASSPARAM)) {
jobParams.put(RESERVEDMAPKEY_UIPASSPARAM, (Serializable) origJobDetail.getJobDataMap().get(RESERVEDMAPKEY_UIPASSPARAM));
}
JobDetail jobDetail = createJobDetails(jobKey, jobParams);
scheduler.addJob(jobDetail, true);
if (triggerCalendar != null) {
scheduler.addCalendar(jobId.toString(), triggerCalendar, true, true);
quartzTrigger.setCalendarName(jobId.toString());
}
if (quartzTrigger instanceof CronTrigger) {
Serializable timezone = jobParams.get("timezone");
if (timezone != null) {
setTimezone((CronTrigger) quartzTrigger, timezone.toString());
}
}
scheduler.rescheduleJob(jobId, jobKey.getUserName(), quartzTrigger);
// if (triggerState != Trigger.STATE_PAUSED) {
// scheduler.resumeTrigger(jobId, jobKey.getUserName());
// }
logger.debug(MessageFormat.format("Scheduling job {0} with trigger {1} and job parameters [ {2} ]", jobId.toString(), trigger, // $NON-NLS-1$
prettyPrintMap(jobParams)));
} catch (org.quartz.SchedulerException e) {
throw new SchedulerException(Messages.getInstance().getString("QuartzScheduler.ERROR_0001_FAILED_TO_SCHEDULE_JOB", jobKey.getJobName()), // $NON-NLS-1$
e);
}
}
use of org.quartz.Calendar in project weicoder by wdcode.
the class JobStoreSupport method triggerFired.
protected TriggerFiredBundle triggerFired(Connection conn, OperableTrigger trigger) throws JobPersistenceException {
JobDetail job;
Calendar cal = null;
// Make sure trigger wasn't deleted, paused, or completed...
try {
// if trigger was deleted, state will be STATE_DELETED
String state = getDelegate().selectTriggerState(conn, trigger.getKey());
if (!state.equals(STATE_ACQUIRED)) {
return null;
}
} catch (SQLException e) {
throw new JobPersistenceException("Couldn't select trigger state: " + e.getMessage(), e);
}
try {
job = retrieveJob(conn, trigger.getJobKey());
if (job == null) {
return null;
}
} catch (JobPersistenceException jpe) {
try {
getLog().error("Error retrieving job, setting trigger state to ERROR.", jpe);
getDelegate().updateTriggerState(conn, trigger.getKey(), STATE_ERROR);
} catch (SQLException sqle) {
getLog().error("Unable to set trigger state to ERROR.", sqle);
}
throw jpe;
}
if (trigger.getCalendarName() != null) {
cal = retrieveCalendar(conn, trigger.getCalendarName());
if (cal == null) {
return null;
}
}
try {
getDelegate().updateFiredTrigger(conn, trigger, STATE_EXECUTING, job);
} catch (SQLException e) {
throw new JobPersistenceException("Couldn't insert fired trigger: " + e.getMessage(), e);
}
Date prevFireTime = trigger.getPreviousFireTime();
// call triggered - to update the trigger's next-fire-time state...
trigger.triggered(cal);
String state = STATE_WAITING;
boolean force = true;
if (job.isConcurrentExectionDisallowed()) {
state = STATE_BLOCKED;
force = false;
try {
getDelegate().updateTriggerStatesForJobFromOtherState(conn, job.getKey(), STATE_BLOCKED, STATE_WAITING);
getDelegate().updateTriggerStatesForJobFromOtherState(conn, job.getKey(), STATE_BLOCKED, STATE_ACQUIRED);
getDelegate().updateTriggerStatesForJobFromOtherState(conn, job.getKey(), STATE_PAUSED_BLOCKED, STATE_PAUSED);
} catch (SQLException e) {
throw new JobPersistenceException("Couldn't update states of blocked triggers: " + e.getMessage(), e);
}
}
if (trigger.getNextFireTime() == null) {
state = STATE_COMPLETE;
force = true;
}
storeTrigger(conn, trigger, job, true, state, force, false);
job.getJobDataMap().clearDirtyFlag();
return new TriggerFiredBundle(job, trigger, cal, trigger.getKey().getGroup().equals(Scheduler.DEFAULT_RECOVERY_GROUP), new Date(), trigger.getPreviousFireTime(), prevFireTime, trigger.getNextFireTime());
}
use of org.quartz.Calendar in project weicoder by wdcode.
the class ExecutingJobsManager method rescheduleJob.
/**
* <p>
* Remove (delete) the <code>{@link org.quartz.Trigger}</code> with the
* given name, and store the new given one - which must be associated
* with the same job.
* </p>
* @param newTrigger
* The new <code>Trigger</code> to be stored.
*
* @return <code>null</code> if a <code>Trigger</code> with the given
* name & group was not found and removed from the store, otherwise
* the first fire time of the newly scheduled trigger.
*/
public Date rescheduleJob(TriggerKey triggerKey, Trigger newTrigger) throws SchedulerException {
validateState();
if (triggerKey == null) {
throw new IllegalArgumentException("triggerKey cannot be null");
}
if (newTrigger == null) {
throw new IllegalArgumentException("newTrigger cannot be null");
}
OperableTrigger trig = (OperableTrigger) newTrigger;
Trigger oldTrigger = getTrigger(triggerKey);
if (oldTrigger == null) {
return null;
} else {
trig.setJobKey(oldTrigger.getJobKey());
}
trig.validate();
Calendar cal = null;
if (newTrigger.getCalendarName() != null) {
cal = resources.getJobStore().retrieveCalendar(newTrigger.getCalendarName());
}
Date ft = trig.computeFirstFireTime(cal);
if (ft == null) {
throw new SchedulerException("Based on configured schedule, the given trigger will never fire.");
}
if (resources.getJobStore().replaceTrigger(triggerKey, trig)) {
notifySchedulerThread(newTrigger.getNextFireTime().getTime());
notifySchedulerListenersUnscheduled(triggerKey);
notifySchedulerListenersSchduled(newTrigger);
} else {
return null;
}
return ft;
}
use of org.quartz.Calendar in project weicoder by wdcode.
the class ExecutingJobsManager method scheduleJobs.
public void scheduleJobs(Map<JobDetail, Set<? extends Trigger>> triggersAndJobs, boolean replace) throws SchedulerException {
validateState();
// make sure all triggers refer to their associated job
for (Entry<JobDetail, Set<? extends Trigger>> e : triggersAndJobs.entrySet()) {
JobDetail job = e.getKey();
if (// there can be one of these (for adding a bulk set of triggers for pre-existing jobs)
job == null)
continue;
Set<? extends Trigger> triggers = e.getValue();
if (// this is possible because the job may be durable, and not yet be having triggers
triggers == null)
continue;
for (Trigger trigger : triggers) {
OperableTrigger opt = (OperableTrigger) trigger;
opt.setJobKey(job.getKey());
opt.validate();
Calendar cal = null;
if (trigger.getCalendarName() != null) {
cal = resources.getJobStore().retrieveCalendar(trigger.getCalendarName());
if (cal == null) {
throw new SchedulerException("Calendar '" + trigger.getCalendarName() + "' not found for trigger: " + trigger.getKey());
}
}
Date ft = opt.computeFirstFireTime(cal);
if (ft == null) {
throw new SchedulerException("Based on configured schedule, the given trigger will never fire.");
}
}
}
resources.getJobStore().storeJobsAndTriggers(triggersAndJobs, replace);
notifySchedulerThread(0L);
for (JobDetail job : triggersAndJobs.keySet()) {
notifySchedulerListenersJobAdded(job);
Set<? extends Trigger> triggers = triggersAndJobs.get(job);
for (Trigger trigger : triggers) {
notifySchedulerListenersSchduled(trigger);
}
}
}
Aggregations