📄 jobexecutioncontext.java
字号:
/* * Copyright James House (c) 2001-2004 * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: 1. * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. 2. Redistributions in * binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */package org.quartz;import java.util.Date;import java.util.HashMap;import org.quartz.spi.TriggerFiredBundle;/** * <p> * A context bundle containing handles to various environment information, that * is given to a <code>{@link org.quartz.JobDetail}</code> instance as it is * executed, and to a <code>{@link Trigger}</code> instance after the * execution completes. * </p> * * <p> * <code>JobExecutionContext</code> s are also returned from the * <code>Scheduler.getCurrentlyExecutingJobs()</code> * method. These are the same instances as those past 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 <code>JobExecutionContext</code>s, and their references to * the <code>Scheduler</code> and <code>Job</code> instances have been lost (a * clone of the <code>JobDetail</code> is still available - just not a handle * to the job instance that is running). * </p> * * @see Job * @see Trigger * * @author James House */public class JobExecutionContext implements java.io.Serializable { /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Data members. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ private transient Scheduler scheduler; private Trigger trigger; private JobDetail jobDetail; private transient Job job; private Calendar calendar; private boolean recovering = false; private int numRefires = 0; private Date fireTime; private Date scheduledFireTime; private Date prevFireTime; private Date nextFireTime; private long jobRunTime = -1; private Object result; private HashMap data = new HashMap(); /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Constructors. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /** * <p> * Create a JobExcecutionContext with the given context data. * </p> */ 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(); } /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Interface. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /** * <p> * Get a handle to the <code>Scheduler</code> instance that fired the * <code>Job</code>. * </p> */ public Scheduler getScheduler() { return scheduler; } /** * <p> * Get a handle to the <code>Trigger</code> instance that fired the * <code>Job</code>. * </p> */ public Trigger getTrigger() { return trigger; } /** * <p> * Get a handle to the <code>Calendar</code> referenced by the <code>Trigger</code> * instance that fired the <code>Job</code>. * </p> */ public Calendar getCalendar() { return calendar; } /** * <p> * If the <code>Job</code> is being re-executed because of a 'recovery' * situation, this method will return <code>true</code>. * </p> */ public boolean isRecovering() { return recovering; } public void incrementRefireCount() { numRefires++; } public int getRefireCount() { return numRefires; } /** * <p> * Get the <code>JobDetail</code> associated with the <code>Job</code>. * </p> */ public JobDetail getJobDetail() { return jobDetail; } /** * <p> * Get the instance of the <code>Job</code> that was created for this * execution. * </p> * * <p> * Note: The Job instance is not available through remote scheduler * interfaces. * </p> */ 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 the <code>Job</code> set before its * execution completed (the type of object set as the result is entirely up * to the particular job). * * <p> * The result itself is meaningless to Quartz, but may be informative * to <code>{@link JobListener}s</code> or * <code>{@link TriggerListener}s</code> that are watching the job's * execution. * </p> * * @return Returns the result. */ public Object getResult() { return result; } /** * Set the result (if any) of the <code>Job</code>'s execution (the type of * object set as the result is entirely up to the particular job). * * <p> * The result itself is meaningless to Quartz, but may be informative * to <code>{@link JobListener}s</code> or * <code>{@link TriggerListener}s</code> that are watching the job's * execution. * </p> * * @return Returns the result. */ public void setResult(Object result) { this.result = result; } /** * The amount of time the job ran for (in milliseconds). The returned * value will be -1 until the job has actually completed (or thrown an * exception), and is therefore generally only useful to * <code>JobListener</code>s and <code>TriggerListener</code>s. * * @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. * * <p>NOTE: this data is volatile - it is lost after the job execution * completes, and all TriggerListeners and JobListeners have been * notified.</p> * * @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); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -