/* * Copyright 2001-2009 James House * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * */ /* * Previously Copyright (c) 2001-2004 James House */ package org.quartz; import java.util.Date; import java.util.HashMap; import org.quartz.spi.TriggerFiredBundle; /** *
* A context bundle containing handles to various environment information, that
* is given to a {@link org.quartz.JobDetail} instance as it is
* executed, and to a {@link Trigger} instance after the
* execution completes.
*
* The JobDataMap found on this object (via the
* getMergedJobDataMap() method) serves as a convenience -
* it is a merge of the JobDataMap found on the
* JobDetail and the one found on the Trigger, with
* the value in the latter overriding any same-named values in the former.
* It is thus considered a 'best practice' that the execute code of a Job
* retrieve data from the JobDataMap found on this object NOTE: Do not
* expect value 'set' into this JobDataMap to somehow be set back onto a
* StatefulJob's own JobDataMap.
*
* JobExecutionContext s are also returned from the
* Scheduler.getCurrentlyExecutingJobs()
* method. These are the same instances as those passed into the jobs that are
* currently executing within the scheduler. The exception to this is when your
* application is using Quartz remotely (i.e. via RMI) - in which case you get
* a clone of the JobExecutionContexts, and their references to
* the Scheduler and Job instances have been lost (a
* clone of the JobDetail is still available - just not a handle
* to the job instance that is running).
*
* Create a JobExcecutionContext with the given context data. *
*/ public JobExecutionContext(Scheduler scheduler, TriggerFiredBundle firedBundle, Job job) { this.scheduler = scheduler; this.trigger = firedBundle.getTrigger(); this.calendar = firedBundle.getCalendar(); this.jobDetail = firedBundle.getJobDetail(); this.job = job; this.recovering = firedBundle.isRecovering(); this.fireTime = firedBundle.getFireTime(); this.scheduledFireTime = firedBundle.getScheduledFireTime(); this.prevFireTime = firedBundle.getPrevFireTime(); this.nextFireTime = firedBundle.getNextFireTime(); this.jobDataMap = new JobDataMap(); this.jobDataMap.putAll(jobDetail.getJobDataMap()); this.jobDataMap.putAll(trigger.getJobDataMap()); } /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Interface. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /** *
* Get a handle to the Scheduler instance that fired the
* Job.
*
* Get a handle to the Trigger instance that fired the
* Job.
*
* Get a handle to the Calendar referenced by the Trigger
* instance that fired the Job.
*
* If the Job is being re-executed because of a 'recovery'
* situation, this method will return true.
*
* Get the convenience JobDataMap of this execution context.
*
* The JobDataMap found on this object serves as a convenience -
* it is a merge of the JobDataMap found on the
* JobDetail and the one found on the Trigger, with
* the value in the latter overriding any same-named values in the former.
* It is thus considered a 'best practice' that the execute code of a Job
* retrieve data from the JobDataMap found on this object.
*
NOTE: Do not
* expect value 'set' into this JobDataMap to somehow be set back onto a
* StatefulJob's own JobDataMap.
*
* Attempts to change the contents of this map typically result in an
* IllegalStateException.
*
* Get the JobDetail associated with the Job.
*
* Get the instance of the Job that was created for this
* execution.
*
* Note: The Job instance is not available through remote scheduler * interfaces. *
*/ public Job getJobInstance() { return job; } /** * The actual time the trigger fired. For instance the scheduled time may * have been 10:00:00 but the actual fire time may have been 10:00:03 if * the scheduler was too busy. * * @return Returns the fireTime. * @see #getScheduledFireTime() */ public Date getFireTime() { return fireTime; } /** * The scheduled time the trigger fired for. For instance the scheduled * time may have been 10:00:00 but the actual fire time may have been * 10:00:03 if the scheduler was too busy. * * @return Returns the scheduledFireTime. * @see #getFireTime() */ public Date getScheduledFireTime() { return scheduledFireTime; } public Date getPreviousFireTime() { return prevFireTime; } public Date getNextFireTime() { return nextFireTime; } public String toString() { return "JobExecutionContext:" + " trigger: '" + getTrigger().getFullName() + " job: " + getJobDetail().getFullName() + " fireTime: '" + getFireTime() + " scheduledFireTime: " + getScheduledFireTime() + " previousFireTime: '" + getPreviousFireTime() + " nextFireTime: " + getNextFireTime() + " isRecovering: " + isRecovering() + " refireCount: " + getRefireCount(); } /** * Returns the result (if any) that theJob set before its
* execution completed (the type of object set as the result is entirely up
* to the particular job).
*
*
* The result itself is meaningless to Quartz, but may be informative
* to {@link JobListener}s or
* {@link TriggerListener}s that are watching the job's
* execution.
*
Job's execution (the type of
* object set as the result is entirely up to the particular job).
*
*
* The result itself is meaningless to Quartz, but may be informative
* to {@link JobListener}s or
* {@link TriggerListener}s that are watching the job's
* execution.
*
JobListeners and TriggerListeners.
*
* @return Returns the jobRunTime.
*/
public long getJobRunTime() {
return jobRunTime;
}
/**
* @param jobRunTime The jobRunTime to set.
*/
public void setJobRunTime(long jobRunTime) {
this.jobRunTime = jobRunTime;
}
/**
* Put the specified value into the context's data map with the given key.
* Possibly useful for sharing data between listeners and jobs.
*
* NOTE: this data is volatile - it is lost after the job execution * completes, and all TriggerListeners and JobListeners have been * notified.
* * @param key * @param value */ public void put(Object key, Object value) { data.put(key, value); } /** * Get the value with the given key from the context's data map. * * @param key */ public Object get(Object key) { return data.get(key); } }