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
delete(java.lang.String id)
Deletes the recurring job based on the given id.void
delete(java.util.UUID id)
Deletes a job and sets it's state to DELETED.void
delete(JobId jobId)
<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(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(IocJobLambda<S> 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.<S> JobId
schedule(IocJobLambda<S> 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.<S> JobId
schedule(IocJobLambda<S> 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.<S> JobId
schedule(IocJobLambda<S> 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.JobId
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.JobId
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.JobId
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.JobId
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.<S> java.lang.String
scheduleRecurrently(java.lang.String id, IocJobLambda<S> iocJob, java.lang.String cron)
Creates a new recurring job based on the given id, lambda and cron expression.<S> java.lang.String
scheduleRecurrently(java.lang.String id, IocJobLambda<S> 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
scheduleRecurrently(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
scheduleRecurrently(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
.<S> java.lang.String
scheduleRecurrently(IocJobLambda<S> iocJob, java.lang.String cron)
Creates a new recurring job based on the given lambda and the given cron expression.java.lang.String
scheduleRecurrently(JobLambda job, java.lang.String cron)
Creates a new recurring job based on the given lambda and the given cron expression.void
shutdown()
Utility method to register the shutdown of JobRunr in various containers - it is even automatically called by Spring Framework.
-
-
-
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 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 <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,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(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(); jobScheduler.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.- Returns:
- the id of the Job
-
schedule
public <S> JobId schedule(IocJobLambda<S> 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:
jobScheduler.<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.- Returns:
- the id of the Job
-
schedule
public JobId 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(); jobScheduler.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.- Returns:
- the id of the Job
-
schedule
public <S> JobId schedule(IocJobLambda<S> 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:
jobScheduler.<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.- Returns:
- the id of the Job
-
schedule
public JobId 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(); jobScheduler.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- Returns:
- the id of the Job
-
schedule
public <S> JobId schedule(IocJobLambda<S> 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:
jobScheduler.<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- Returns:
- the id of the Job
-
schedule
public JobId 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(); jobScheduler.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.- Returns:
- the id of the Job
-
schedule
public <S> JobId schedule(IocJobLambda<S> 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:
jobScheduler.<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.- Returns:
- the id of the Job
-
delete
public void delete(java.util.UUID id)
Deletes a job and sets it's state to DELETED. If the job is being processed, it will be interrupted.- Parameters:
id
- the id of the job
-
delete
public void delete(JobId jobId)
- See Also:
delete(UUID)
-
scheduleRecurrently
public java.lang.String scheduleRecurrently(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(); jobScheduler.scheduleRecurrently(() -> 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
-
scheduleRecurrently
public <S> java.lang.String scheduleRecurrently(IocJobLambda<S> 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:
jobScheduler.<MyService>scheduleRecurrently(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
-
scheduleRecurrently
public java.lang.String scheduleRecurrently(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(); jobScheduler.scheduleRecurrently("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
-
scheduleRecurrently
public <S> java.lang.String scheduleRecurrently(java.lang.String id, IocJobLambda<S> 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:
jobScheduler.<MyService>scheduleRecurrently("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
-
scheduleRecurrently
public java.lang.String scheduleRecurrently(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(); jobScheduler.scheduleRecurrently("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
-
scheduleRecurrently
public <S> java.lang.String scheduleRecurrently(java.lang.String id, IocJobLambda<S> 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:
jobScheduler.<MyService>scheduleRecurrently("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
-
delete
public void delete(java.lang.String id)
Deletes the recurring job based on the given id.An example:
jobScheduler.delete("my-recurring-job"));
- Parameters:
id
- the id of the recurring job to delete
-
shutdown
public void shutdown()
Utility method to register the shutdown of JobRunr in various containers - it is even automatically called by Spring Framework. Note that this will stop the BackgroundJobServer, the Dashboard and the StorageProvider. JobProcessing will stop and enqueueing new jobs will fail.
-
-