Class JobScheduler


  • public class JobScheduler
    extends java.lang.Object
    Provides methods for creating fire-and-forget, delayed and recurring jobs as well as to delete existing background jobs.
    • Constructor Summary

      Constructors 
      Constructor Description
      JobScheduler​(StorageProvider storageProvider)
      Creates a new JobScheduler using the provided storageProvider
      JobScheduler​(StorageProvider storageProvider, java.util.List<JobFilter> jobFilters)
      Creates a new JobScheduler using the provided storageProvider and the list of JobFilters that will be used for every background job
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void delete​(java.lang.String id)
      Deletes the recurring job based on the given id.
      void delete​(java.util.UUID id)
      Deletes a job and sets it's state to DELETED.
      void delete​(JobId jobId)  
      <S,​T>
      void
      enqueue​(java.util.stream.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.
      <T> void enqueue​(java.util.stream.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.
      <S> JobId enqueue​(IocJobLambda<S> iocJob)
      Creates a new fire-and-forget job based on a given lambda.
      JobId enqueue​(JobLambda job)
      Creates a new fire-and-forget job based on a given lambda.
      <S> JobId schedule​(IocJobLambda<S> 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.
      <S> JobId schedule​(IocJobLambda<S> 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.
      <S> JobId schedule​(IocJobLambda<S> 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.
      <S> JobId schedule​(IocJobLambda<S> 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.
      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.
      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.
      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.
      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.
      <S> java.lang.String scheduleRecurringly​(java.lang.String id, IocJobLambda<S> iocJob, java.lang.String cron)
      Creates a new recurring job based on the given id, lambda and cron expression.
      <S> java.lang.String scheduleRecurringly​(java.lang.String id, IocJobLambda<S> iocJob, java.lang.String cron, java.time.ZoneId zoneId)
      Creates a new recurring job based on the given id, lambda, cron expression and ZoneId.
      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.
      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 and ZoneId.
      <S> java.lang.String scheduleRecurringly​(IocJobLambda<S> iocJob, java.lang.String cron)
      Creates a new recurring job based on the given lambda and the given cron expression.
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JobScheduler

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

        public JobScheduler​(StorageProvider storageProvider,
                            java.util.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
    • Method Detail

      • 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 <T> void enqueue​(java.util.stream.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,​T> void enqueue​(java.util.stream.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​(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();
              jobScheduler.schedule(() -> service.doWork(), ZonedDateTime.now().plusHours(5));
         
        Parameters:
        job - the lambda which defines the fire-and-forget job
        zonedDateTime - The moment in time at which the job will be enqueued.
        Returns:
        the id of the Job
      • schedule

        public <S> JobId schedule​(IocJobLambda<S> 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 resolve MyService.
        An example:
        
              jobScheduler.<MyService>schedule(x -> x.doWork(), ZonedDateTime.now().plusHours(5));
         
        Parameters:
        iocJob - the lambda which defines the fire-and-forget job
        zonedDateTime - The moment in time at which the job will be enqueued.
        Returns:
        the id of the Job
      • schedule

        public 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();
              jobScheduler.schedule(() -> service.doWork(), OffsetDateTime.now().plusHours(5));
         
        Parameters:
        job - the lambda which defines the fire-and-forget job
        offsetDateTime - The moment in time at which the job will be enqueued.
        Returns:
        the id of the Job
      • schedule

        public <S> JobId schedule​(IocJobLambda<S> 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 resolve MyService.
        An example:
        
              jobScheduler.<MyService>schedule(x -> x.doWork(), OffsetDateTime.now().plusHours(5));
         
        Parameters:
        iocJob - the lambda which defines the fire-and-forget job
        offsetDateTime - The moment in time at which the job will be enqueued.
        Returns:
        the id of the Job
      • schedule

        public 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();
              jobScheduler.schedule(() -> service.doWork(), LocalDateTime.now().plusHours(5));
         
        Parameters:
        job - the lambda which defines the fire-and-forget 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
        Returns:
        the id of the Job
      • schedule

        public <S> JobId schedule​(IocJobLambda<S> 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 resolve MyService.
        An example:
        
              jobScheduler.<MyService>schedule(x -> x.doWork(), LocalDateTime.now().plusHours(5));
         
        Parameters:
        iocJob - the lambda which defines the fire-and-forget 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
        Returns:
        the id of the Job
      • schedule

        public 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();
              jobScheduler.schedule(() -> service.doWork(), Instant.now().plusHours(5));
         
        Parameters:
        job - the lambda which defines the fire-and-forget job
        instant - The moment in time at which the job will be enqueued.
        Returns:
        the id of the Job
      • schedule

        public <S> JobId schedule​(IocJobLambda<S> 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 resolve MyService.
        An example:
        
              jobScheduler.<MyService>schedule(x -> x.doWork(), Instant.now().plusHours(5));
         
        Parameters:
        iocJob - the lambda which defines the fire-and-forget job
        instant - The moment in time at which the job will be enqueued.
        Returns:
        the id of the Job
      • delete

        public 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
      • scheduleRecurringly

        public 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();
              jobScheduler.scheduleRecurringly(() -> service.doWork(), Cron.daily());
         
        Parameters:
        job - the lambda which defines the fire-and-forget job
        cron - 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 <S> java.lang.String scheduleRecurringly​(IocJobLambda<S> 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 resolve MyService. The jobs will be scheduled using the systemDefault timezone.
        An example:
        
              jobScheduler.<MyService>scheduleRecurringly(x -> x.doWork(), Cron.daily());
         
        Parameters:
        iocJob - the lambda which defines the fire-and-forget job
        cron - 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 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 timezone
        An example:
        
              MyService service = new MyService();
              jobScheduler.scheduleRecurringly("my-recurring-job", () -> service.doWork(), Cron.daily());
         
        Parameters:
        id - the id of this recurring job which can be used to alter or delete it
        job - the lambda which defines the fire-and-forget job
        cron - 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 <S> java.lang.String scheduleRecurringly​(java.lang.String id,
                                                        IocJobLambda<S> 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 resolve MyService. The jobs will be scheduled using the systemDefault timezone
        An example:
        
              jobScheduler.<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 it
        iocJob - the lambda which defines the fire-and-forget job
        cron - 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 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 and ZoneId.
        An example:
        
              MyService service = new MyService();
              jobScheduler.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 it
        job - the lambda which defines the fire-and-forget job
        cron - The cron expression defining when to run this recurring job
        zoneId - 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 <S> java.lang.String scheduleRecurringly​(java.lang.String id,
                                                        IocJobLambda<S> iocJob,
                                                        java.lang.String cron,
                                                        java.time.ZoneId zoneId)
        Creates a new recurring job based on the given id, lambda, cron expression and ZoneId. The IoC container will be used to resolve MyService.
        An example:
        
              jobScheduler.<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 it
        iocJob - the lambda which defines the fire-and-forget job
        cron - The cron expression defining when to run this recurring job
        zoneId - 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 void delete​(java.lang.String id)
        Deletes the recurring job based on the given id.
        An example:
        
              jobScheduler.delete("my-recurring-job"));
         
        Parameters:
        id - the id of the recurring job to delete