Class JobScheduler
- java.lang.Object
-
- com.sodius.oslc.server.core.jobs.JobScheduler
-
public abstract class JobScheduler extends Object
Schedules background jobs, either at fixed rate or a fixed delay between executions.Jobs are created either with
clusterJob(String)
orlocalJob(String)
and thenscheduled
to execute.- Since:
- 3.11.0
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
JobScheduler()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Job.Builder
clusterJob(String jobId)
Creates a builder for a job that must run on only one node of the cluster each time that it triggers.protected void
dispose()
Disposes the resources used by this scheduler and unschedules all jobs.protected abstract void
doSchedule(Job job)
Performs the scheduling of the job.protected abstract void
doUnschedule(Job job)
Performs the job unscheduling.static JobScheduler
getInstance()
Returns the scheduler instance.Optional<Job>
getJob(String id)
Returns the job of given identifier built by this scheduler, if anyJobConfig
getJobConfig(Job job, List<Locale> locales)
Returns a resource describing the configuration of the job.Collection<Job>
getJobs()
Returns an unmodifiable collection of jobs built by this scheduler.protected abstract Job.Builder
jobBuilder()
Creates a builder of job.Job.Builder
localJob(String jobId)
Creates builder for a job scheduled so that it will apply only to this particular node of the cluster.void
schedule(Job job, JobSchedule defaultSchedule)
Schedules the background job.void
schedule(Job job, JobScheduleConfig scheduleConfig, List<Locale> locales)
Changes the schedule of the background job.static void
setInstance(JobScheduler instance)
Changes the scheduler instance.
-
-
-
Method Detail
-
getInstance
public static JobScheduler getInstance()
Returns the scheduler instance.- Returns:
- the scheduler instance.
- See Also:
setInstance(JobScheduler)
-
setInstance
public static void setInstance(JobScheduler instance)
Changes the scheduler instance. The default scheduler is already cluster compatible. Applications may provide an alternate scheduler if they want to rely on a different job framework than the default.- Parameters:
instance
- the new scheduler
-
clusterJob
public final Job.Builder clusterJob(String jobId)
Creates a builder for a job that must run on only one node of the cluster each time that it triggers. UselocalJob(String)
if the job must run on each node of the cluster.Note a cluster job cannot use a
fixed delay
schedule.- Parameters:
jobId
- the identifier of the job to create- Returns:
- a job builder instance
- Throws:
NullPointerException
- if jobId is null or empty
-
localJob
public final Job.Builder localJob(String jobId)
Creates builder for a job scheduled so that it will apply only to this particular node of the cluster. UseclusterJob(String)
for a job that must run on only one node of the cluster each time that it triggers.- Parameters:
jobId
- the identifier of the job to create- Returns:
- a job builder instance
- Throws:
NullPointerException
- if jobId is null or empty
-
jobBuilder
protected abstract Job.Builder jobBuilder()
Creates a builder of job.- Returns:
- a new builder instance
-
getJobs
public Collection<Job> getJobs()
Returns an unmodifiable collection of jobs built by this scheduler.- Returns:
- the known jobs
-
getJob
public Optional<Job> getJob(String id)
Returns the job of given identifier built by this scheduler, if any- Parameters:
id
- the job identifier to look for- Returns:
- the corresponding job, or an empty optional
-
schedule
public final void schedule(Job job, JobSchedule defaultSchedule) throws JobSchedulerException
Schedules the background job. This method is to call when a job is just created and is to schedule initially.The given schedule is used as a default. If an administrator has edited the schedule for this job, to use a more suitable time for triggering it, the default schedule is discarded and the edited one is applied.
This method automatically cancels previous scheduling, if any. All jobs are also automatically unscheduled when the application is
disposed
.This method may also be used to
disable
job scheduling, so that the job no longer runs.- Parameters:
job
- the job to scheduledefaultSchedule
- the default schedule to use- Throws:
NullPointerException
- if job or defaultSchedule is nullIllegalArgumentException
- if it is acluster
job and afixed delay
schedule.JobSchedulerException
- if anything prevents scheduling the job
-
getJobConfig
public JobConfig getJobConfig(Job job, List<Locale> locales)
Returns a resource describing the configuration of the job.- Parameters:
locales
- the locales to use, in decreasing order starting with the preferred locale.- Returns:
- the job config resource
-
schedule
public final void schedule(Job job, JobScheduleConfig scheduleConfig, List<Locale> locales) throws JobSchedulerException, SettingStoreException
Changes the schedule of the background job. This method is called byJobConfigService
when an administrator edited a job schedule.The given schedule is first
validated
, to ensure the edited settings are applicable to the job. The new schedule is then saved inserver settings
andapplied
on the job.- Parameters:
job
- the job to schedulescheduleConfig
- the edited schedule configurationlocales
- the locales to use, in decreasing order starting with the preferred locale- Throws:
JobSchedulerException
- if anything prevents scheduling the jobSettingStoreException
- if the edited schedule cannot be stored in server settingsIllegalArgumentException
- if the schedule is invalid and cannot be applied
-
doSchedule
protected abstract void doSchedule(Job job) throws JobSchedulerException
Performs the scheduling of the job. The job is known to be alreadyunscheduled
at this stage.- Parameters:
job
- the job to schedule- Throws:
JobSchedulerException
- if anything prevents scheduling the job
-
doUnschedule
protected abstract void doUnschedule(Job job) throws JobSchedulerException
Performs the job unscheduling.- Parameters:
job
- the job to unschedule- Throws:
JobSchedulerException
- if anything prevents unscheduling the job
-
dispose
protected void dispose()
Disposes the resources used by this scheduler and unschedules all jobs.
-
-