Class BackgroundJob

java.lang.Object
org.jobrunr.scheduling.BackgroundJob

public class BackgroundJob extends 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 the JobScheduler which exposes the same methods.
  • Method Details

    • create

      public static JobId create(JobBuilder jobBuilder)
      Creates a new Job using a JobBuilder 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

      public static void create(Stream<JobBuilder> jobBuilderStream)
      Creates a new Job for each JobBuilder and provides an alternative to the job annotation.
      Parameters:
      jobBuilderStream - the jobBuilders for which to create jobs.
    • 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(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 job
      job - the lambda which defines the fire-and-forget job
      Returns:
      the id of the job
    • enqueue

      public 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 as jobFromStream.
      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 jobs
      jobFromStream - the lambda which defines the fire-and-forget job to create for each item in the input
    • 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 resolve MyService.
      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(UUID id, IocJobLambda<S> iocJob)
      Creates a new fire-and-forget job based on a given lambda. The IoC container will be used to resolve MyService. 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 job
      iocJob - the lambda which defines the fire-and-forget job
      Returns:
      the id of the job
    • enqueue

      public 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 as jobFromStream. The IoC container will be used to resolve MyService.
      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 jobs
      iocJobFromStream - the lambda which defines the fire-and-forget job to create for each item in the input
    • schedule

      public 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.
      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(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. 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 job
      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 <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. The IoC container will be used to resolve MyService.
      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(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. The IoC container will be used to resolve MyService. 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 job
      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 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.
      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(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. 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 job
      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 <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. The IoC container will be used to resolve MyService.
      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(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. The IoC container will be used to resolve MyService. 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 job
      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 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.
      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 Instant
      job - the lambda which defines the fire-and-forget job
      Returns:
      the id of the Job
    • schedule

      public 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. 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 job
      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
      job - the lambda which defines the fire-and-forget job
      Returns:
      the id of the Job
    • schedule

      public 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. The IoC container will be used to resolve MyService.
      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 Instant
      iocJob - the lambda which defines the fire-and-forget job
      Returns:
      the id of the Job
    • schedule

      public 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. The IoC container will be used to resolve MyService. 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 job
      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
      iocJob - the lambda which defines the fire-and-forget job
      Returns:
      the id of the Job
    • schedule

      public static JobId schedule(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(UUID id, 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 job
      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 <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. The IoC container will be used to resolve MyService.
      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(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. The IoC container will be used to resolve MyService. 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 job
      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
    • delete

      public static void delete(UUID id)
      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

      public static void delete(JobId jobId)
      See Also:
    • scheduleRecurrently

      public static String scheduleRecurrently(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 job
      job - 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 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 resolve MyService. 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 job
      iocJob - 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 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. The jobs will be scheduled using the systemDefault timezone
      An 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 it
      cron - The cron expression defining when to run this recurring job
      job - 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, IocJobLambda<S> iocJob)
      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 resolve MyService. The jobs will be scheduled using the systemDefault timezone
      An 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 it
      cron - The cron expression defining when to run this recurring job
      iocJob - 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 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.
      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 it
      cron - The cron expression defining when to run this recurring job
      zoneId - The zoneId (timezone) of when to run this recurring job
      job - 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 resolve MyService.
      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 it
      cron - The cron expression defining when to run this recurring job
      zoneId - The zoneId (timezone) of when to run this recurring job
      iocJob - 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 String scheduleRecurrently(Duration duration, JobLambda job)
      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 job
      job - 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

      public static <S> String scheduleRecurrently(Duration duration, IocJobLambda<S> iocJob)
      Creates a new recurring job based on the given duration and the given lambda. The IoC container will be used to resolve MyService. 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 job
      iocJob - 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

      public 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. 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 it
      duration - the duration defining the time between each instance of this recurring job
      job - 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

      public 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. The IoC container will be used to resolve MyService. 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 it
      duration - the duration defining the time between each instance of this recurring job
      iocJob - 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

      public static String createRecurrently(RecurringJobBuilder recurringJobBuilder)
      Creates a new or alters the existing recurring job based on the given RecurringJobBuilder.
      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

      public static void delete(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)