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 thenscheduledto execute.- Since:
- 3.11.0
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedJobScheduler()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Job.BuilderclusterJob(String jobId)Creates a builder for a job that must run on only one node of the cluster each time that it triggers.protected voiddispose()Disposes the resources used by this scheduler and unschedules all jobs.protected abstract voiddoSchedule(Job job)Performs the scheduling of the job.protected abstract voiddoUnschedule(Job job)Performs the job unscheduling.static JobSchedulergetInstance()Returns the scheduler instance.Optional<Job>getJob(String id)Returns the job of given identifier built by this scheduler, if anyJobConfiggetJobConfig(Job job)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.BuilderjobBuilder()Creates a builder of job.Job.BuilderlocalJob(String jobId)Creates builder for a job scheduled so that it will apply only to this particular node of the cluster.voidschedule(Job job, JobSchedule defaultSchedule)Schedules the background job.voidschedule(Job job, JobScheduleConfig scheduleConfig)Changes the schedule of the background job.static voidsetInstance(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 delayschedule.- 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
disablejob 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 aclusterjob and afixed delayschedule.JobSchedulerException- if anything prevents scheduling the job
-
getJobConfig
public JobConfig getJobConfig(Job job)
Returns a resource describing the configuration of the job.- Parameters:
job- the job for which a configuration is to retrieve- Returns:
- the job config resource
- Since:
- 3.12.0
-
schedule
public final void schedule(Job job, JobScheduleConfig scheduleConfig) throws JobSchedulerException, SettingStoreException
Changes the schedule of the background job. This method is called byJobConfigServicewhen 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 settingsandappliedon the job.- Parameters:
job- the job to schedulescheduleConfig- the edited schedule configuration- 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- Since:
- 3.12.0
-
doSchedule
protected abstract void doSchedule(Job job) throws JobSchedulerException
Performs the scheduling of the job. The job is known to be alreadyunscheduledat 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.
-
-