Package org.jobrunr.scheduling
Class BackgroundJob
java.lang.Object
org.jobrunr.scheduling.BackgroundJob
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 the
JobScheduler
which exposes the same methods.-
Method Summary
Modifier and TypeMethodDescriptionstatic void
create
(Stream<JobBuilder> jobBuilderStream) Creates a newJob
for eachJobBuilder
and provides an alternative to the job annotation.static JobId
create
(JobBuilder jobBuilder) Creates a newJob
using aJobBuilder
that can be enqueued or scheduled and provides an alternative to the job annotation.static String
createRecurrently
(RecurringJobBuilder recurringJobBuilder) Creates a new or alters the existing recurring job based on the givenRecurringJobBuilder
.static void
Deletes the recurring job based on the given id.static void
Deletes a job and sets its state to DELETED.static void
static <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
.static <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
.static <S> JobId
enqueue
(UUID id, IocJobLambda<S> iocJob) Creates a new fire-and-forget job based on a given lambda.static JobId
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
Creates a new fire-and-forget job based on a given lambda.static <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.static JobId
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
(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
(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
(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
(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
(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
(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
(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.static JobId
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
(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.static JobId
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.static <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.static JobId
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.static <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.static JobId
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.static <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.static String
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.static <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.static String
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.static <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.static String
scheduleRecurrently
(String id, Duration duration, JobLambda job) Creates a new or alters the existing recurring job based on the given id, duration and lambda.static <S> String
scheduleRecurrently
(String cron, IocJobLambda<S> iocJob) Creates a new recurring job based on the given cron expression and the given lambda.static String
scheduleRecurrently
(String cron, JobLambda job) Creates a new recurring job based on the given cron expression and the given lambda.static <S> String
scheduleRecurrently
(Duration duration, IocJobLambda<S> iocJob) Creates a new recurring job based on the given duration and the given lambda.static String
scheduleRecurrently
(Duration duration, JobLambda job) Creates a new recurring job based on the given duration and the given lambda.static void
setJobScheduler
(JobScheduler jobScheduler)
-
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(); BackgroundJob.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 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Deletes a job and sets its state to DELETED. If the job is being processed, it will be interrupted.- Parameters:
id
- the id of the job
-
delete
- See Also:
-
scheduleRecurrently
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:
-
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:
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:
-
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(); 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:
-
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:
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:
-
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(); 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:
-
scheduleRecurrently
public static <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:
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:
-
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 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
-
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 fire-and-forget 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 fire-and-forget 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 fire-and-forget job- Returns:
- the id of this recurring job which can be used to alter or delete it
-
createRecurrently
Creates a new or alters the existing recurring job based on the givenRecurringJobBuilder
.An example:
BackgroundJob.createRecurrently(aRecurringJob() .withCron("* * 0 * * *") .withDetails(() -> service.sendMail(toRequestParam, subjectRequestParam, bodyRequestParam));
- Parameters:
recurringJobBuilder
- the builder defining the recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
-
delete
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
-