Package org.jobrunr.scheduling
Class BackgroundJob
- java.lang.Object
-
- org.jobrunr.scheduling.BackgroundJob
-
public class BackgroundJob extends java.lang.Object
Provides static methods for creating fire-and-forget, delayed and recurring jobs as well as to delete existing background jobs. If you prefer not to use a static accessor, you can inject theJobScheduler
which exposes the same methods.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
delete(java.lang.String id)
Deletes the recurring job based on the given id.static void
delete(java.util.UUID id)
Deletes a job and sets it's state to DELETED.static void
delete(JobId jobId)
static <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
.static <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
.static <S> JobId
enqueue(java.util.UUID id, IocJobLambda<S> iocJob)
Creates a new fire-and-forget job based on a given lambda.static JobId
enqueue(java.util.UUID id, JobLambda job)
Creates a new fire-and-forget job based on a given lambda.static <S> JobId
enqueue(IocJobLambda<S> iocJob)
Creates a new fire-and-forget job based on a given lambda.static JobId
enqueue(JobLambda job)
Creates a new fire-and-forget job based on a given lambda.static <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.static 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.static <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.static 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.static <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.static 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.static <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.static 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.static <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.static 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.static <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.static 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.static <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.static 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.static <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.static 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.static <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.static 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.static <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.static 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.static <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.static java.lang.String
scheduleRecurrently(java.lang.String cron, JobLambda job)
Creates a new recurring job based on the given cron expression and the given lambda.static void
setJobScheduler(JobScheduler jobScheduler)
-
-
-
Method Detail
-
enqueue
public static JobId 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 static JobId enqueue(java.util.UUID id, JobLambda job)
Creates a new fire-and-forget job based on a given lambda. If a job with that id already exists, JobRunr will not save it again.An example:
MyService service = new MyService(); BackgroundJob.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 static <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(); 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 static <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:
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 static <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:
BackgroundJob.<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 static <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(); 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 static 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(); BackgroundJob.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 static 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(); BackgroundJob.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 static <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:
BackgroundJob.<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 static <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:
BackgroundJob.<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 static 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(); BackgroundJob.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 static 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(); BackgroundJob.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 static <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:
BackgroundJob.<MyService>schedule(id, 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 static <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:
BackgroundJob.<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 static 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(); BackgroundJob.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 static 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(); BackgroundJob.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 static <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:
BackgroundJob.<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 static <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:
BackgroundJob.<MyService>schedule(id, 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 static 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(); BackgroundJob.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 static 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(); BackgroundJob.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 static <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:
BackgroundJob.<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 static <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:
BackgroundJob.<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
-
delete
public static 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 static void delete(JobId jobId)
- See Also:
delete(UUID)
-
scheduleRecurrently
public static java.lang.String scheduleRecurrently(java.lang.String cron, JobLambda job)
Creates a new recurring job based on the given cron expression and the given lambda. The jobs will be scheduled using the systemDefault timezone.An example:
MyService service = new MyService(); BackgroundJob.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 static <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:
BackgroundJob.<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 static 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(); BackgroundJob.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 static <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:
BackgroundJob.<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 static 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(); BackgroundJob.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 static <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:
BackgroundJob.<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
-
delete
public static void delete(java.lang.String id)
Deletes the recurring job based on the given id.An example:
BackgroundJob.delete("my-recurring-job"));
- Parameters:
id
- the id of the recurring job to delete
-
setJobScheduler
public static void setJobScheduler(JobScheduler jobScheduler)
-
-