org.quartz.impl.JobDetailImpl 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.impl;
import java.util.LinkedHashSet;
import java.util.Set;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.PersistJobDataAfterExecution;
import org.quartz.Scheduler;
import org.quartz.StatefulJob;
import org.quartz.Trigger;
import org.quartz.utils.ClassUtils;
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
.
*
*
*
* Job
s have a name and group associated with them, which
* should uniquely identify them within a single {@link Scheduler}
.
*
*
*
* Trigger
s are the 'mechanism' by which Job
s
* are scheduled. Many Trigger
s 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 JobDetailImpl implements Cloneable, java.io.Serializable, JobDetail {
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Data members.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
private String name;
private String group = Scheduler.DEFAULT_GROUP;
private String description;
private Class extends Job> jobClass;
private JobDataMap jobDataMap;
private boolean durability = false;
private boolean shouldRecover = false;
private transient JobKey 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 JobDetailImpl() {
// 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.
*
* @deprecated use {@link JobBuilder}
*/
public JobDetailImpl(String name, Class extends Job> 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.
*
* @deprecated use {@link JobBuilder}
*/
public JobDetailImpl(String name, String group, Class extends Job> 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.
*
* @deprecated use {@link JobBuilder}
*/
public JobDetailImpl(String name, String group, Class extends Job> jobClass,
boolean durability, boolean recover) {
setName(name);
setGroup(group);
setJobClass(jobClass);
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;
this.key = null;
}
/**
*
* 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;
this.key = null;
}
/**
*
* Returns the 'full name' of the JobDetail
in the format
* "group.name".
*
*/
public String getFullName() {
return group + "." + name;
}
/* (non-Javadoc)
* @see org.quartz.JobDetailI#getKey()
*/
public JobKey getKey() {
if(key == null) {
if(getName() == null)
return null;
key = new JobKey(getName(), getGroup());
}
return key;
}
public void setKey(JobKey key) {
if(key == null)
throw new IllegalArgumentException("Key cannot be null!");
setName(key.getName());
setGroup(key.getGroup());
this.key = key;
}
/* (non-Javadoc)
* @see org.quartz.JobDetailI#getDescription()
*/
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;
}
/* (non-Javadoc)
* @see org.quartz.JobDetailI#getJobClass()
*/
public Class extends Job> 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 extends Job> 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;
}
/* (non-Javadoc)
* @see org.quartz.JobDetailI#getJobDataMap()
*/
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;
}
/**
*
* 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;
}
/* (non-Javadoc)
* @see org.quartz.JobDetailI#isDurable()
*/
public boolean isDurable() {
return durability;
}
/**
* @return whether the associated Job class carries the {@link PersistJobDataAfterExecution} annotation.
*/
public boolean isPersistJobDataAfterExecution() {
return ClassUtils.isAnnotationPresent(jobClass, PersistJobDataAfterExecution.class);
}
/**
* @return whether the associated Job class carries the {@link DisallowConcurrentExecution} annotation.
*/
public boolean isConcurrentExectionDisallowed() {
return ClassUtils.isAnnotationPresent(jobClass, DisallowConcurrentExecution.class);
}
/* (non-Javadoc)
* @see org.quartz.JobDetailI#requestsRecovery()
*/
public boolean requestsRecovery() {
return shouldRecover;
}
/**
*
* Return a simple string representation of this object.
*
*/
public String toString() {
return "JobDetail '" + getFullName() + "': jobClass: '"
+ ((getJobClass() == null) ? null : getJobClass().getName())
+ " concurrentExectionDisallowed: " + isConcurrentExectionDisallowed()
+ " persistJobDataAfterExecution: " + isPersistJobDataAfterExecution()
+ " isDurable: " + isDurable() + " requestsRecovers: " + requestsRecovery();
}
public boolean equals(Object obj) {
if (!(obj instanceof JobDetail)) {
return false;
}
JobDetail other = (JobDetail) obj;
if(other.getKey() == null || getKey() == null)
return false;
if (!other.getKey().equals(getKey())) {
return false;
}
return true;
}
public int hashCode() {
return getKey().hashCode();
}
public Object clone() {
JobDetailImpl copy;
try {
copy = (JobDetailImpl) super.clone();
if (jobDataMap != null) {
copy.jobDataMap = (JobDataMap) jobDataMap.clone();
}
} catch (CloneNotSupportedException ex) {
throw new IncompatibleClassChangeError("Not Cloneable.");
}
return copy;
}
public JobBuilder getJobBuilder() {
JobBuilder b = JobBuilder.newJob()
.ofType(getJobClass())
.requestRecovery(requestsRecovery())
.storeDurably(isDurable())
.usingJobData(getJobDataMap())
.withDescription(getDescription())
.withIdentity(getKey());
return b;
}
}