Class JobRequestScheduler


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

    This JobRequestScheduler allows to schedule jobs by means of an implementation of a JobRequest.

    • Constructor Summary

      Constructors 
      Constructor Description
      JobRequestScheduler​(StorageProvider storageProvider)
      Creates a new JobRequestScheduler using the provided storageProvider
      JobRequestScheduler​(StorageProvider storageProvider, java.util.List<JobFilter> jobFilters)
      Creates a new JobRequestScheduler 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 enqueue​(java.util.stream.Stream<? extends JobRequest> input)
      Creates new fire-and-forget jobs for each item in the input stream.
      JobId enqueue​(java.util.UUID id, JobRequest jobRequest)
      Creates a new fire-and-forget job based on a given jobRequest.
      JobId enqueue​(JobRequest jobRequest)
      Creates a new fire-and-forget job based on a given jobRequest.
      JobId schedule​(java.time.Instant instant, JobRequest 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.
      JobId schedule​(java.time.LocalDateTime localDateTime, JobRequest 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.
      JobId schedule​(java.time.OffsetDateTime offsetDateTime, JobRequest 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.
      JobId schedule​(java.time.ZonedDateTime zonedDateTime, JobRequest 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.
      JobId schedule​(java.util.UUID id, java.time.Instant instant, JobRequest 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.
      JobId schedule​(java.util.UUID id, java.time.LocalDateTime localDateTime, JobRequest 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.
      JobId schedule​(java.util.UUID id, java.time.OffsetDateTime offsetDateTime, JobRequest 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.
      JobId schedule​(java.util.UUID id, java.time.ZonedDateTime zonedDateTime, JobRequest 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.
      java.lang.String scheduleRecurrently​(java.lang.String id, java.lang.String cron, java.time.ZoneId zoneId, JobRequest jobRequest)
      Creates a new or alters the existing recurring job based on the given id, cron expression, ZoneId and jobRequest.
      java.lang.String scheduleRecurrently​(java.lang.String id, java.lang.String cron, JobRequest jobRequest)
      Creates a new or alters the existing recurring job based on the given id, cron expression and jobRequest.
      java.lang.String scheduleRecurrently​(java.lang.String id, java.time.Duration duration, JobRequest jobRequest)
      Creates a new or alters the existing recurring job based on the given id, duration and jobRequest.
      java.lang.String scheduleRecurrently​(java.lang.String cron, JobRequest jobRequest)
      Creates a new recurring job based on the given cron expression and the given jobRequest.
      java.lang.String scheduleRecurrently​(java.time.Duration duration, JobRequest jobRequest)
      Creates a new recurring job based on the given duration and the given jobRequest.
      • Methods inherited from class java.lang.Object

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

      • JobRequestScheduler

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

        public JobRequestScheduler​(StorageProvider storageProvider,
                                   java.util.List<JobFilter> jobFilters)
        Creates a new JobRequestScheduler 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​(JobRequest 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:
        
                    jobScheduler.enqueue(new MyJobRequest());
               
        Parameters:
        jobRequest - the jobRequest which defines the fire-and-forget job.
        Returns:
        the id of the job
      • enqueue

        public JobId enqueue​(java.util.UUID id,
                             JobRequest 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:
        
                    jobScheduler.enqueue(id, new MyJobRequest());
               
        Parameters:
        id - the uuid with which to save the job
        jobRequest - the jobRequest which defines the fire-and-forget job.
        Returns:
        the id of the job
      • enqueue

        public void enqueue​(java.util.stream.Stream<? extends JobRequest> input)
        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();
              jobScheduler.enqueue(workStream);
         
        Parameters:
        input - the stream of jobRequests for which to create fire-and-forget jobs
      • schedule

        public JobId schedule​(java.time.ZonedDateTime zonedDateTime,
                              JobRequest 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:
        
              jobScheduler.schedule(ZonedDateTime.now().plusHours(5), new MyJobRequest());
         
        Parameters:
        zonedDateTime - the moment in time at which the job will be enqueued.
        jobRequest - the jobRequest which defines the fire-and-forget job
        Returns:
        the id of the Job
      • schedule

        public JobId schedule​(java.util.UUID id,
                              java.time.ZonedDateTime zonedDateTime,
                              JobRequest 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:
        
              jobScheduler.schedule(id, ZonedDateTime.now().plusHours(5), new MyJobRequest());
         
        Parameters:
        id - the uuid with which to save the job
        zonedDateTime - the moment in time at which the job will be enqueued.
        jobRequest - the jobRequest which defines the fire-and-forget job
        Returns:
        the id of the Job
      • schedule

        public JobId schedule​(java.time.OffsetDateTime offsetDateTime,
                              JobRequest 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:
        
              jobScheduler.schedule(OffsetDateTime.now().plusHours(5), new MyJobRequest());
         
        Parameters:
        offsetDateTime - the moment in time at which the job will be enqueued.
        jobRequest - the jobRequest which defines the fire-and-forget job
        Returns:
        the id of the Job
      • schedule

        public JobId schedule​(java.util.UUID id,
                              java.time.OffsetDateTime offsetDateTime,
                              JobRequest 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:
        
              jobScheduler.schedule(id, OffsetDateTime.now().plusHours(5), new MyJobRequest());
         
        Parameters:
        id - the uuid with which to save the job
        offsetDateTime - the moment in time at which the job will be enqueued.
        jobRequest - the jobRequest which defines the fire-and-forget job
        Returns:
        the id of the Job
      • schedule

        public JobId schedule​(java.time.LocalDateTime localDateTime,
                              JobRequest 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:
        
              jobScheduler.schedule(LocalDateTime.now().plusHours(5), new MyJobRequest());
         
        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
        jobRequest - the jobRequest which defines the fire-and-forget job
        Returns:
        the id of the Job
      • schedule

        public JobId schedule​(java.util.UUID id,
                              java.time.LocalDateTime localDateTime,
                              JobRequest 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:
        
              jobScheduler.schedule(id, LocalDateTime.now().plusHours(5), new MyJobRequest());
         
        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
        jobRequest - the jobRequest which defines the fire-and-forget job
        Returns:
        the id of the Job
      • schedule

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

        public JobId schedule​(java.util.UUID id,
                              java.time.Instant instant,
                              JobRequest 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:
        
              jobScheduler.schedule(id, Instant.now().plusHours(5), new MyJobRequest());
         
        Parameters:
        id - the uuid with which to save the job
        instant - the moment in time at which the job will be enqueued.
        jobRequest - the jobRequest which defines the fire-and-forget job
        Returns:
        the id of the Job
      • scheduleRecurrently

        public java.lang.String scheduleRecurrently​(java.lang.String cron,
                                                    JobRequest jobRequest)
        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:
        
              jobScheduler.scheduleRecurrently(Cron.daily(), new MyJobRequest());
         
        Parameters:
        cron - The cron expression defining when to run this recurring job
        jobRequest - 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:
        Cron
      • scheduleRecurrently

        public java.lang.String scheduleRecurrently​(java.lang.String id,
                                                    java.lang.String cron,
                                                    JobRequest 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:
        
              jobScheduler.scheduleRecurrently("my-recurring-job", Cron.daily(), new MyJobRequest());
         
        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
        jobRequest - 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:
        Cron
      • scheduleRecurrently

        public java.lang.String scheduleRecurrently​(java.lang.String id,
                                                    java.lang.String cron,
                                                    java.time.ZoneId zoneId,
                                                    JobRequest jobRequest)
        Creates a new or alters the existing recurring job based on the given id, cron expression, 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:
        
              jobScheduler.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 it
        cron - The cron expression defining when to run this recurring job
        zoneId - The zoneId (timezone) of when to run this recurring job
        jobRequest - 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:
        Cron
      • scheduleRecurrently

        public java.lang.String scheduleRecurrently​(java.time.Duration duration,
                                                    JobRequest jobRequest)
        Creates a new recurring job based on the given duration 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 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"), new MyJobRequest());
         
        Parameters:
        duration - the duration defining the time between each instance of this recurring job.
        jobRequest - the jobRequest which defines the recurring job
        Returns:
        the id of this recurring job which can be used to alter or delete it
      • scheduleRecurrently

        public java.lang.String scheduleRecurrently​(java.lang.String id,
                                                    java.time.Duration duration,
                                                    JobRequest jobRequest)
        Creates a new or alters the existing recurring job based on the given id, duration 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 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"), new MyJobRequest());
         
        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
        jobRequest - the jobRequest which defines the recurring job
        Returns:
        the id of this recurring job which can be used to alter or delete it