Class BackgroundJobRequest
java.lang.Object
org.jobrunr.scheduling.BackgroundJobRequest
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
JobRequestScheduler which exposes the same methods.-
Method Summary
Modifier and TypeMethodDescriptionstatic voidcreate(Stream<JobBuilder> jobBuilderStream) Creates a newJobfor eachJobBuilderand provides an alternative to the job annotation.static JobIdcreate(JobBuilder jobBuilder) Creates a newJobusing aJobBuilderthat can be enqueued or scheduled and provides an alternative to the job annotation.static StringcreateRecurrently(RecurringJobBuilder recurringJobBuilder) Creates a new or alters the existing recurring job based on the givenRecurringJobBuilder.static voidDeletes a job and sets its state to DELETED.static voidSeedelete(UUID)static voidDeletes the recurring job based on the given id.static voidenqueue(Stream<? extends JobRequest> input) Creates new fire-and-forget jobs for each item in the input stream.static JobIdenqueue(UUID id, JobRequest jobRequest) Creates a new fire-and-forget job based on a givenJobRequest.static JobIdenqueue(JobRequest jobRequest) Creates a new fire-and-forget job based on a givenJobRequest.static JobIdschedule(Temporal scheduleAt, JobRequest jobRequest) Creates a new fire-and-forget job based on the givenJobRequestand schedules it to be enqueued at the given moment of time.static JobIdschedule(UUID id, Temporal scheduleAt, JobRequest jobRequest) Creates a new fire-and-forget job based on the givenJobRequestand schedules it to be enqueued at the given moment of time.static StringscheduleRecurrently(String id, String cron, ZoneId zoneId, JobRequest jobRequest) Creates a new or alters the existing recurring job based on the given id, cron expression,ZoneIdandJobRequest.static StringscheduleRecurrently(String id, String cron, JobRequest jobRequest) Creates a new or alters the existing recurring job based on the given id, cron expression andJobRequest.static StringscheduleRecurrently(String id, Duration duration, JobRequest jobRequest) Creates a new or alters the existing recurring job based on the given id, duration andJobRequest.static StringscheduleRecurrently(String cron, JobRequest jobRequest) Creates a new recurring job based on the given cron expression and the givenJobRequest.static StringscheduleRecurrently(Duration duration, JobRequest jobRequest) Creates a new recurring job based on the given duration and the givenJobRequest.static voidsetJobRequestScheduler(JobRequestScheduler jobRequestScheduler)
-
Method Details
-
create
Creates a newJobusing aJobBuilderthat can be enqueued or scheduled and provides an alternative to the job annotation.- Parameters:
jobBuilder- theJobBuilderwith all the details of the job- Returns:
- the id of the job
-
create
Creates a newJobfor eachJobBuilderand 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 givenJobRequest. JobRunr will try to find the JobRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor.An example:
BackgroundJobRequest.enqueue(new MyJobRequest());- Parameters:
jobRequest- theJobRequestwhich defines the fire-and-forget job.- Returns:
- the id of the job
-
enqueue
Creates a new fire-and-forget job based on a givenJobRequest. JobRunr will try to find the JobRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor.An example:
BackgroundJobRequest.enqueueJobRequest(id, new MyJobRequest());- Parameters:
id- the uuid with which to save the jobjobRequest- theJobRequestwhich 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. JobRunr will try to find the JobRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor.An example:
Stream<MyJobRequest> workStream = getWorkStream(); BackgroundJobRequest.enqueue(workStream);- Parameters:
input- the stream of jobRequests for which to create fire-and-forget jobs
-
schedule
Creates a new fire-and-forget job based on the givenJobRequestand schedules it to be enqueued at the given moment of time. JobRunr will try to find the JobRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor.Supported
Temporalimplementations:CarbonAwarePeriodto schedule a Carbon Aware jobInstantChronoLocalDateTime(e.g.,LocalDateTime): converted toInstantusingZoneId.systemDefault()ChronoZonedDateTime(e.g.,ZonedDateTime)OffsetDateTime
An Example with
Instant:BackgroundJobRequest.schedule(Instant.now().plus(5, ChronoUnit.HOURS), new MyJobRequest());An Example with
CarbonAwarePeriod:BackgroundJobRequest.schedule(CarbonAware.between(Instant.now(), Instant.now().plus(5, ChronoUnit.HOURS)), new MyJobRequest());- Parameters:
scheduleAt- the moment in time at which the job will be enqueued.jobRequest- theJobRequestwhich defines the fire-and-forget job- Returns:
- the id of the Job
-
schedule
Creates a new fire-and-forget job based on the givenJobRequestand schedules it to be enqueued at the given moment of time. JobRunr will try to find the JobRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor. If a job with that id already exists, JobRunr will not save it again.Supported
Temporalimplementations:CarbonAwarePeriodto schedule a Carbon Aware jobInstantChronoLocalDateTime(e.g.,LocalDateTime): converted toInstantusingZoneId.systemDefault()ChronoZonedDateTime) (e.g.,ZonedDateTime)OffsetDateTime
An Example with
Instant:BackgroundJobRequest.schedule(id, Instant.now().plus(5, ChronoUnit.HOURS), new MyJobRequest());An Example with
CarbonAwarePeriod:BackgroundJobRequest.schedule(id, CarbonAware.between(Instant.now(), Instant.now().plus(5, ChronoUnit.HOURS)), new MyJobRequest());- Parameters:
id- the uuid with which to save the jobscheduleAt- the moment in time at which the job will be enqueued.jobRequest- theJobRequestwhich 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
Seedelete(UUID) -
scheduleRecurrently
Creates a new recurring job based on the given cron expression and the givenJobRequest. JobRunr will try to find the JobRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor. The jobs will be scheduled using the systemDefault timezone.An example:
BackgroundJobRequest.scheduleRecurrently(Cron.daily(), new MyJobRequest());- Parameters:
cron- The cron expression defining when to run this recurring jobjobRequest- theJobRequestwhich defines the recurring 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 andJobRequest. JobRunr will try to find the JobRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor. The jobs will be scheduled using the systemDefault timezoneAn example:
BackgroundJobRequest.scheduleRecurrently("my-recurring-job", Cron.daily(), new MyJobRequest());- 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 jobjobRequest- the jobRequest which defines the recurring job- Returns:
- the id of this recurring job which can be used to alter or delete it
- See Also:
-
scheduleRecurrently
public static String scheduleRecurrently(String id, String cron, ZoneId zoneId, JobRequest jobRequest) Creates a new or alters the existing recurring job based on the given id, cron expression,ZoneIdandJobRequest. JobRunr will try to find the JobRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor.An example:
BackgroundJobRequest.scheduleRecurrently("my-recurring-job", Cron.daily(), ZoneId.of("Europe/Brussels"), new MyJobRequest());- 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 jobjobRequest- theJobRequestwhich defines the recurring 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 givenJobRequest. 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 jobjobRequest- the jobRequest which defines the recurring 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 andJobRequest. 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 jobjobRequest- theJobRequestwhich defines the recurring 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 * * *") .withJobLambda(() -> 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
-
deleteRecurringJob
Deletes the recurring job based on the given id.An example:
BackgroundJobRequest.deleteRecurringJob("my-recurring-job"));- Parameters:
id- the id of the recurring job to delete
-
setJobRequestScheduler
-