org.quartz.JobBuilder Maven / Gradle / Ivy
Show all versions of quartz Show documentation
/*
* All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
*
* 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 org.quartz.impl.JobDetailImpl;
import org.quartz.utils.Key;
/**
* JobBuilder
is used to instantiate {@link JobDetail}s.
*
* The builder will always try to keep itself in a valid state, with
* reasonable defaults set for calling build() at any point. For instance
* if you do not invoke withIdentity(..) a job name will be generated
* for you.
*
* Quartz provides a builder-style API for constructing scheduling-related
* entities via a Domain-Specific Language (DSL). The DSL can best be
* utilized through the usage of static imports of the methods on the classes
* TriggerBuilder
, JobBuilder
,
* DateBuilder
, JobKey
, TriggerKey
* and the various ScheduleBuilder
implementations.
*
* Client code can then use the DSL to write code such as this:
*
* JobDetail job = newJob(MyJob.class)
* .withIdentity("myJob")
* .build();
*
* Trigger trigger = newTrigger()
* .withIdentity(triggerKey("myTrigger", "myTriggerGroup"))
* .withSchedule(simpleSchedule()
* .withIntervalInHours(1)
* .repeatForever())
* .startAt(futureDate(10, MINUTES))
* .build();
*
* scheduler.scheduleJob(job, trigger);
*
*
* @see TriggerBuilder
* @see DateBuilder
* @see JobDetail
*/
public class JobBuilder {
private JobKey key;
private String description;
private Class extends Job> jobClass;
private boolean durability;
private boolean shouldRecover;
private JobDataMap jobDataMap = new JobDataMap();
protected JobBuilder() {
}
/**
* Create a JobBuilder with which to define a JobDetail
.
*
* @return a new JobBuilder
*/
public static JobBuilder newJob() {
return new JobBuilder();
}
/**
* Create a JobBuilder with which to define a JobDetail
,
* and set the class name of the Job
to be executed.
*
* @return a new JobBuilder
*/
public static JobBuilder newJob(Class extends Job> jobClass) {
JobBuilder b = new JobBuilder();
b.ofType(jobClass);
return b;
}
/**
* Produce the JobDetail
instance defined by this
* JobBuilder
.
*
* @return the defined JobDetail.
*/
public JobDetail build() {
JobDetailImpl job = new JobDetailImpl();
job.setJobClass(jobClass);
job.setDescription(description);
if(key == null)
key = new JobKey(Key.createUniqueName(null), null);
job.setKey(key);
job.setDurability(durability);
job.setRequestsRecovery(shouldRecover);
if(!jobDataMap.isEmpty())
job.setJobDataMap(jobDataMap);
return job;
}
/**
* Use a JobKey
with the given name and default group to
* identify the JobDetail.
*
* If none of the 'withIdentity' methods are set on the JobBuilder,
* then a random, unique JobKey will be generated.
*
* @param name the name element for the Job's JobKey
* @return the updated JobBuilder
* @see JobKey
* @see JobDetail#getKey()
*/
public JobBuilder withIdentity(String name) {
key = new JobKey(name, null);
return this;
}
/**
* Use a JobKey
with the given name and group to
* identify the JobDetail.
*
* If none of the 'withIdentity' methods are set on the JobBuilder,
* then a random, unique JobKey will be generated.
*
* @param name the name element for the Job's JobKey
* @param group the group element for the Job's JobKey
* @return the updated JobBuilder
* @see JobKey
* @see JobDetail#getKey()
*/
public JobBuilder withIdentity(String name, String group) {
key = new JobKey(name, group);
return this;
}
/**
* Use a JobKey
to identify the JobDetail.
*
* If none of the 'withIdentity' methods are set on the JobBuilder,
* then a random, unique JobKey will be generated.
*
* @param jobKey the Job's JobKey
* @return the updated JobBuilder
* @see JobKey
* @see JobDetail#getKey()
*/
public JobBuilder withIdentity(JobKey jobKey) {
this.key = jobKey;
return this;
}
/**
* Set the given (human-meaningful) description of the Job.
*
* @param jobDescription the description for the Job
* @return the updated JobBuilder
* @see JobDetail#getDescription()
*/
public JobBuilder withDescription(String jobDescription) {
this.description = jobDescription;
return this;
}
/**
* Set the class which will be instantiated and executed when a
* Trigger fires that is associated with this JobDetail.
*
* @param jobClazz a class implementing the Job interface.
* @return the updated JobBuilder
* @see JobDetail#getJobClass()
*/
public JobBuilder ofType(Class extends Job> jobClazz) {
this.jobClass = jobClazz;
return this;
}
/**
* 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
.
*
*
* @return the updated JobBuilder
* @see JobDetail#requestsRecovery()
*/
public JobBuilder requestRecovery() {
this.shouldRecover = true;
return this;
}
/**
* 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
.
*
*
* @param jobShouldRecover the desired setting
* @return the updated JobBuilder
*/
public JobBuilder requestRecovery(boolean jobShouldRecover) {
this.shouldRecover = jobShouldRecover;
return this;
}
/**
* 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
* - this method sets the value to true
.
*
*
* @return the updated JobBuilder
* @see JobDetail#isDurable()
*/
public JobBuilder storeDurably() {
this.durability = true;
return this;
}
/**
* 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
.
*
*
* @param jobDurability the value to set for the durability property.
* @return the updated JobBuilder
* @see JobDetail#isDurable()
*/
public JobBuilder storeDurably(boolean jobDurability) {
this.durability = jobDurability;
return this;
}
/**
* Add the given key-value pair to the JobDetail's {@link JobDataMap}.
*
* @return the updated JobBuilder
* @see JobDetail#getJobDataMap()
*/
public JobBuilder usingJobData(String dataKey, String value) {
jobDataMap.put(dataKey, value);
return this;
}
/**
* Add the given key-value pair to the JobDetail's {@link JobDataMap}.
*
* @return the updated JobBuilder
* @see JobDetail#getJobDataMap()
*/
public JobBuilder usingJobData(String dataKey, Integer value) {
jobDataMap.put(dataKey, value);
return this;
}
/**
* Add the given key-value pair to the JobDetail's {@link JobDataMap}.
*
* @return the updated JobBuilder
* @see JobDetail#getJobDataMap()
*/
public JobBuilder usingJobData(String dataKey, Long value) {
jobDataMap.put(dataKey, value);
return this;
}
/**
* Add the given key-value pair to the JobDetail's {@link JobDataMap}.
*
* @return the updated JobBuilder
* @see JobDetail#getJobDataMap()
*/
public JobBuilder usingJobData(String dataKey, Float value) {
jobDataMap.put(dataKey, value);
return this;
}
/**
* Add the given key-value pair to the JobDetail's {@link JobDataMap}.
*
* @return the updated JobBuilder
* @see JobDetail#getJobDataMap()
*/
public JobBuilder usingJobData(String dataKey, Double value) {
jobDataMap.put(dataKey, value);
return this;
}
/**
* Add the given key-value pair to the JobDetail's {@link JobDataMap}.
*
* @return the updated JobBuilder
* @see JobDetail#getJobDataMap()
*/
public JobBuilder usingJobData(String dataKey, Boolean value) {
jobDataMap.put(dataKey, value);
return this;
}
/**
* Add all the data from the given {@link JobDataMap} to the
* {@code JobDetail}'s {@code JobDataMap}.
*
* @return the updated JobBuilder
* @see JobDetail#getJobDataMap()
*/
public JobBuilder usingJobData(JobDataMap newJobDataMap) {
jobDataMap.putAll(newJobDataMap);
return this;
}
/**
* Replace the {@code JobDetail}'s {@link JobDataMap} with the
* given {@code JobDataMap}.
*
* @return the updated JobBuilder
* @see JobDetail#getJobDataMap()
*/
public JobBuilder setJobData(JobDataMap newJobDataMap) {
jobDataMap = newJobDataMap;
return this;
}
}