All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.quartz.JobDetail Maven / Gradle / Ivy


/* 
 * Copyright 2001-2009 Terracotta, Inc. 
 * 
 * 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.
 * 
 */

package org.quartz;

import java.util.LinkedHashSet;
import java.util.Set;

import org.quartz.utils.Key;

/**
 * 

* Conveys the detail properties of a given Job instance. *

* *

* Quartz does not store an actual instance of a Job class, but * instead allows you to define an instance of one, through the use of a JobDetail. *

* *

* Jobs have a name and group associated with them, which * should uniquely identify them within a single {@link Scheduler}. *

* *

* Triggers are the 'mechanism' by which Jobs * are scheduled. Many Triggers can point to the same Job, * but a single Trigger can only point to one Job. *

* * @see Job * @see StatefulJob * @see JobDataMap * @see Trigger * * @author James House * @author Sharada Jambula */ public class JobDetail implements Cloneable, java.io.Serializable { /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Data members. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ private String name; private String group = Scheduler.DEFAULT_GROUP; private String description; private Class jobClass; private JobDataMap jobDataMap; private boolean volatility = false; private boolean durability = false; private boolean shouldRecover = false; private Set jobListeners = new LinkedHashSet(); private transient Key key = null; /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Constructors. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /** *

* Create a JobDetail with no specified name or group, and * the default settings of all the other properties. *

* *

* Note that the {@link #setName(String)},{@link #setGroup(String)}and * {@link #setJobClass(Class)}methods must be called before the job can be * placed into a {@link Scheduler} *

*/ public JobDetail() { // do nothing... } /** *

* Create a JobDetail with the given name, given class, default group, * and the default settings of all the other properties. *

* * @param group if null, Scheduler.DEFAULT_GROUP will be used. * * @exception IllegalArgumentException * if name is null or empty, or the group is an empty string. */ public JobDetail(String name, Class jobClass) { this(name, null, jobClass); } /** *

* Create a JobDetail with the given name, group and class, * and the default settings of all the other properties. *

* * @param group if null, Scheduler.DEFAULT_GROUP will be used. * * @exception IllegalArgumentException * if name is null or empty, or the group is an empty string. */ public JobDetail(String name, String group, Class jobClass) { setName(name); setGroup(group); setJobClass(jobClass); } /** *

* Create a JobDetail with the given name, and group, and * the given settings of all the other properties. *

* * @param group if null, Scheduler.DEFAULT_GROUP will be used. * * @exception IllegalArgumentException * if name is null or empty, or the group is an empty string. */ public JobDetail(String name, String group, Class jobClass, boolean volatility, boolean durability, boolean recover) { setName(name); setGroup(group); setJobClass(jobClass); setVolatility(volatility); setDurability(durability); setRequestsRecovery(recover); } /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Interface. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /** *

* Get the name of this Job. *

*/ public String getName() { return name; } /** *

* Set the name of this Job. *

* * @exception IllegalArgumentException * if name is null or empty. */ public void setName(String name) { if (name == null || name.trim().length() == 0) { throw new IllegalArgumentException("Job name cannot be empty."); } this.name = name; } /** *

* Get the group of this Job. *

*/ public String getGroup() { return group; } /** *

* Set the group of this Job. *

* * @param group if null, Scheduler.DEFAULT_GROUP will be used. * * @exception IllegalArgumentException * if the group is an empty string. */ public void setGroup(String group) { if (group != null && group.trim().length() == 0) { throw new IllegalArgumentException( "Group name cannot be empty."); } if (group == null) { group = Scheduler.DEFAULT_GROUP; } this.group = group; } /** *

* Returns the 'full name' of the JobDetail in the format * "group.name". *

*/ public String getFullName() { return group + "." + name; } public Key getKey() { if(key == null) { key = new Key(getName(), getGroup()); } return key; } /** *

* Return the description given to the Job instance by its * creator (if any). *

* * @return null if no description was set. */ public String getDescription() { return description; } /** *

* Set a description for the Job instance - may be useful * for remembering/displaying the purpose of the job, though the * description has no meaning to Quartz. *

*/ public void setDescription(String description) { this.description = description; } /** *

* Get the instance of Job that will be executed. *

*/ public Class getJobClass() { return jobClass; } /** *

* Set the instance of Job that will be executed. *

* * @exception IllegalArgumentException * if jobClass is null or the class is not a Job. */ public void setJobClass(Class jobClass) { if (jobClass == null) { throw new IllegalArgumentException("Job class cannot be null."); } if (!Job.class.isAssignableFrom(jobClass)) { throw new IllegalArgumentException( "Job class must implement the Job interface."); } this.jobClass = jobClass; } /** *

* Get the JobDataMap that is associated with the Job. *

*/ public JobDataMap getJobDataMap() { if (jobDataMap == null) { jobDataMap = new JobDataMap(); } return jobDataMap; } /** *

* Set the JobDataMap to be associated with the Job. *

*/ public void setJobDataMap(JobDataMap jobDataMap) { this.jobDataMap = jobDataMap; } /** *

* Validates whether the properties of the JobDetail are * valid for submission into a Scheduler. * * @throws IllegalStateException * if a required property (such as Name, Group, Class) is not * set. */ public void validate() throws SchedulerException { if (name == null) { throw new SchedulerException("Job's name cannot be null", SchedulerException.ERR_CLIENT_ERROR); } if (group == null) { throw new SchedulerException("Job's group cannot be null", SchedulerException.ERR_CLIENT_ERROR); } if (jobClass == null) { throw new SchedulerException("Job's class cannot be null", SchedulerException.ERR_CLIENT_ERROR); } } /** *

* Set whether or not the Job should be persisted in the * {@link org.quartz.spi.JobStore} for re-use after program * restarts. *

* *

* If not explicitly set, the default value is false. *

*/ public void setVolatility(boolean volatility) { this.volatility = volatility; } /** *

* Set whether or not the Job should remain stored after it * is orphaned (no {@link Trigger}s point to it). *

* *

* If not explicitly set, the default value is false. *

*/ public void setDurability(boolean durability) { this.durability = durability; } /** *

* Set whether or not the the Scheduler should re-execute * the Job if a 'recovery' or 'fail-over' situation is * encountered. *

* *

* If not explicitly set, the default value is false. *

* * @see JobExecutionContext#isRecovering() */ public void setRequestsRecovery(boolean shouldRecover) { this.shouldRecover = shouldRecover; } /** *

* Whether or not the Job should not be persisted in the * {@link org.quartz.spi.JobStore} for re-use after program * restarts. *

* *

* If not explicitly set, the default value is false. *

* * @return true if the Job should be garbage * collected along with the {@link Scheduler}. */ public boolean isVolatile() { return volatility; } /** *

* Whether or not the Job should remain stored after it is * orphaned (no {@link Trigger}s point to it). *

* *

* If not explicitly set, the default value is false. *

* * @return true if the Job should remain persisted after * being orphaned. */ public boolean isDurable() { return durability; } /** *

* Whether or not the Job implements the interface {@link StatefulJob}. *

*/ public boolean isStateful() { if (jobClass == null) { return false; } return (StatefulJob.class.isAssignableFrom(jobClass)); } /** *

* Instructs the Scheduler whether or not the Job * should be re-executed if a 'recovery' or 'fail-over' situation is * encountered. *

* *

* If not explicitly set, the default value is false. *

* * @see JobExecutionContext#isRecovering() */ public boolean requestsRecovery() { return shouldRecover; } /** *

* Add the specified name of a {@link JobListener} to the * end of the Job's list of listeners. *

* * @see #getJobListenerNames() */ public void addJobListener(String name) { if (jobListeners.add(name) == false) { throw new IllegalArgumentException( "Job listener '" + name + "' is already registered for job detail: " + getFullName()); } } /** *

* Remove the specified name of a {@link JobListener} from * the Job's list of listeners. *

* * @return true if the given name was found in the list, and removed */ public boolean removeJobListener(String name) { return jobListeners.remove(name); } /** *

* Returns an array of String s containing the names of all * {@link JobListener}s assigned to the Job, * in the order in which they should be notified. *

* * @see #addJobListener(String) */ public String[] getJobListenerNames() { return (String[])jobListeners.toArray(new String[jobListeners.size()]); } /** *

* Return a simple string representation of this object. *

*/ public String toString() { return "JobDetail '" + getFullName() + "': jobClass: '" + ((getJobClass() == null) ? null : getJobClass().getName()) + " isStateful: " + isStateful() + " isVolatile: " + isVolatile() + " isDurable: " + isDurable() + " requestsRecovers: " + requestsRecovery(); } public boolean equals(Object obj) { if (!(obj instanceof JobDetail)) { return false; } JobDetail other = (JobDetail) obj; if (other.getName() == null && getName() != null) { return false; } if (other.getName() != null && !other.getName().equals(getName())) { return false; } if (other.getGroup() == null && getGroup() != null) { return false; } if (other.getGroup() != null && !other.getGroup().equals(getGroup())) { return false; } return true; } public int hashCode() { return getFullName().hashCode(); } public Object clone() { JobDetail copy; try { copy = (JobDetail) super.clone(); copy.jobListeners = new LinkedHashSet(); copy.jobListeners.addAll(jobListeners); if (jobDataMap != null) { copy.jobDataMap = (JobDataMap) jobDataMap.clone(); } } catch (CloneNotSupportedException ex) { throw new IncompatibleClassChangeError("Not Cloneable."); } return copy; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy