Package io.jmix.quartz.service
Class QuartzService
java.lang.Object
io.jmix.quartz.service.QuartzService
Serves as proxy from Jmix to the Quartz engine for fetch information about jobs and triggers and update them
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected UnconstrainedDataManagerprotected static final longprotected QuartzJobDetailsFinderprotected Messagesprotected QuartzJobClassFinderprotected QuartzPropertiesprotected RunningJobsCacheprotected org.quartz.Scheduler -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected org.quartz.CronScheduleBuilderbuildCronSchedule(TriggerModel triggerModel) protected org.quartz.JobDetailbuildJobDetail(JobModel jobModel, @Nullable org.quartz.JobDetail existedJobDetail, List<JobDataParameterModel> jobDataParameterModels) protected org.quartz.SimpleScheduleBuilderbuildSimpleSchedule(TriggerModel triggerModel) protected org.quartz.TriggerbuildTrigger(org.quartz.JobDetail jobDetail, TriggerModel triggerModel) booleancheckJobExists(String jobName, String jobGroup) Delegates to the Quartz engine determination if the given job existsbooleancheckTriggerExists(String triggerName, String triggerGroup) Delegates to the Quartz engine determination if the given trigger existsprotected voidcopyPreviousFireTime(org.quartz.Trigger existingTrigger, org.quartz.Trigger newTrigger) Copies the last fire time of the trigger being replaced to its replacement, so that a reschedule keeps the execution history visible.protected JobModelcreateJobModel(org.quartz.JobKey jobKey, org.quartz.JobDetail jobDetail, List<org.quartz.JobKey> jobDetailsKeys) protected TriggerModelcreateTriggerModel(org.quartz.Trigger trigger, Date now) voidDelegates to the Quartz engine deleting given jobvoidexecuteNow(String jobName, String jobGroup) Delegates to the Quartz engine triggering given job (executing now)Returns information about all configured quartz jobs with related triggersgetDataParamsOfJob(org.quartz.JobKey jobKey) Returns given job's parametersgetDisplayedClassName(org.quartz.JobDetail jobDetail) Returns names of all known JobDetail groupsReturns names of all known Trigger groupsprotected booleanhasActiveTrigger(Collection<? extends org.quartz.Trigger> triggers) Defines whether any of the triggers is active, which means the job they belong to is waiting to be fired or running.booleanisJobRunning(org.quartz.JobKey jobKey) Checks if provided job is runningprotected booleanisJobRunningWithinCurrentScheduler(org.quartz.JobKey jobKey) protected booleanisScheduleChanged(org.quartz.Trigger existingTrigger, org.quartz.Trigger newTrigger) protected booleanisTriggerChanged(org.quartz.Trigger existingTrigger, org.quartz.Trigger newTrigger, TriggerModel triggerModel) Defines whether the trigger registered in the scheduler differs from the trigger built from the submitted model, which means the registered trigger must be replaced.protected voidlogMissingMisfireInstruction(String misfireInstructionId) voidDelegates to the Quartz engine pausing given job.protected Class<? extends org.quartz.Job> resolveJobClass(String jobClassName) Validates that the given class name is allowed as a Job class and loads it.@Nullable org.quartz.JobDetailresolveJobDetail(org.quartz.JobKey jobKey) Fetches theJobDetailfor the given key.protected JobStateresolveJobState(org.quartz.JobKey jobKey, org.quartz.JobDetail jobDetail, boolean active, boolean hasBlockedTrigger) protected StringresolveMisfireInstructionId(org.quartz.Trigger trigger) voidDelegates to the Quartz engine resuming given job.voidsaveJob(JobSaveContext context) Saves the job in the Quartz engine.protected voidscheduleTrigger(org.quartz.Trigger trigger, boolean paused) Schedules the trigger, paused if requested.protected voidsetupSchedule(org.quartz.TriggerBuilder<org.quartz.Trigger> triggerBuilder, TriggerModel triggerModel) protected voidsetupTriggerActivityDates(org.quartz.TriggerBuilder<org.quartz.Trigger> triggerBuilder, TriggerModel triggerModel) voidupdateQuartzJob(JobModel jobModel, List<JobDataParameterModel> jobDataParameterModels, List<TriggerModel> triggerModels, boolean replaceJobIfExists) Updates job in the Quartz engineprotected voidupdateTriggers(org.quartz.JobDetail jobDetail, List<TriggerModel> triggerModels, boolean existingJob) Synchronizes triggers of the job with the provided models: unchanged triggers are left untouched, so they keep their execution state and pause state, changed ones are rescheduled keeping their last fire time and pause state, missing ones are scheduled and obsolete ones are unscheduled.
-
Field Details
-
DEFAULT_REPEAT_INTERVAL_MILLIS
protected static final long DEFAULT_REPEAT_INTERVAL_MILLIS- See Also:
-
scheduler
@Autowired protected org.quartz.Scheduler scheduler -
jobDetailsFinder
-
dataManager
-
messages
-
runningJobsCache
-
quartzProperties
-
quartzJobClassFinder
-
-
Constructor Details
-
QuartzService
public QuartzService()
-
-
Method Details
-
getAllJobs
Returns information about all configured quartz jobs with related triggers -
saveJob
Saves the job in the Quartz engine. Job data parameters and triggers are taken from theJobModelcollections. Existing triggers of the job are synchronized with the provided ones: unchanged triggers are kept with their execution state and pause state, changed ones are rescheduled keeping their last fire time and pause state, obsolete ones are removed, an empty collection removes all triggers of the job. A save never starts a job that is not running: triggers added to an existing job without active triggers are paused.The behavior is defined by the original job key of the context (see
JobSaveContext.setOriginalJobKey(JobKey)) and the target key built from the job model name and group (an empty group means the default group):- the original key is not set — the job is created under the target key; if a job with this key
already exists,
QuartzJobSaveExceptionis thrown and the existing job is not touched; - the original key equals the target key — the existing job is updated in place (or created, if it no longer exists in the engine);
- the original key differs from the target key — the job is recreated under the target key and
removed under the original one; its triggers are created anew, since a trigger cannot be moved to
another job, so their execution state is not kept, but they are paused if the original job had no
active triggers; if a job with the target key already exists,
QuartzJobSaveExceptionis thrown and nothing is changed.
- Parameters:
context- the job model to save and the save options- Throws:
QuartzJobSaveException- if the job cannot be saved; with a JDBC job store all performed operations are rolled back, so a failed rename leaves the original job intact
- the original key is not set — the job is created under the target key; if a job with this key
already exists,
-
updateQuartzJob
@Transactional(rollbackForClassName="Exception") public void updateQuartzJob(JobModel jobModel, List<JobDataParameterModel> jobDataParameterModels, List<TriggerModel> triggerModels, boolean replaceJobIfExists) Updates job in the Quartz engine- Parameters:
jobModel- job to editjobDataParameterModels- parameters for jobtriggerModels- triggers for job; existing triggers of the job are synchronized with provided ones: unchanged triggers are kept with their execution state and pause state, changed ones are rescheduled keeping their last fire time and pause state, obsolete ones are removed, empty list removes all triggers; triggers added to an existing job without active triggers are pausedreplaceJobIfExists- replace if job with the same name already exists
-
resumeJob
Delegates to the Quartz engine resuming given job. This operation makes sense only for job with paused triggers.- Parameters:
jobName- name of the jobjobGroup- group of the job
-
pauseJob
Delegates to the Quartz engine pausing given job. This operation makes sense only for jobs with active triggers.- Parameters:
jobName- name of the jobjobGroup- group of the job
-
executeNow
Delegates to the Quartz engine triggering given job (executing now)- Parameters:
jobName- name of the jobjobGroup- group of the job
-
deleteJob
Delegates to the Quartz engine deleting given job- Parameters:
jobName- name of the jobjobGroup- group of the job
-
checkJobExists
Delegates to the Quartz engine determination if the given job exists- Parameters:
jobName- name of the jobjobGroup- group of the job- Returns:
- true if job with provided name and group exists, false otherwise
-
checkTriggerExists
Delegates to the Quartz engine determination if the given trigger exists- Parameters:
triggerName- name of the triggertriggerGroup- group of the trigger- Returns:
- true if trigger with provided name and group exists, false otherwise
-
resolveJobDetail
public @Nullable org.quartz.JobDetail resolveJobDetail(org.quartz.JobKey jobKey) Fetches theJobDetailfor the given key. Returns anInvalidJobDetailwhen the job class is missing, ornullwhen the detail cannot be fetched. -
isJobRunning
public boolean isJobRunning(org.quartz.JobKey jobKey) Checks if provided job is running -
getDisplayedClassName
-
getDataParamsOfJob
Returns given job's parameters- Parameters:
jobKey- key of job- Returns:
- parameters of given job
-
getJobGroupNames
Returns names of all known JobDetail groups -
getTriggerGroupNames
Returns names of all known Trigger groups -
createJobModel
protected JobModel createJobModel(org.quartz.JobKey jobKey, org.quartz.JobDetail jobDetail, List<org.quartz.JobKey> jobDetailsKeys) throws org.quartz.SchedulerException - Throws:
org.quartz.SchedulerException
-
createTriggerModel
-
resolveJobState
protected JobState resolveJobState(org.quartz.JobKey jobKey, org.quartz.JobDetail jobDetail, boolean active, boolean hasBlockedTrigger) -
isJobRunningWithinCurrentScheduler
protected boolean isJobRunningWithinCurrentScheduler(org.quartz.JobKey jobKey) -
resolveMisfireInstructionId
-
buildJobDetail
protected org.quartz.JobDetail buildJobDetail(JobModel jobModel, @Nullable org.quartz.JobDetail existedJobDetail, List<JobDataParameterModel> jobDataParameterModels) throws ClassNotFoundException - Throws:
ClassNotFoundException
-
resolveJobClass
protected Class<? extends org.quartz.Job> resolveJobClass(String jobClassName) throws ClassNotFoundException Validates that the given class name is allowed as a Job class and loads it. The allow-list check is performed before class loading to prevent execution of static initializers of arbitrary classes.- Throws:
ClassNotFoundException
-
updateTriggers
protected void updateTriggers(org.quartz.JobDetail jobDetail, List<TriggerModel> triggerModels, boolean existingJob) throws org.quartz.SchedulerException Synchronizes triggers of the job with the provided models: unchanged triggers are left untouched, so they keep their execution state and pause state, changed ones are rescheduled keeping their last fire time and pause state, missing ones are scheduled and obsolete ones are unscheduled. Triggers added to an existing job that has no active triggers are scheduled paused, so that the save does not start a job that is not running.- Parameters:
existingJob- whether the job existed in the scheduler before this save- Throws:
org.quartz.SchedulerException
-
isTriggerChanged
protected boolean isTriggerChanged(org.quartz.Trigger existingTrigger, org.quartz.Trigger newTrigger, TriggerModel triggerModel) Defines whether the trigger registered in the scheduler differs from the trigger built from the submitted model, which means the registered trigger must be replaced. The built trigger is compared instead of the raw model values, so that the defaults and the normalization applied on build (default time zone and repeat interval, start time rounding of cron triggers) do not count as changes. -
isScheduleChanged
protected boolean isScheduleChanged(org.quartz.Trigger existingTrigger, org.quartz.Trigger newTrigger) -
copyPreviousFireTime
protected void copyPreviousFireTime(org.quartz.Trigger existingTrigger, org.quartz.Trigger newTrigger) Copies the last fire time of the trigger being replaced to its replacement, so that a reschedule keeps the execution history visible. Quartz reads the previous fire time only for informational purposes (trigger listing,JobExecutionContext): the next fire time and the misfire handling do not depend on it. The fire counter of a simple trigger is not copied, the replacement starts its repeat count anew. -
scheduleTrigger
protected void scheduleTrigger(org.quartz.Trigger trigger, boolean paused) throws org.quartz.SchedulerException Schedules the trigger, paused if requested.- Throws:
org.quartz.SchedulerException
-
hasActiveTrigger
protected boolean hasActiveTrigger(Collection<? extends org.quartz.Trigger> triggers) throws org.quartz.SchedulerException Defines whether any of the triggers is active, which means the job they belong to is waiting to be fired or running.- Throws:
org.quartz.SchedulerException
-
buildTrigger
protected org.quartz.Trigger buildTrigger(org.quartz.JobDetail jobDetail, TriggerModel triggerModel) -
setupSchedule
protected void setupSchedule(org.quartz.TriggerBuilder<org.quartz.Trigger> triggerBuilder, TriggerModel triggerModel) -
setupTriggerActivityDates
protected void setupTriggerActivityDates(org.quartz.TriggerBuilder<org.quartz.Trigger> triggerBuilder, TriggerModel triggerModel) -
buildCronSchedule
-
buildSimpleSchedule
-
logMissingMisfireInstruction
-