org.jobrunr.scheduling.BackgroundJobRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jobrunr Show documentation
Show all versions of jobrunr Show documentation
An easy way to perform background processing on the JVM. Backed by persistent storage. Open and free for commercial use.
package org.jobrunr.scheduling;
import org.jobrunr.jobs.JobId;
import org.jobrunr.jobs.lambdas.JobRequest;
import java.time.*;
import java.util.UUID;
import java.util.stream.Stream;
import static java.time.ZoneId.systemDefault;
/**
* 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 {@link JobRequestScheduler} which exposes the same methods.
*
* @author Ronald Dehuysser
*/
public class BackgroundJobRequest {
private BackgroundJobRequest() {
}
private static JobRequestScheduler jobRequestScheduler;
/**
* Creates a new {@link org.jobrunr.jobs.Job} using a {@link JobBuilder} that can be enqueued or scheduled and provides an alternative to the job annotation.
* @param jobBuilder the jobBuilder with all the details of the job
* @return the id of the job
*/
public static JobId create(JobBuilder jobBuilder) {
return jobRequestScheduler.create(jobBuilder);
}
/**
* Creates a new {@link org.jobrunr.jobs.Job} for each {@link JobBuilder} and provides an alternative to the job annotation.
*
* @param jobBuilderStream the jobBuilders for which to create jobs.
*/
public static void create(Stream jobBuilderStream) {
jobRequestScheduler.create(jobBuilderStream);
}
/**
* Creates a new fire-and-forget job based on a given jobRequest. 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:
* {@code
* BackgroundJobRequest.enqueue(new MyJobRequest());
* }
*
* @param jobRequest the jobRequest which defines the fire-and-forget job.
* @return the id of the job
*/
public static JobId enqueue(JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.enqueue(jobRequest);
}
/**
* Creates a new fire-and-forget job based on a given jobRequest. 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:
* {@code
* BackgroundJobRequest.enqueueJobRequest(id, new MyJobRequest());
* }
*
* @param id the uuid with which to save the job
* @param jobRequest the jobRequest which defines the fire-and-forget job.
* @return the id of the job
*/
public static JobId enqueue(UUID id, JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.enqueue(id, jobRequest);
}
/**
* 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:
* {@code
* Stream workStream = getWorkStream();
* BackgroundJobRequest.enqueue(workStream);
* }
*
* @param input the stream of jobRequests for which to create fire-and-forget jobs
*/
public static void enqueue(Stream extends JobRequest> input) {
verifyJobScheduler();
jobRequestScheduler.enqueue(input);
}
/**
* Creates a new fire-and-forget job based on the given jobRequest and 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.
* An example:
* {@code
* BackgroundJobRequest.schedule(ZonedDateTime.now().plusHours(5), new MyJobRequest());
* }
*
* @param zonedDateTime the moment in time at which the job will be enqueued.
* @param jobRequest the jobRequest which defines the fire-and-forget job
* @return the id of the Job
*/
public static JobId schedule(ZonedDateTime zonedDateTime, JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.schedule(zonedDateTime.toInstant(), jobRequest);
}
/**
* Creates a new fire-and-forget job based on the given jobRequest and 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.
* An example:
* {@code
* BackgroundJobRequest.schedule(id, ZonedDateTime.now().plusHours(5), new MyJobRequest());
* }
*
* @param id the uuid with which to save the job
* @param zonedDateTime the moment in time at which the job will be enqueued.
* @param jobRequest the jobRequest which defines the fire-and-forget job
* @return the id of the Job
*/
public static JobId schedule(UUID id, ZonedDateTime zonedDateTime, JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.schedule(id, zonedDateTime.toInstant(), jobRequest);
}
/**
* Creates a new fire-and-forget job based on the given jobRequest and 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.
* An example:
* {@code
* BackgroundJobRequest.schedule(OffsetDateTime.now().plusHours(5), new MyJobRequest());
* }
*
* @param offsetDateTime the moment in time at which the job will be enqueued.
* @param jobRequest the jobRequest which defines the fire-and-forget job
* @return the id of the Job
*/
public static JobId schedule(OffsetDateTime offsetDateTime, JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.schedule(offsetDateTime.toInstant(), jobRequest);
}
/**
* Creates a new fire-and-forget job based on the given jobRequest and 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.
* An example:
* {@code
* BackgroundJobRequest.schedule(id, OffsetDateTime.now().plusHours(5), new MyJobRequest());
* }
*
* @param id the uuid with which to save the job
* @param offsetDateTime the moment in time at which the job will be enqueued.
* @param jobRequest the jobRequest which defines the fire-and-forget job
* @return the id of the Job
*/
public static JobId schedule(UUID id, OffsetDateTime offsetDateTime, JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.schedule(id, offsetDateTime.toInstant(), jobRequest);
}
/**
* Creates a new fire-and-forget job based on the given jobRequest and 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.
* An example:
* {@code
* BackgroundJobRequest.schedule(LocalDateTime.now().plusHours(5), new MyJobRequest());
* }
*
* @param localDateTime the moment in time at which the job will be enqueued. It will use the systemDefault ZoneId to transform it to an UTC Instant
* @param jobRequest the jobRequest which defines the fire-and-forget job
* @return the id of the Job
*/
public static JobId schedule(LocalDateTime localDateTime, JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.schedule(localDateTime.atZone(systemDefault()).toInstant(), jobRequest);
}
/**
* Creates a new fire-and-forget job based on the given jobRequest and 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.
* An example:
* {@code
* BackgroundJobRequest.schedule(id, LocalDateTime.now().plusHours(5), new MyJobRequest());
* }
*
* @param id the uuid with which to save the job
* @param localDateTime the moment in time at which the job will be enqueued. It will use the systemDefault ZoneId to transform it to an UTC Instant
* @param jobRequest the jobRequest which defines the fire-and-forget job
* @return the id of the Job
*/
public static JobId schedule(UUID id, LocalDateTime localDateTime, JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.schedule(id, localDateTime.atZone(systemDefault()).toInstant(), jobRequest);
}
/**
* Creates a new fire-and-forget job based on the given jobRequest and 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.
* An example:
* {@code
* BackgroundJobRequest.schedule(Instant.now().plusHours(5), new MyJobRequest());
* }
*
* @param instant the moment in time at which the job will be enqueued.
* @param jobRequest the lambda which defines the fire-and-forget job
* @return the id of the Job
*/
public static JobId schedule(Instant instant, JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.schedule(instant, jobRequest);
}
/**
* Creates a new fire-and-forget job based on the given jobRequest and 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.
* An example:
* {@code
* BackgroundJobRequest.schedule(id, Instant.now().plusHours(5), new MyJobRequest());
* }
*
* @param id the uuid with which to save the job
* @param instant the moment in time at which the job will be enqueued.
* @param jobRequest the jobRequest which defines the fire-and-forget job
* @return the id of the Job
*/
public static JobId schedule(UUID id, Instant instant, JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.schedule(id, instant, jobRequest);
}
/**
* Deletes a job and sets its state to DELETED. If the job is being processed, it will be interrupted.
*
* @param id the id of the job
*/
public static void delete(UUID id) {
verifyJobScheduler();
jobRequestScheduler.delete(id);
}
/**
* @see #delete(UUID)
*/
public static void delete(JobId jobId) {
delete(jobId.asUUID());
}
/**
* Creates a new recurring job based on the given cron expression and the given jobRequest. 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:
* {@code
* BackgroundJobRequest.scheduleRecurrently(Cron.daily(), new MyJobRequest());
* }
*
* @param cron The cron expression defining when to run this recurring job
* @param jobRequest the jobRequest which defines the recurring job
* @return the id of this recurring job which can be used to alter or delete it
* @see org.jobrunr.scheduling.cron.Cron
*/
public static String scheduleRecurrently(String cron, JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.scheduleRecurrently(cron, jobRequest);
}
/**
* Creates a new or alters the existing recurring job based on the given id, cron expression and jobRequest. 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:
* {@code
* BackgroundJobRequest.scheduleRecurrently("my-recurring-job", Cron.daily(), new MyJobRequest());
* }
*
* @param id the id of this recurring job which can be used to alter or delete it
* @param cron The cron expression defining when to run this recurring job
* @param jobRequest the jobRequest which defines the recurring job
* @return the id of this recurring job which can be used to alter or delete it
* @see org.jobrunr.scheduling.cron.Cron
*/
public static String scheduleRecurrently(String id, String cron, JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.scheduleRecurrently(id, cron, systemDefault(), jobRequest);
}
/**
* Creates a new or alters the existing recurring job based on the given id, cron expression, {@code ZoneId} and jobRequest. 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:
* {@code
* BackgroundJobRequest.scheduleRecurrently("my-recurring-job", Cron.daily(), ZoneId.of("Europe/Brussels"), new MyJobRequest());
* }
*
* @param id the id of this recurring job which can be used to alter or delete it
* @param cron The cron expression defining when to run this recurring job
* @param zoneId The zoneId (timezone) of when to run this recurring job
* @param jobRequest the jobRequest which defines the recurring job
* @return the id of this recurring job which can be used to alter or delete it
* @see org.jobrunr.scheduling.cron.Cron
*/
public static String scheduleRecurrently(String id, String cron, ZoneId zoneId, JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.scheduleRecurrently(id, cron, zoneId, jobRequest);
}
/**
* Creates a new recurring job based on the given duration and the given jobRequest. 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:
* {@code
* MyService service = new MyService();
* BackgroundJob.scheduleRecurrently(Duration.parse("P5D"), () -> service.doWork());
* }
*
* @param duration the duration defining the time between each instance of this recurring job
* @param jobRequest the jobRequest which defines the recurring job
* @return the id of this recurring job which can be used to alter or delete it
*/
public static String scheduleRecurrently(Duration duration, JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.scheduleRecurrently(duration, jobRequest);
}
/**
* Creates a new or alters the existing recurring job based on the given id, duration and jobRequest. 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:
* {@code
* MyService service = new MyService();
* BackgroundJob.scheduleRecurrently("my-recurring-job", Duration.parse("P5D"), () -> service.doWork());
* }
*
* @param id the id of this recurring job which can be used to alter or delete it
* @param duration the duration defining the time between each instance of this recurring job
* @param jobRequest the jobRequest which defines the recurring job
* @return the id of this recurring job which can be used to alter or delete it
*/
public static String scheduleRecurrently(String id, Duration duration, JobRequest jobRequest) {
verifyJobScheduler();
return jobRequestScheduler.scheduleRecurrently(id, duration, jobRequest);
}
/**
* Creates a new or alters the existing recurring job based on the given {@link RecurringJobBuilder}.
* An example:
* {@code
*
* BackgroundJob.createRecurrently(aRecurringJob()
* .withCron("* * 0 * * *")
* .withDetails(() -> service.sendMail(toRequestParam, subjectRequestParam, bodyRequestParam));
* }
*
* @param recurringJobBuilder the builder defining the recurring job
* @return the id of this recurring job which can be used to alter or delete it
*/
public static String createRecurrently(RecurringJobBuilder recurringJobBuilder) {
verifyJobScheduler();
return jobRequestScheduler.createRecurrently(recurringJobBuilder);
}
/**
* Deletes the recurring job based on the given id.
* An example:
* {@code
* BackgroundJobRequest.deleteRecurringJob("my-recurring-job"));
* }
*
* @param id the id of the recurring job to delete
*/
public static void deleteRecurringJob(String id) {
verifyJobScheduler();
jobRequestScheduler.deleteRecurringJob(id);
}
private static void verifyJobScheduler() {
if (jobRequestScheduler != null) return;
throw new IllegalStateException("The JobRequestScheduler has not been initialized. Use the fluent JobRunr.configure() API to setup JobRunr or set the JobRequestScheduler via the static setter method.");
}
public static void setJobRequestScheduler(JobRequestScheduler jobRequestScheduler) {
BackgroundJobRequest.jobRequestScheduler = jobRequestScheduler;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy