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.
-
-
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 <TService,TItem>
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
.static <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
.static <TService> JobId
enqueue(IocJobLambda<TService> 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 <TService> JobId
schedule(IocJobLambda<TService> 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.static <TService> JobId
schedule(IocJobLambda<TService> 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.static <TService> JobId
schedule(IocJobLambda<TService> 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.static <TService> JobId
schedule(IocJobLambda<TService> 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.static 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.static 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.static 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.static 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.static <TService> java.lang.String
scheduleRecurringly(java.lang.String id, IocJobLambda<TService> iocJob, java.lang.String cron)
Creates a new recurring job based on the given id, lambda and cron expression.static <TService> java.lang.String
scheduleRecurringly(java.lang.String id, IocJobLambda<TService> iocJob, java.lang.String cron, java.time.ZoneId zoneId)
Creates a new recurring job based on the given id, lambda, cron expression andZoneId
.static 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.static 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
.static <TService> java.lang.String
scheduleRecurringly(IocJobLambda<TService> iocJob, java.lang.String cron)
Creates a new recurring job based on the given lambda and the given cron expression.static 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.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 <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 static <TService> JobId enqueue(IocJobLambda<TService> 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 <TService,TItem> 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 static 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(); 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.- Returns:
- the id of the Job
-
schedule
public static <TService> JobId schedule(IocJobLambda<TService> 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.- Returns:
- the id of the Job
-
schedule
public static 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(); 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.- Returns:
- the id of the Job
-
schedule
public static <TService> JobId schedule(IocJobLambda<TService> 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.- Returns:
- the id of the Job
-
schedule
public static 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(); 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- Returns:
- the id of the Job
-
schedule
public static <TService> JobId schedule(IocJobLambda<TService> 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- Returns:
- the id of the Job
-
schedule
public static 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(); 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.- Returns:
- the id of the Job
-
schedule
public static <TService> JobId schedule(IocJobLambda<TService> 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.- 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)
-
scheduleRecurringly
public static 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 static <TService> java.lang.String scheduleRecurringly(IocJobLambda<TService> 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 static 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 static <TService> java.lang.String scheduleRecurringly(java.lang.String id, IocJobLambda<TService> 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 static 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 static <TService> java.lang.String scheduleRecurringly(java.lang.String id, IocJobLambda<TService> 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
-
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)
-
-