Package org.jobrunr.scheduling
Class JobScheduler
java.lang.Object
org.jobrunr.scheduling.AbstractJobScheduler
org.jobrunr.scheduling.JobScheduler
Provides methods for creating fire-and-forget, delayed and recurring jobs as well as to delete existing background jobs.
This JobScheduler
allows to schedule jobs by means of a Java 8 lambda which is analyzed.
-
Constructor Summary
ConstructorDescriptionJobScheduler
(StorageProvider storageProvider) Creates a new JobScheduler using the provided storageProviderJobScheduler
(StorageProvider storageProvider, List<JobFilter> jobFilters) Creates a new JobScheduler using the provided storageProvider and the list of JobFilters that will be used for every background jobJobScheduler
(StorageProvider storageProvider, JobDetailsGenerator jobDetailsGenerator, List<JobFilter> jobFilters) -
Method Summary
Modifier and TypeMethodDescriptionvoid
create
(Stream<JobBuilder> jobBuilderStream) Creates a newJob
for eachJobBuilder
and provides an alternative to the job annotation.create
(JobBuilder jobBuilder) Creates a newJob
using aJobBuilder
that can be enqueued or scheduled and provides an alternative to the job annotation.createRecurrently
(RecurringJobBuilder recurringJobBuilder) Creates a new or alters the existing recurring job based on the given RecurringJobBuilder (using id, cron expression and lambda).<S,
T> void enqueue
(Stream<T> input, IocJobLambdaFromStream<S, T> iocJobFromStream) Creates new fire-and-forget jobs for each item in the input stream using the lambda passed asjobFromStream
.<T> void
enqueue
(Stream<T> input, JobLambdaFromStream<T> jobFromStream) Creates new fire-and-forget jobs for each item in the input stream using the lambda passed asjobFromStream
.<S> JobId
enqueue
(UUID id, IocJobLambda<S> iocJob) Creates a new fire-and-forget job based on a given lambda.Creates a new fire-and-forget job based on the given lambda.<S> JobId
enqueue
(IocJobLambda<S> iocJob) Creates a new fire-and-forget job based on a given lambda.Creates a new fire-and-forget job based on a given lambda.<S> JobId
schedule
(Instant instant, IocJobLambda<S> iocJob) Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.<S> JobId
schedule
(LocalDateTime localDateTime, IocJobLambda<S> iocJob) Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.schedule
(LocalDateTime localDateTime, JobLambda job) Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.<S> JobId
schedule
(OffsetDateTime offsetDateTime, IocJobLambda<S> iocJob) Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.schedule
(OffsetDateTime offsetDateTime, JobLambda job) Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.<S> JobId
schedule
(ZonedDateTime zonedDateTime, IocJobLambda<S> iocJob) Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.schedule
(ZonedDateTime zonedDateTime, JobLambda job) Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.<S> JobId
schedule
(UUID id, Instant instant, IocJobLambda<S> iocJob) Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.<S> JobId
schedule
(UUID id, LocalDateTime localDateTime, IocJobLambda<S> iocJob) Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.schedule
(UUID id, LocalDateTime localDateTime, JobLambda job) Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.<S> JobId
schedule
(UUID id, OffsetDateTime offsetDateTime, IocJobLambda<S> iocJob) Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.schedule
(UUID id, OffsetDateTime offsetDateTime, JobLambda job) Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.<S> JobId
schedule
(UUID id, ZonedDateTime zonedDateTime, IocJobLambda<S> iocJob) Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.schedule
(UUID id, ZonedDateTime zonedDateTime, JobLambda job) Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.<S> String
scheduleRecurrently
(String id, String cron, ZoneId zoneId, IocJobLambda<S> iocJob) Creates a new or alters the existing recurring job based on the given id, cron expression,ZoneId
and lambda.scheduleRecurrently
(String id, String cron, ZoneId zoneId, JobLambda job) Creates a new or alters the existing recurring job based on the given id, cron expression,ZoneId
and lambda.<S> String
scheduleRecurrently
(String id, String cron, IocJobLambda<S> iocJob) Creates a new or alters the existing recurring job based on the given id, cron expression and lambda.scheduleRecurrently
(String id, String cron, JobLambda job) Creates a new or alters the existing recurring job based on the given id, cron expression and lambda.<S> String
scheduleRecurrently
(String id, Duration duration, IocJobLambda<S> iocJob) Creates a new or alters the existing recurring job based on the given id, duration and lambda.scheduleRecurrently
(String id, Duration duration, JobLambda job) Creates a new or alters the existing recurring job based on the given id, duration and lambda.<S> String
scheduleRecurrently
(String cron, IocJobLambda<S> iocJob) Creates a new recurring job based on the given cron expression and the given lambda.scheduleRecurrently
(String cron, JobLambda job) Creates a new recurring job based on the given lambda and the given cron expression.<S> String
scheduleRecurrently
(Duration duration, IocJobLambda<S> iocJob) Creates a new recurring job based on the given duration and the given lambda.scheduleRecurrently
(Duration duration, JobLambda job) Creates a new recurring job based on the given duration and the given lambda.
-
Constructor Details
-
JobScheduler
Creates a new JobScheduler using the provided storageProvider- Parameters:
storageProvider
- the storageProvider to use
-
JobScheduler
Creates a new JobScheduler using the provided storageProvider and the list of JobFilters that will be used for every background job- Parameters:
storageProvider
- the storageProvider to usejobFilters
- list of jobFilters that will be used for every job
-
JobScheduler
public JobScheduler(StorageProvider storageProvider, JobDetailsGenerator jobDetailsGenerator, List<JobFilter> jobFilters)
-
-
Method Details
-
create
Creates a newJob
using aJobBuilder
that can be enqueued or scheduled and provides an alternative to the job annotation.- Parameters:
jobBuilder
- the jobBuilder with all the details of the job- Returns:
- the id of the job
-
create
Creates a newJob
for eachJobBuilder
and provides an alternative to the job annotation.- Parameters:
jobBuilderStream
- the jobBuilders for which to create jobs.
-
enqueue
Creates a new fire-and-forget job based on a given lambda.An example:
MyService service = new MyService(); jobScheduler.enqueue(() -> service.doWork());
- Parameters:
job
- the lambda which defines the fire-and-forget job- Returns:
- the id of the job
-
enqueue
Creates a new fire-and-forget job based on the given lambda. If a job with that id already exists, JobRunr will not save it again.An example:
MyService service = new MyService(); jobScheduler.enqueue(id, () -> service.doWork());
- Parameters:
id
- the uuid with which to save the jobjob
- the lambda which defines the fire-and-forget job- Returns:
- the id of the job
-
enqueue
Creates new fire-and-forget jobs for each item in the input stream using the lambda passed asjobFromStream
.An example:
MyService service = new MyService(); Stream<UUID> workStream = getWorkStream(); jobScheduler.enqueue(workStream, (uuid) -> service.doWork(uuid));
- Parameters:
input
- the stream of items for which to create fire-and-forget jobsjobFromStream
- the lambda which defines the fire-and-forget job to create for each item in theinput
-
enqueue
Creates a new fire-and-forget job based on a given lambda. The IoC container will be used to resolveMyService
.An example:
jobScheduler.<MyService>enqueue(x -> x.doWork());
- Parameters:
iocJob
- the lambda which defines the fire-and-forget job- Returns:
- the id of the job
-
enqueue
Creates a new fire-and-forget job based on a given lambda. The IoC container will be used to resolveMyService
. If a job with that id already exists, JobRunr will not save it again.An example:
jobScheduler.<MyService>enqueue(id, x -> x.doWork());
- Parameters:
id
- the uuid with which to save the jobiocJob
- the lambda which defines the fire-and-forget job- Returns:
- the id of the job
-
enqueue
Creates new fire-and-forget jobs for each item in the input stream using the lambda passed asjobFromStream
. The IoC container will be used to resolveMyService
.An example:
Stream<UUID> workStream = getWorkStream(); jobScheduler.<MyService, UUID>enqueue(workStream, (x, uuid) -> x.doWork(uuid));
- Parameters:
input
- the stream of items for which to create fire-and-forget jobsiocJobFromStream
- the lambda which defines the fire-and-forget job to create for each item in theinput
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.An example:
MyService service = new MyService(); jobScheduler.schedule(ZonedDateTime.now().plusHours(5), () -> service.doWork());
- Parameters:
zonedDateTime
- the moment in time at which the job will be enqueued.job
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. If a job with that id already exists, JobRunr will not save it again.An example:
MyService service = new MyService(); jobScheduler.schedule(id, ZonedDateTime.now().plusHours(5), () -> service.doWork());
- Parameters:
id
- the uuid with which to save the jobzonedDateTime
- the moment in time at which the job will be enqueued.job
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. The IoC container will be used to resolveMyService
.An example:
jobScheduler.<MyService>schedule(ZonedDateTime.now().plusHours(5), x -> x.doWork());
- Parameters:
zonedDateTime
- the moment in time at which the job will be enqueued.iocJob
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. The IoC container will be used to resolveMyService
. If a job with that id already exists, JobRunr will not save it again.An example:
jobScheduler.<MyService>schedule(id, ZonedDateTime.now().plusHours(5), x -> x.doWork());
- Parameters:
id
- the uuid with which to save the jobzonedDateTime
- the moment in time at which the job will be enqueued.iocJob
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.An example:
MyService service = new MyService(); jobScheduler.schedule(OffsetDateTime.now().plusHours(5), () -> service.doWork());
- Parameters:
offsetDateTime
- The moment in time at which the job will be enqueued.job
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. If a job with that id already exists, JobRunr will not save it again.An example:
MyService service = new MyService(); jobScheduler.schedule(id, OffsetDateTime.now().plusHours(5), () -> service.doWork());
- Parameters:
id
- the uuid with which to save the joboffsetDateTime
- The moment in time at which the job will be enqueued.job
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. The IoC container will be used to resolveMyService
.An example:
jobScheduler.<MyService>schedule(OffsetDateTime.now().plusHours(5), x -> x.doWork());
- Parameters:
offsetDateTime
- The moment in time at which the job will be enqueued.iocJob
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. The IoC container will be used to resolveMyService
. If a job with that id already exists, JobRunr will not save it again.An example:
jobScheduler.<MyService>schedule(id, OffsetDateTime.now().plusHours(5), x -> x.doWork());
- Parameters:
id
- the uuid with which to save the joboffsetDateTime
- The moment in time at which the job will be enqueued.iocJob
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.An example:
MyService service = new MyService(); jobScheduler.schedule(LocalDateTime.now().plusHours(5), () -> service.doWork());
- Parameters:
localDateTime
- the moment in time at which the job will be enqueued. It will use the systemDefault ZoneId to transform it to an UTC Instantjob
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. If a job with that id already exists, JobRunr will not save it again.An example:
MyService service = new MyService(); jobScheduler.schedule(id, LocalDateTime.now().plusHours(5), () -> service.doWork());
- Parameters:
id
- the uuid with which to save the joblocalDateTime
- the moment in time at which the job will be enqueued. It will use the systemDefault ZoneId to transform it to an UTC Instantjob
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. The IoC container will be used to resolveMyService
.An example:
jobScheduler.<MyService>schedule(LocalDateTime.now().plusHours(5), x -> x.doWork());
- Parameters:
localDateTime
- the moment in time at which the job will be enqueued. It will use the systemDefault ZoneId to transform it to an UTC InstantiocJob
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. The IoC container will be used to resolveMyService
. If a job with that id already exists, JobRunr will not save it again.An example:
jobScheduler.<MyService>schedule(LocalDateTime.now().plusHours(5), x -> x.doWork());
- Parameters:
id
- the uuid with which to save the joblocalDateTime
- the moment in time at which the job will be enqueued. It will use the systemDefault ZoneId to transform it to an UTC InstantiocJob
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.An example:
MyService service = new MyService(); jobScheduler.schedule(Instant.now().plusHours(5), () -> service.doWork());
- Parameters:
instant
- the moment in time at which the job will be enqueued.job
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. If a job with that id already exists, JobRunr will not save it again.An example:
MyService service = new MyService(); jobScheduler.schedule(id, Instant.now().plusHours(5), () -> service.doWork());
- Parameters:
id
- the uuid with which to save the jobinstant
- the moment in time at which the job will be enqueued.job
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. The IoC container will be used to resolveMyService
.An example:
jobScheduler.<MyService>schedule(Instant.now().plusHours(5), x -> x.doWork());
- Parameters:
instant
- the moment in time at which the job will be enqueued.iocJob
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. The IoC container will be used to resolveMyService
. If a job with that id already exists, JobRunr will not save it again.An example:
jobScheduler.<MyService>schedule(id, Instant.now().plusHours(5), x -> x.doWork());
- Parameters:
id
- the uuid with which to save the jobinstant
- the moment in time at which the job will be enqueued.iocJob
- the lambda which defines the fire-and-forget job- Returns:
- the id of the Job
-
createRecurrently
Creates a new or alters the existing recurring job based on the given RecurringJobBuilder (using id, cron expression and lambda). If no zoneId is set on the builder the jobs will be scheduled using the systemDefault timezone.An example:
jobScheduler.createRecurrently(aRecurringJob() .withCron("* * 0 * * *") .withDetails(() -> service.doWork());
- Parameters:
recurringJobBuilder
- the builder describing your recurring job.- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
-
scheduleRecurrently
Creates a new recurring job based on the given lambda and the given cron expression. The jobs will be scheduled using the systemDefault timezone.An example:
MyService service = new MyService(); jobScheduler.scheduleRecurrently(Cron.daily(), () -> service.doWork());
- Parameters:
cron
- The cron expression defining when to run this recurring jobjob
- the lambda which defines the recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
-
scheduleRecurrently
Creates a new recurring job based on the given cron expression and the given lambda. The IoC container will be used to resolveMyService
. The jobs will be scheduled using the systemDefault timezone.An example:
jobScheduler.<MyService>scheduleRecurrently(Cron.daily(), x -> x.doWork());
- Parameters:
cron
- The cron expression defining when to run this recurring jobiocJob
- the lambda which defines the recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
-
scheduleRecurrently
Creates a new or alters the existing recurring job based on the given id, cron expression and lambda. The jobs will be scheduled using the systemDefault timezoneAn example:
MyService service = new MyService(); jobScheduler.scheduleRecurrently("my-recurring-job", Cron.daily(), () -> service.doWork());
- Parameters:
id
- the id of this recurring job which can be used to alter or delete itcron
- The cron expression defining when to run this recurring jobjob
- the lambda which defines the recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
-
scheduleRecurrently
Creates a new or alters the existing recurring job based on the given id, cron expression and lambda. The IoC container will be used to resolveMyService
. The jobs will be scheduled using the systemDefault timezoneAn example:
jobScheduler.<MyService>scheduleRecurrently("my-recurring-job", Cron.daily()), x -> x.doWork();
- Parameters:
id
- the id of this recurring job which can be used to alter or delete itcron
- The cron expression defining when to run this recurring jobiocJob
- the lambda which defines the recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
-
scheduleRecurrently
Creates a new or alters the existing recurring job based on the given id, cron expression,ZoneId
and lambda.An example:
MyService service = new MyService(); jobScheduler.scheduleRecurrently("my-recurring-job", Cron.daily(), ZoneId.of("Europe/Brussels"), () -> service.doWork());
- Parameters:
id
- the id of this recurring job which can be used to alter or delete itcron
- The cron expression defining when to run this recurring jobzoneId
- The zoneId (timezone) of when to run this recurring jobjob
- the lambda which defines the recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
-
scheduleRecurrently
public <S> String scheduleRecurrently(String id, String cron, ZoneId zoneId, IocJobLambda<S> iocJob) Creates a new or alters the existing recurring job based on the given id, cron expression,ZoneId
and lambda. The IoC container will be used to resolveMyService
.An example:
jobScheduler.<MyService>scheduleRecurrently("my-recurring-job", Cron.daily(), ZoneId.of("Europe/Brussels"), x -> x.doWork());
- Parameters:
id
- the id of this recurring job which can be used to alter or delete itcron
- The cron expression defining when to run this recurring jobzoneId
- The zoneId (timezone) of when to run this recurring jobiocJob
- the lambda which defines the recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
-
scheduleRecurrently
Creates a new recurring job based on the given duration and the given lambda. The first run of this recurring job will happen after the given duration unless your duration is smaller or equal than your backgroundJobServer pollInterval.An example:
MyService service = new MyService(); BackgroundJob.scheduleRecurrently(Duration.parse("P5D"), () -> service.doWork());
- Parameters:
duration
- the duration defining the time between each instance of this recurring job.job
- the lambda which defines the recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
-
scheduleRecurrently
Creates a new recurring job based on the given duration and the given lambda. The IoC container will be used to resolveMyService
. The first run of this recurring job will happen after the given duration unless your duration is smaller or equal than your backgroundJobServer pollInterval.An example:
BackgroundJob.<MyService>scheduleRecurrently(Duration.parse("P5D"), x -> x.doWork());
- Parameters:
duration
- the duration defining the time between each instance of this recurring jobiocJob
- the lambda which defines the recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
-
scheduleRecurrently
Creates a new or alters the existing recurring job based on the given id, duration and lambda. The first run of this recurring job will happen after the given duration unless your duration is smaller or equal than your backgroundJobServer pollInterval.An example:
MyService service = new MyService(); BackgroundJob.scheduleRecurrently("my-recurring-job", Duration.parse("P5D"), () -> service.doWork());
- Parameters:
id
- the id of this recurring job which can be used to alter or delete itduration
- the duration defining the time between each instance of this recurring jobjob
- the lambda which defines the recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
-
scheduleRecurrently
Creates a new or alters the existing recurring job based on the given id, duration and lambda. The IoC container will be used to resolveMyService
. The first run of this recurring job will happen after the given duration unless your duration is smaller or equal than your backgroundJobServer pollInterval.An example:
BackgroundJob.<MyService>scheduleRecurrently("my-recurring-job", Duration.parse("P5D"), x -> x.doWork());
- Parameters:
id
- the id of this recurring job which can be used to alter or delete itduration
- the duration defining the time between each instance of this recurring jobiocJob
- the lambda which defines the recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
-