Class QuartzService

java.lang.Object
io.jmix.quartz.service.QuartzService

@Service("quartz_QuartzService") public class QuartzService extends Object
Serves as proxy from Jmix to the Quartz engine for fetch information about jobs and triggers and update them
  • Field Details

  • Constructor Details

    • QuartzService

      public QuartzService()
  • Method Details

    • getAllJobs

      public List<JobModel> getAllJobs()
      Returns information about all configured quartz jobs with related triggers
    • saveJob

      @Transactional(rollbackFor=java.lang.Exception.class) public void saveJob(JobSaveContext context)
      Saves the job in the Quartz engine. Job data parameters and triggers are taken from the JobModel collections. 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, QuartzJobSaveException is 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, QuartzJobSaveException is 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
    • 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 edit
      jobDataParameterModels - parameters for job
      triggerModels - 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 paused
      replaceJobIfExists - replace if job with the same name already exists
    • resumeJob

      public void resumeJob(String jobName, String jobGroup)
      Delegates to the Quartz engine resuming given job. This operation makes sense only for job with paused triggers.
      Parameters:
      jobName - name of the job
      jobGroup - group of the job
    • pauseJob

      public void pauseJob(String jobName, String jobGroup)
      Delegates to the Quartz engine pausing given job. This operation makes sense only for jobs with active triggers.
      Parameters:
      jobName - name of the job
      jobGroup - group of the job
    • executeNow

      public void executeNow(String jobName, String jobGroup)
      Delegates to the Quartz engine triggering given job (executing now)
      Parameters:
      jobName - name of the job
      jobGroup - group of the job
    • deleteJob

      public void deleteJob(String jobName, String jobGroup)
      Delegates to the Quartz engine deleting given job
      Parameters:
      jobName - name of the job
      jobGroup - group of the job
    • checkJobExists

      public boolean checkJobExists(String jobName, String jobGroup)
      Delegates to the Quartz engine determination if the given job exists
      Parameters:
      jobName - name of the job
      jobGroup - group of the job
      Returns:
      true if job with provided name and group exists, false otherwise
    • checkTriggerExists

      public boolean checkTriggerExists(String triggerName, String triggerGroup)
      Delegates to the Quartz engine determination if the given trigger exists
      Parameters:
      triggerName - name of the trigger
      triggerGroup - 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 the JobDetail for the given key. Returns an InvalidJobDetail when the job class is missing, or null when the detail cannot be fetched.
    • isJobRunning

      public boolean isJobRunning(org.quartz.JobKey jobKey)
      Checks if provided job is running
    • getDisplayedClassName

      public String getDisplayedClassName(org.quartz.JobDetail jobDetail)
    • getDataParamsOfJob

      public List<JobDataParameterModel> getDataParamsOfJob(org.quartz.JobKey jobKey)
      Returns given job's parameters
      Parameters:
      jobKey - key of job
      Returns:
      parameters of given job
    • getJobGroupNames

      public List<String> getJobGroupNames()
      Returns names of all known JobDetail groups
    • getTriggerGroupNames

      public List<String> 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

      protected TriggerModel createTriggerModel(org.quartz.Trigger trigger, Date now)
    • 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

      protected String resolveMisfireInstructionId(org.quartz.Trigger trigger)
    • 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

      protected org.quartz.CronScheduleBuilder buildCronSchedule(TriggerModel triggerModel)
    • buildSimpleSchedule

      protected org.quartz.SimpleScheduleBuilder buildSimpleSchedule(TriggerModel triggerModel)
    • logMissingMisfireInstruction

      protected void logMissingMisfireInstruction(String misfireInstructionId)