Package org.jobrunr.scheduling
Class JobScheduler
- java.lang.Object
-
- org.jobrunr.scheduling.JobScheduler
-
public class JobScheduler extends java.lang.Object
Provides methods for creating fire-and-forget, delayed and recurring jobs as well as to delete existing background jobs.
-
-
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 job
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
deleteRecurringly(java.lang.String id)
Deletes the recurring job based on the given id.<TItem,TService>
voidenqueue(java.util.stream.Stream<TItem> input, IocJobLambdaFromStream<TService,TItem> iocJobFromStream)
Creates new fire-and-forget jobs for each item in the input stream using the lambda passed asjobFromStream
.<TItem> void
enqueue(java.util.stream.Stream<TItem> input, JobLambdaFromStream<TItem> jobFromStream)
Creates new fire-and-forget jobs for each item in the input stream using the lambda passed asjobFromStream
.<T> java.util.UUID
enqueue(IocJobLambda<T> iocJob)
Creates a new fire-and-forget job based on a given lambda.java.util.UUID
enqueue(JobLambda job)
Creates a new fire-and-forget job based on a given lambda.<T> java.util.UUID
schedule(IocJobLambda<T> iocJob, java.time.Instant instant)
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.<T> java.util.UUID
schedule(IocJobLambda<T> iocJob, java.time.LocalDateTime localDateTime)
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.<T> java.util.UUID
schedule(IocJobLambda<T> iocJob, java.time.OffsetDateTime offsetDateTime)
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.<T> java.util.UUID
schedule(IocJobLambda<T> iocJob, java.time.ZonedDateTime zonedDateTime)
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.java.util.UUID
schedule(JobLambda job, java.time.Instant instant)
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.java.util.UUID
schedule(JobLambda job, java.time.LocalDateTime localDateTime)
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.java.util.UUID
schedule(JobLambda job, java.time.OffsetDateTime offsetDateTime)
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.java.util.UUID
schedule(JobLambda job, java.time.ZonedDateTime zonedDateTime)
Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.<T> java.lang.String
scheduleRecurringly(java.lang.String id, IocJobLambda<T> iocJob, java.lang.String cron)
Creates a new recurring job based on the given id, lambda and cron expression.<T> java.lang.String
scheduleRecurringly(java.lang.String id, IocJobLambda<T> iocJob, java.lang.String cron, java.time.ZoneId zoneId)
Creates a new recurring job based on the given id, lambda, cron expression andZoneId
.java.lang.String
scheduleRecurringly(java.lang.String id, JobLambda job, java.lang.String cron)
Creates a new recurring job based on the given id, lambda and cron expression.java.lang.String
scheduleRecurringly(java.lang.String id, JobLambda job, java.lang.String cron, java.time.ZoneId zoneId)
Creates a new recurring job based on the given id, lambda, cron expression andZoneId
.<T> java.lang.String
scheduleRecurringly(IocJobLambda<T> iocJob, java.lang.String cron)
Creates a new recurring job based on the given lambda and the given cron expression.java.lang.String
scheduleRecurringly(JobLambda job, java.lang.String cron)
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
-
-
Method Detail
-
enqueue
public java.util.UUID enqueue(JobLambda job)
Creates a new fire-and-forget job based on a given lambda.An example:
MyService service = new MyService(); BackgroundJob.enqueue(() -> service.doWork());
- Parameters:
job
- the lambda which defines the fire-and-forget job- Returns:
- the id of the job
-
enqueue
public <TItem> void enqueue(java.util.stream.Stream<TItem> input, JobLambdaFromStream<TItem> 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(); BackgroundJob.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 <T> java.util.UUID enqueue(IocJobLambda<T> iocJob)
Creates a new fire-and-forget job based on a given lambda. The IoC container will be used to resolveMyService
.An example:
BackgroundJob.<MyService>enqueue(x -> x.doWork());
- Parameters:
iocJob
- the lambda which defines the fire-and-forget job- Returns:
- the id of the job
-
enqueue
public <TItem,TService> void enqueue(java.util.stream.Stream<TItem> input, IocJobLambdaFromStream<TService,TItem> 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(); BackgroundJob.<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 java.util.UUID schedule(JobLambda job, java.time.ZonedDateTime zonedDateTime)
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(); BackgroundJob.schedule(() -> service.doWork(), ZonedDateTime.now().plusHours(5));
- Parameters:
job
- the lambda which defines the fire-and-forget jobzonedDateTime
- The moment in time at which the job will be enqueued.
-
schedule
public <T> java.util.UUID schedule(IocJobLambda<T> iocJob, java.time.ZonedDateTime zonedDateTime)
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:
BackgroundJob.<MyService>schedule(x -> x.doWork(), ZonedDateTime.now().plusHours(5));
- Parameters:
iocJob
- the lambda which defines the fire-and-forget jobzonedDateTime
- The moment in time at which the job will be enqueued.
-
schedule
public java.util.UUID schedule(JobLambda job, java.time.OffsetDateTime offsetDateTime)
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(); BackgroundJob.schedule(() -> service.doWork(), OffsetDateTime.now().plusHours(5));
- Parameters:
job
- the lambda which defines the fire-and-forget joboffsetDateTime
- The moment in time at which the job will be enqueued.
-
schedule
public <T> java.util.UUID schedule(IocJobLambda<T> iocJob, java.time.OffsetDateTime offsetDateTime)
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:
BackgroundJob.<MyService>schedule(x -> x.doWork(), OffsetDateTime.now().plusHours(5));
- Parameters:
iocJob
- the lambda which defines the fire-and-forget joboffsetDateTime
- The moment in time at which the job will be enqueued.
-
schedule
public java.util.UUID schedule(JobLambda job, java.time.LocalDateTime localDateTime)
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(); BackgroundJob.schedule(() -> service.doWork(), LocalDateTime.now().plusHours(5));
- Parameters:
job
- the lambda which defines the fire-and-forget joblocalDateTime
- The moment in time at which the job will be enqueued. It will use the systemDefault ZoneId to transform it to an UTC Instant
-
schedule
public <T> java.util.UUID schedule(IocJobLambda<T> iocJob, java.time.LocalDateTime localDateTime)
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:
BackgroundJob.<MyService>schedule(x -> x.doWork(), LocalDateTime.now().plusHours(5));
- Parameters:
iocJob
- the lambda which defines the fire-and-forget joblocalDateTime
- The moment in time at which the job will be enqueued. It will use the systemDefault ZoneId to transform it to an UTC Instant
-
schedule
public java.util.UUID schedule(JobLambda job, java.time.Instant instant)
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(); BackgroundJob.schedule(() -> service.doWork(), Instant.now().plusHours(5));
- Parameters:
job
- the lambda which defines the fire-and-forget jobinstant
- The moment in time at which the job will be enqueued.
-
schedule
public <T> java.util.UUID schedule(IocJobLambda<T> iocJob, java.time.Instant instant)
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:
BackgroundJob.<MyService>schedule(x -> x.doWork(), Instant.now().plusHours(5));
- Parameters:
iocJob
- the lambda which defines the fire-and-forget jobinstant
- The moment in time at which the job will be enqueued.
-
scheduleRecurringly
public java.lang.String scheduleRecurringly(JobLambda job, java.lang.String cron)
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(); BackgroundJob.scheduleRecurringly(() -> service.doWork(), Cron.daily());
- Parameters:
job
- the lambda which defines the fire-and-forget jobcron
- The cron expression defining when to run this recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
Cron
-
scheduleRecurringly
public <T> java.lang.String scheduleRecurringly(IocJobLambda<T> iocJob, java.lang.String cron)
Creates a new recurring job based on the given lambda and the given cron expression. The IoC container will be used to resolveMyService
. The jobs will be scheduled using the systemDefault timezone.An example:
BackgroundJob.<MyService>scheduleRecurringly(x -> x.doWork(), Cron.daily());
- Parameters:
iocJob
- the lambda which defines the fire-and-forget jobcron
- The cron expression defining when to run this recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
Cron
-
scheduleRecurringly
public java.lang.String scheduleRecurringly(java.lang.String id, JobLambda job, java.lang.String cron)
Creates a new recurring job based on the given id, lambda and cron expression. The jobs will be scheduled using the systemDefault timezoneAn example:
MyService service = new MyService(); BackgroundJob.scheduleRecurringly("my-recurring-job", () -> service.doWork(), Cron.daily());
- Parameters:
id
- the id of this recurring job which can be used to alter or delete itjob
- the lambda which defines the fire-and-forget jobcron
- The cron expression defining when to run this recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
Cron
-
scheduleRecurringly
public <T> java.lang.String scheduleRecurringly(java.lang.String id, IocJobLambda<T> iocJob, java.lang.String cron)
Creates a new recurring job based on the given id, lambda and cron expression. The IoC container will be used to resolveMyService
. The jobs will be scheduled using the systemDefault timezoneAn example:
BackgroundJob.<MyService>scheduleRecurringly("my-recurring-job", x -> x.doWork(), Cron.daily());
- Parameters:
id
- the id of this recurring job which can be used to alter or delete itiocJob
- the lambda which defines the fire-and-forget jobcron
- The cron expression defining when to run this recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
Cron
-
scheduleRecurringly
public java.lang.String scheduleRecurringly(java.lang.String id, JobLambda job, java.lang.String cron, java.time.ZoneId zoneId)
Creates a new recurring job based on the given id, lambda, cron expression andZoneId
.An example:
MyService service = new MyService(); BackgroundJob.scheduleRecurringly("my-recurring-job", () -> service.doWork(), Cron.daily(), ZoneId.of("Europe/Brussels"));
- Parameters:
id
- the id of this recurring job which can be used to alter or delete itjob
- the lambda which defines the fire-and-forget jobcron
- The cron expression defining when to run this recurring jobzoneId
- The zoneId (timezone) of when to run this recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
Cron
-
scheduleRecurringly
public <T> java.lang.String scheduleRecurringly(java.lang.String id, IocJobLambda<T> iocJob, java.lang.String cron, java.time.ZoneId zoneId)
Creates a new recurring job based on the given id, lambda, cron expression andZoneId
. The IoC container will be used to resolveMyService
.An example:
BackgroundJob.<MyService>scheduleRecurringly("my-recurring-job", x -> x.doWork(), Cron.daily(), ZoneId.of("Europe/Brussels"));
- Parameters:
id
- the id of this recurring job which can be used to alter or delete itiocJob
- the lambda which defines the fire-and-forget jobcron
- The cron expression defining when to run this recurring jobzoneId
- The zoneId (timezone) of when to run this recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
Cron
-
deleteRecurringly
public void deleteRecurringly(java.lang.String id)
Deletes the recurring job based on the given id.An example:
BackgroundJob.deleteRecurringly("my-recurring-job"));
- Parameters:
id
- the id of the recurring job to delete
-
-