@ProviderType
public interface Scheduler
Job
interface or the Runnable
interface.
A job can be scheduled either by creating a ScheduleOptions
instance
through one of the scheduler methods and then calling schedule(Object, ScheduleOptions)
or
by using the whiteboard pattern and registering a Runnable service with either
the PROPERTY_SCHEDULER_EXPRESSION
or PROPERTY_SCHEDULER_PERIOD
property. If both properties are specified, only PROPERTY_SCHEDULER_EXPRESSION
is considered for scheduling.
Services registered by the whiteboard pattern can by default run concurrently,
which usually is not wanted. Therefore it is advisable to also set the
PROPERTY_SCHEDULER_CONCURRENT
property with Boolean.FALSE.
Jobs started through the scheduler API are not persisted and are not
restarted after a bundle restart. If the client bundle is stopped, the scheduler
will stop all jobs started by this bundle as well. However, the client bundle does
not need to keep a reference to the scheduler service.Modifier and Type | Field and Description |
---|---|
static java.lang.String |
PROPERTY_SCHEDULER_CONCURRENT
Name of the configuration property to define if the job can be run concurrently.
|
static java.lang.String |
PROPERTY_SCHEDULER_EXPRESSION
Name of the configuration property to define the cron expression for a job.
|
static java.lang.String |
PROPERTY_SCHEDULER_IMMEDIATE
Name of the configuration property to define if a periodically job
should be scheduled immediate.
|
static java.lang.String |
PROPERTY_SCHEDULER_NAME
Name of the configuration property to define the job name.
|
static java.lang.String |
PROPERTY_SCHEDULER_PERIOD
Name of the configuration property to define the period for a job.
|
static java.lang.String |
PROPERTY_SCHEDULER_RUN_ON
Name of the configuration property to define the instances this job should run on.
|
static java.lang.String |
PROPERTY_SCHEDULER_THREAD_POOL
Name of the configuration property to define the thread pool to be used.
|
static java.lang.String |
PROPERTY_SCHEDULER_TIMES
Name of the optional configuration property to define the number of times the job
should be executed when
PROPERTY_SCHEDULER_PERIOD is defined. |
static java.lang.String |
VALUE_RUN_ON_LEADER
Value for
PROPERTY_SCHEDULER_RUN_ON to run the job on the leader only. |
static java.lang.String |
VALUE_RUN_ON_SINGLE
Value for
PROPERTY_SCHEDULER_RUN_ON to run the job on a single instance only. |
Modifier and Type | Method and Description |
---|---|
void |
addJob(java.lang.String name,
java.lang.Object job,
java.util.Map<java.lang.String,java.io.Serializable> config,
java.lang.String schedulingExpression,
boolean canRunConcurrently)
Deprecated.
Use
schedule(Object, ScheduleOptions) instead. |
void |
addPeriodicJob(java.lang.String name,
java.lang.Object job,
java.util.Map<java.lang.String,java.io.Serializable> config,
long period,
boolean canRunConcurrently)
Deprecated.
Use
schedule(Object, ScheduleOptions) instead. |
void |
addPeriodicJob(java.lang.String name,
java.lang.Object job,
java.util.Map<java.lang.String,java.io.Serializable> config,
long period,
boolean canRunConcurrently,
boolean startImmediate)
Deprecated.
Use
schedule(Object, ScheduleOptions) instead. |
ScheduleOptions |
AT(java.util.Date date)
Create a schedule options to fire a job once at a specific date.
|
ScheduleOptions |
AT(java.util.Date date,
int times,
long period)
Create a schedule options to fire a job period starting at a specific date.
|
ScheduleOptions |
EXPR(java.lang.String expression)
Create a schedule options to schedule the job based on the expression.
|
void |
fireJob(java.lang.Object job,
java.util.Map<java.lang.String,java.io.Serializable> config)
Deprecated.
Use
schedule(Object, ScheduleOptions) instead. |
boolean |
fireJob(java.lang.Object job,
java.util.Map<java.lang.String,java.io.Serializable> config,
int times,
long period)
Deprecated.
Use
schedule(Object, ScheduleOptions) instead. |
void |
fireJobAt(java.lang.String name,
java.lang.Object job,
java.util.Map<java.lang.String,java.io.Serializable> config,
java.util.Date date)
Deprecated.
Use
schedule(Object, ScheduleOptions) instead. |
boolean |
fireJobAt(java.lang.String name,
java.lang.Object job,
java.util.Map<java.lang.String,java.io.Serializable> config,
java.util.Date date,
int times,
long period)
Deprecated.
Use
schedule(Object, ScheduleOptions) instead. |
ScheduleOptions |
NOW()
Create a schedule options to fire a job immediately and only once.
|
ScheduleOptions |
NOW(int times,
long period)
Create a schedule options to fire a job immediately more than once.
|
void |
removeJob(java.lang.String name)
Deprecated.
Use
unschedule(String) instead. |
boolean |
schedule(java.lang.Object job,
ScheduleOptions options)
Schedule a job based on the options.
|
boolean |
unschedule(java.lang.String jobName)
Remove a scheduled job by name.
|
static final java.lang.String PROPERTY_SCHEDULER_PERIOD
static final java.lang.String PROPERTY_SCHEDULER_IMMEDIATE
static final java.lang.String PROPERTY_SCHEDULER_EXPRESSION
static final java.lang.String PROPERTY_SCHEDULER_CONCURRENT
static final java.lang.String PROPERTY_SCHEDULER_NAME
static final java.lang.String PROPERTY_SCHEDULER_TIMES
PROPERTY_SCHEDULER_PERIOD
is defined.
This property is of type integer and must have a positive value.static final java.lang.String PROPERTY_SCHEDULER_RUN_ON
VALUE_RUN_ON_LEADER
: the job is only run on the leader
- constant VALUE_RUN_ON_SINGLE
: the job is only run on a single instance in a cluster. This is
basically the same as VALUE_RUN_ON_LEADER
but it's not further specified which
single instance is used.
Default is to start the job on all instances. This property needs to be of type String.
If no topology information is available (= no Apache Sling Discovery Implementation active)
this option is ignored, and the job is run on all instances.static final java.lang.String VALUE_RUN_ON_LEADER
PROPERTY_SCHEDULER_RUN_ON
to run the job on the leader only.static final java.lang.String VALUE_RUN_ON_SINGLE
PROPERTY_SCHEDULER_RUN_ON
to run the job on a single instance only.static final java.lang.String PROPERTY_SCHEDULER_THREAD_POOL
boolean schedule(java.lang.Object job, ScheduleOptions options)
Job
or a Runnable
. The
options have to be created
by one of the provided methods from this scheduler.
The job is only started on this instance - if it is started at all. The
options for running on a single instance, on the leader etc. (see
ScheduleOptions.onInstancesOnly(String[])
,
ScheduleOptions.onLeaderOnly(boolean)
,
and ScheduleOptions.onSingleInstanceOnly(boolean)
) are only useful,
if the same job is scheduled on all instances in a cluster. In this case this
extra configuration controls on which instances the job is really started.
Using the above options might not start the job on the current instance, for
example if the current instance is not the leader.job
- The job to execute (either Job
or Runnable
).options
- Required options defining how to schedule the jobNOW()
,
NOW(int, long)
,
AT(Date)
,
AT(Date, int, long)
,
EXPR(String)
boolean unschedule(java.lang.String jobName)
jobName
- The name of the job.true
if the job existed and could be stopped, false
otherwise.ScheduleOptions NOW()
ScheduleOptions NOW(int times, long period)
times
- The number of times this job should be started (must be higher than 1 or
-1 for endless)period
- Every period seconds this job is started (must be at higher than 0).ScheduleOptions AT(java.util.Date date)
In case the scheduled time was missed due to the Scheduler not running or no thread being available at that point in time the job will be executed only once, as soon as the Scheduler is running again and a thread is available for processing the job.
date
- The date this job should be run.ScheduleOptions AT(java.util.Date date, int times, long period)
In case the scheduled time was missed due to the Scheduler not running or no thread being available at that point in time
the behavior depends on the times
parameter:
times
is -1 (meaning unlimited repetitions) there will be no immediate action. Instead the scheduler just waits for next scheduled interval.
date
- The date this job should be run.times
- The number of times this job should be started (must be higher than 1 or
-1 for endless)period
- Every period seconds this job is started (must be at higher than 0).ScheduleOptions EXPR(java.lang.String expression)
In case the scheduled time was missed due to the Scheduler not running or no thread being available at that point in time the job will be executed only once, as soon as the Scheduler is running again and a thread is available for processing the job. No matter how many trigger executions were missed, only a single immediate execution is performed.
expression
- The cron exception@Deprecated void addJob(java.lang.String name, java.lang.Object job, java.util.Map<java.lang.String,java.io.Serializable> config, java.lang.String schedulingExpression, boolean canRunConcurrently) throws java.lang.Exception
schedule(Object, ScheduleOptions)
instead.name
- The name of the job - or null. If no name is specified it can't be cancelled.job
- The job to execute (either Job
or Runnable
).config
- An optional configuration object - this configuration is only passed to the job the job implements Job
.schedulingExpression
- The time specification using a scheduling expression.canRunConcurrently
- Whether this job can run even if previous scheduled runs are still running.java.lang.IllegalArgumentException
- If the scheduling expression can't be parsed or if the job has not the correct type.java.lang.Exception
- If the job can't be scheduled.@Deprecated void addPeriodicJob(java.lang.String name, java.lang.Object job, java.util.Map<java.lang.String,java.io.Serializable> config, long period, boolean canRunConcurrently) throws java.lang.Exception
schedule(Object, ScheduleOptions)
instead.name
- The name of the job - or null. If no name is specified it can't be cancelled.job
- The job to execute (either Job
or Runnable
).config
- An optional configuration object - this configuration is only passed to the job the job implements Job
.period
- Every period seconds this job is started.canRunConcurrently
- Whether this job can run even if previous scheduled runs are still running.java.lang.IllegalArgumentException
- If the job has not the correct type.java.lang.Exception
- If the job can't be scheduled.@Deprecated void addPeriodicJob(java.lang.String name, java.lang.Object job, java.util.Map<java.lang.String,java.io.Serializable> config, long period, boolean canRunConcurrently, boolean startImmediate) throws java.lang.Exception
schedule(Object, ScheduleOptions)
instead.name
- The name of the job - or null. If no name is specified it can't be cancelled.job
- The job to execute (either Job
or Runnable
).config
- An optional configuration object - this configuration is only passed to the job the job implements Job
.period
- Every period seconds this job is started.canRunConcurrently
- Whether this job can run even if previous scheduled runs are still running.startImmediate
- Whether to start the job immediately for the first time or wait for the period to expire.java.lang.IllegalArgumentException
- If the job has not the correct type.java.lang.Exception
- If the job can't be scheduled.@Deprecated void fireJob(java.lang.Object job, java.util.Map<java.lang.String,java.io.Serializable> config) throws java.lang.Exception
schedule(Object, ScheduleOptions)
instead.job
- The job to execute (either Job
or Runnable
).config
- An optional configuration object - this configuration is only passed to the job the job implements Job
.java.lang.IllegalArgumentException
- If the job has not the correct type.java.lang.Exception
- If the job can't be scheduled.@Deprecated boolean fireJob(java.lang.Object job, java.util.Map<java.lang.String,java.io.Serializable> config, int times, long period)
schedule(Object, ScheduleOptions)
instead.job
- The job to execute (either Job
or Runnable
).config
- An optional configuration object - this configuration is only passed to the job the job implements Job
.times
- The number of times this job should be started (must be higher than 1)period
- Every period seconds this job is started.java.lang.IllegalArgumentException
- If the job has not the correct type.@Deprecated void fireJobAt(java.lang.String name, java.lang.Object job, java.util.Map<java.lang.String,java.io.Serializable> config, java.util.Date date) throws java.lang.Exception
schedule(Object, ScheduleOptions)
instead.name
- The name of the job - or null. If no name is specified it can't be cancelled.job
- The job to execute (either Job
or Runnable
).config
- An optional configuration object - this configuration is only passed to the job the job implements Job
.date
- The date this job should be run.java.lang.IllegalArgumentException
- If the job has not the correct type.java.lang.Exception
- If the job can't be scheduled.@Deprecated boolean fireJobAt(java.lang.String name, java.lang.Object job, java.util.Map<java.lang.String,java.io.Serializable> config, java.util.Date date, int times, long period)
schedule(Object, ScheduleOptions)
instead.name
- The name of the job - or null. If no name is specified it can't be cancelled.job
- The job to execute (either Job
or Runnable
).config
- An optional configuration object - this configuration is only passed to the job the job implements Job
.date
- The date this job should be run.times
- The number of times this job should be started (must be higher than 1)period
- Every period seconds this job is started.java.lang.IllegalArgumentException
- If the job has not the correct type.@Deprecated void removeJob(java.lang.String name) throws java.util.NoSuchElementException
unschedule(String)
instead.name
- The name of the job.java.util.NoSuchElementException
- If the job is not scheduled.Copyright © 2022 The Apache Software Foundation. All rights reserved.