Package org.jobrunr.scheduling
Class JobScheduler
- java.lang.Object
-
- org.jobrunr.scheduling.AbstractJobScheduler
-
- org.jobrunr.scheduling.JobScheduler
-
public class JobScheduler extends AbstractJobScheduler
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.
-
-
Field Summary
-
Fields inherited from class org.jobrunr.scheduling.AbstractJobScheduler
BATCH_SIZE
-
-
Constructor Summary
Constructors Constructor Description JobScheduler(StorageProvider storageProvider)
Creates a new JobScheduler using the provided storageProviderJobScheduler(StorageProvider storageProvider, java.util.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, java.util.List<JobFilter> jobFilters)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <S,T>
voidenqueue(java.util.stream.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(java.util.stream.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(java.util.UUID id, IocJobLambda<S> iocJob)
Creates a new fire-and-forget job based on a given lambda.JobId
enqueue(java.util.UUID id, JobLambda job)
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.JobId
enqueue(JobLambda job)
Creates a new fire-and-forget job based on a given lambda.<S> JobId
schedule(java.time.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.JobId
schedule(java.time.Instant instant, 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(java.time.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.JobId
schedule(java.time.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(java.time.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.JobId
schedule(java.time.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(java.time.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.JobId
schedule(java.time.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(java.util.UUID id, java.time.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.JobId
schedule(java.util.UUID id, java.time.Instant instant, 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(java.util.UUID id, java.time.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.JobId
schedule(java.util.UUID id, java.time.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(java.util.UUID id, java.time.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.JobId
schedule(java.util.UUID id, java.time.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(java.util.UUID id, java.time.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.JobId
schedule(java.util.UUID id, java.time.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> java.lang.String
scheduleRecurrently(java.lang.String id, java.lang.String cron, java.time.ZoneId zoneId, IocJobLambda<S> iocJob)
Creates a new recurring job based on the given id, cron expression,ZoneId
and lambda.java.lang.String
scheduleRecurrently(java.lang.String id, java.lang.String cron, java.time.ZoneId zoneId, JobLambda job)
Creates a new recurring job based on the given id, cron expression,ZoneId
and lambda.<S> java.lang.String
scheduleRecurrently(java.lang.String id, java.lang.String cron, IocJobLambda<S> iocJob)
Creates a new recurring job based on the given id, cron expression and lambda.java.lang.String
scheduleRecurrently(java.lang.String id, java.lang.String cron, JobLambda job)
Creates a new recurring job based on the given id, cron expression and lambda.<S> java.lang.String
scheduleRecurrently(java.lang.String cron, IocJobLambda<S> iocJob)
Creates a new recurring job based on the given cron expression and the given lambda.java.lang.String
scheduleRecurrently(java.lang.String cron, JobLambda job)
Creates a new recurring job based on the given lambda and the given cron expression.
-
-
-
Constructor Detail
-
JobScheduler
public JobScheduler(StorageProvider storageProvider)
Creates a new JobScheduler using the provided storageProvider- Parameters:
storageProvider
- the storageProvider to use
-
JobScheduler
public JobScheduler(StorageProvider storageProvider, java.util.List<JobFilter> jobFilters)
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, java.util.List<JobFilter> jobFilters)
-
-
Method Detail
-
enqueue
public JobId enqueue(JobLambda job)
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
public JobId enqueue(java.util.UUID id, JobLambda job)
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
public <T> void enqueue(java.util.stream.Stream<T> input, JobLambdaFromStream<T> jobFromStream)
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
public <S> JobId enqueue(IocJobLambda<S> iocJob)
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
public <S> JobId enqueue(java.util.UUID id, IocJobLambda<S> iocJob)
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
public <S,T> void enqueue(java.util.stream.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
. 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
public JobId schedule(java.time.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.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
public JobId schedule(java.util.UUID id, java.time.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. 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
public <S> JobId schedule(java.time.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. 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
public <S> JobId schedule(java.util.UUID id, java.time.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. 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
public JobId schedule(java.time.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.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
public JobId schedule(java.util.UUID id, java.time.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. 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
public <S> JobId schedule(java.time.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. 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
public <S> JobId schedule(java.util.UUID id, java.time.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. 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
public JobId schedule(java.time.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.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
public JobId schedule(java.util.UUID id, java.time.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. 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
public <S> JobId schedule(java.time.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. 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
public <S> JobId schedule(java.util.UUID id, java.time.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. 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
public JobId schedule(java.time.Instant instant, 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.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
public JobId schedule(java.util.UUID id, java.time.Instant instant, 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. 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
public <S> JobId schedule(java.time.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. 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
public <S> JobId schedule(java.util.UUID id, java.time.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. 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
-
scheduleRecurrently
public java.lang.String scheduleRecurrently(java.lang.String cron, JobLambda job)
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 fire-and-forget job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
Cron
-
scheduleRecurrently
public <S> java.lang.String scheduleRecurrently(java.lang.String cron, IocJobLambda<S> iocJob)
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 fire-and-forget job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
Cron
-
scheduleRecurrently
public java.lang.String scheduleRecurrently(java.lang.String id, java.lang.String cron, JobLambda job)
Creates a new 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 fire-and-forget job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
Cron
-
scheduleRecurrently
public <S> java.lang.String scheduleRecurrently(java.lang.String id, java.lang.String cron, IocJobLambda<S> iocJob)
Creates a new 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 fire-and-forget job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
Cron
-
scheduleRecurrently
public java.lang.String scheduleRecurrently(java.lang.String id, java.lang.String cron, java.time.ZoneId zoneId, JobLambda job)
Creates a new 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 fire-and-forget job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
Cron
-
scheduleRecurrently
public <S> java.lang.String scheduleRecurrently(java.lang.String id, java.lang.String cron, java.time.ZoneId zoneId, IocJobLambda<S> iocJob)
Creates a new 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 fire-and-forget job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
Cron
-
-