Class JobScheduler


public class JobScheduler extends AbstractJobScheduler
Provides methods for creating fire-and-forget, delayed and recurring jobs as well as to delete existing background jobs.

This JobScheduler allows to schedule jobs by means of a Java 8 lambda which is analyzed.

  • Constructor Details

    • JobScheduler

      public JobScheduler(StorageProvider storageProvider)
      Creates a new JobScheduler using the provided storageProvider
      Parameters:
      storageProvider - the storageProvider to use
    • JobScheduler

      public JobScheduler(StorageProvider storageProvider, List<JobFilter> jobFilters)
      Creates a new JobScheduler using the provided storageProvider and the list of JobFilters that will be used for every background job
      Parameters:
      storageProvider - the storageProvider to use
      jobFilters - list of jobFilters that will be used for every job
    • JobScheduler

      public JobScheduler(StorageProvider storageProvider, JobDetailsGenerator jobDetailsGenerator, List<JobFilter> jobFilters)
  • Method Details

    • create

      public 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 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 JobId enqueue(JobLambda job)
      Creates a new fire-and-forget job based on a given lambda.
      An example:
      
                  MyService service = new MyService();
                  jobScheduler.enqueue(() -> service.doWork());
             
      Parameters:
      job - the lambda which defines the fire-and-forget job
      Returns:
      the id of the job
    • enqueue

      public JobId enqueue(UUID id, JobLambda job)
      Creates a new fire-and-forget job based on the given lambda. If a job with that id already exists, JobRunr will not save it again.
      An example:
      
                  MyService service = new MyService();
                  jobScheduler.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 <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();
            jobScheduler.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 <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:
      
                  jobScheduler.<MyService>enqueue(x -> x.doWork());
             
      Parameters:
      iocJob - the lambda which defines the fire-and-forget job
      Returns:
      the id of the job
    • enqueue

      public <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:
      
                  jobScheduler.<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 <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();
            jobScheduler.<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 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();
            jobScheduler.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 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();
            jobScheduler.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 <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:
      
            jobScheduler.<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 <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:
      
            jobScheduler.<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 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();
            jobScheduler.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 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();
            jobScheduler.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 <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:
      
            jobScheduler.<MyService>schedule(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 <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:
      
            jobScheduler.<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 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();
            jobScheduler.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 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();
            jobScheduler.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 <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:
      
            jobScheduler.<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 <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:
      
            jobScheduler.<MyService>schedule(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 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();
            jobScheduler.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 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();
            jobScheduler.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 <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:
      
            jobScheduler.<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 <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:
      
            jobScheduler.<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
    • createRecurrently

      public String createRecurrently(RecurringJobBuilder recurringJobBuilder)
      Creates a new or alters the existing recurring job based on the given RecurringJobBuilder (using id, cron expression and lambda). If no zoneId is set on the builder the jobs will be scheduled using the systemDefault timezone.
      An example:
      
            jobScheduler.createRecurrently(aRecurringJob()
                                                .withCron("* * 0 * * *")
                                                .withDetails(() -> service.doWork());
       
      Parameters:
      recurringJobBuilder - the builder describing your recurring job.
      Returns:
      the id of this recurring job which can be used to alter or delete it
      See Also:
    • scheduleRecurrently

      public String scheduleRecurrently(String cron, JobLambda job)
      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();
            jobScheduler.scheduleRecurrently(Cron.daily(), () -> service.doWork());
       
      Parameters:
      cron - The cron expression defining when to run this recurring job
      job - the lambda 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 <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:
      
            jobScheduler.<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 recurring job
      Returns:
      the id of this recurring job which can be used to alter or delete it
      See Also:
    • scheduleRecurrently

      public 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();
            jobScheduler.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 recurring job
      Returns:
      the id of this recurring job which can be used to alter or delete it
      See Also:
    • scheduleRecurrently

      public <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:
      
            jobScheduler.<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 recurring job
      Returns:
      the id of this recurring job which can be used to alter or delete it
      See Also:
    • scheduleRecurrently

      public 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();
            jobScheduler.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 recurring job
      Returns:
      the id of this recurring job which can be used to alter or delete it
      See Also:
    • scheduleRecurrently

      public <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:
      
            jobScheduler.<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 recurring job
      Returns:
      the id of this recurring job which can be used to alter or delete it
      See Also:
    • scheduleRecurrently

      public 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 recurring job
      Returns:
      the id of this recurring job which can be used to alter or delete it
    • scheduleRecurrently

      public <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 recurring job
      Returns:
      the id of this recurring job which can be used to alter or delete it
    • scheduleRecurrently

      public 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 recurring job
      Returns:
      the id of this recurring job which can be used to alter or delete it
    • scheduleRecurrently

      public <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 recurring job
      Returns:
      the id of this recurring job which can be used to alter or delete it