Class TaskRequestScheduler


  • public class TaskRequestScheduler
    extends AbstractTaskScheduler
    Provides methods for creating fire-and-forget, delayed and recurring tasks as well as to delete existing background tasks.

    This TaskRequestScheduler allows to schedule tasks by means of an implementation of a TaskRequest.

    Author:
    Ronald Dehuysser
    • Constructor Detail

      • TaskRequestScheduler

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

        public TaskRequestScheduler​(PartitionedStorageProvider storageProvider,
                                    List<TaskFilter> taskFilters)
        Creates a new TaskRequestScheduler using the provided storageProvider and the list of TaskFilters that will be used for every background task
        Parameters:
        storageProvider - the storageProvider to use
        taskFilters - list of taskFilters that will be used for every task
    • Method Detail

      • enqueue

        public TaskId enqueue​(TaskRequest taskRequest)
        Creates a new fire-and-forget task based on a given taskRequest. Carrot will try to find the TaskRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor.

        An example:

        
                    taskScheduler.enqueue(new MyTaskRequest());
               
        Parameters:
        taskRequest - the taskRequest which defines the fire-and-forget task.
        Returns:
        the id of the task
      • enqueue

        public TaskId enqueue​(UUID id,
                              TaskRequest taskRequest)
        Creates a new fire-and-forget task based on a given taskRequest. Carrot will try to find the TaskRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor.

        An example:

        
                    taskScheduler.enqueue(id, new MyTaskRequest());
               
        Parameters:
        id - the uuid with which to save the task
        taskRequest - the taskRequest which defines the fire-and-forget task.
        Returns:
        the id of the task
      • enqueue

        public void enqueue​(Stream<? extends TaskRequest> input)
        Creates new fire-and-forget tasks for each item in the input stream. Carrot will try to find the TaskRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor.

        An example:

        
              Stream<MyTaskRequest> workStream = getWorkStream();
              taskScheduler.enqueue(workStream);
         
        Parameters:
        input - the stream of taskRequests for which to create fire-and-forget tasks
      • schedule

        public TaskId schedule​(ZonedDateTime zonedDateTime,
                               TaskRequest taskRequest)
        Creates a new fire-and-forget task based on the given taskRequest and schedules it to be enqueued at the given moment of time. Carrot will try to find the TaskRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor.

        An example:

        
              taskScheduler.schedule(ZonedDateTime.now().plusHours(5), new MyTaskRequest());
         
        Parameters:
        zonedDateTime - the moment in time at which the task will be enqueued.
        taskRequest - the taskRequest which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public TaskId schedule​(UUID id,
                               ZonedDateTime zonedDateTime,
                               TaskRequest taskRequest)
        Creates a new fire-and-forget task based on the given taskRequest and schedules it to be enqueued at the given moment of time. Carrot will try to find the TaskRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor. If a task with that id already exists, Carrot will not save it again.

        An example:

        
              taskScheduler.schedule(id, ZonedDateTime.now().plusHours(5), new MyTaskRequest());
         
        Parameters:
        id - the uuid with which to save the task
        zonedDateTime - the moment in time at which the task will be enqueued.
        taskRequest - the taskRequest which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public TaskId schedule​(OffsetDateTime offsetDateTime,
                               TaskRequest taskRequest)
        Creates a new fire-and-forget task based on the given taskRequest and schedules it to be enqueued at the given moment of time. Carrot will try to find the TaskRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor.

        An example:

        
              taskScheduler.schedule(OffsetDateTime.now().plusHours(5), new MyTaskRequest());
         
        Parameters:
        offsetDateTime - the moment in time at which the task will be enqueued.
        taskRequest - the taskRequest which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public TaskId schedule​(UUID id,
                               OffsetDateTime offsetDateTime,
                               TaskRequest taskRequest)
        Creates a new fire-and-forget task based on the given taskRequest and schedules it to be enqueued at the given moment of time. Carrot will try to find the TaskRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor. If a task with that id already exists, Carrot will not save it again.

        An example:

        
              taskScheduler.schedule(id, OffsetDateTime.now().plusHours(5), new MyTaskRequest());
         
        Parameters:
        id - the uuid with which to save the task
        offsetDateTime - the moment in time at which the task will be enqueued.
        taskRequest - the taskRequest which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public TaskId schedule​(LocalDateTime localDateTime,
                               TaskRequest taskRequest)
        Creates a new fire-and-forget task based on the given taskRequest and schedules it to be enqueued at the given moment of time. Carrot will try to find the TaskRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor.

        An example:

        
              taskScheduler.schedule(LocalDateTime.now().plusHours(5), new MyTaskRequest());
         
        Parameters:
        localDateTime - the moment in time at which the task will be enqueued. It will use the systemDefault ZoneId to transform it to an UTC Instant
        taskRequest - the taskRequest which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public TaskId schedule​(UUID id,
                               LocalDateTime localDateTime,
                               TaskRequest taskRequest)
        Creates a new fire-and-forget task based on the given taskRequest and schedules it to be enqueued at the given moment of time. Carrot will try to find the TaskRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor. If a task with that id already exists, Carrot will not save it again.

        An example:

        
              taskScheduler.schedule(id, LocalDateTime.now().plusHours(5), new MyTaskRequest());
         
        Parameters:
        id - the uuid with which to save the task
        localDateTime - the moment in time at which the task will be enqueued. It will use the systemDefault ZoneId to transform it to an UTC Instant
        taskRequest - the taskRequest which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public TaskId schedule​(Instant instant,
                               TaskRequest taskRequest)
        Creates a new fire-and-forget task based on the given taskRequest and schedules it to be enqueued at the given moment of time. Carrot will try to find the TaskRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor.

        An example:

        
              taskScheduler.schedule(Instant.now().plusHours(5), new MyTaskRequest());
         
        Parameters:
        instant - the moment in time at which the task will be enqueued.
        taskRequest - the lambda which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public TaskId schedule​(UUID id,
                               Instant instant,
                               TaskRequest taskRequest)
        Creates a new fire-and-forget task based on the given taskRequest and schedules it to be enqueued at the given moment of time. Carrot will try to find the TaskRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor. If a task with that id already exists, Carrot will not save it again.

        An example:

        
              taskScheduler.schedule(id, Instant.now().plusHours(5), new MyTaskRequest());
         
        Parameters:
        id - the uuid with which to save the task
        instant - the moment in time at which the task will be enqueued.
        taskRequest - the taskRequest which defines the fire-and-forget task
        Returns:
        the id of the Task
      • scheduleRecurrently

        public String scheduleRecurrently​(String cron,
                                          TaskRequest taskRequest)
        Creates a new recurring task based on the given cron expression and the given taskRequest. Carrot will try to find the TaskRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor. The tasks will be scheduled using the systemDefault timezone.

        An example:

        
              taskScheduler.scheduleRecurrently(Cron.daily(), new MyTaskRequest());
         
        Parameters:
        cron - The cron expression defining when to run this recurring task
        taskRequest - the taskRequest which defines the recurring task
        Returns:
        the id of this recurring task which can be used to alter or delete it
        See Also:
        Cron
      • scheduleRecurrently

        public String scheduleRecurrently​(String id,
                                          String cron,
                                          TaskRequest taskRequest)
        Creates a new or alters the existing recurring task based on the given id, cron expression and taskRequest. Carrot will try to find the TaskRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor. The tasks will be scheduled using the systemDefault timezone

        An example:

        
              taskScheduler.scheduleRecurrently("my-recurring-task", Cron.daily(), new MyTaskRequest());
         
        Parameters:
        id - the id of this recurring task which can be used to alter or delete it
        cron - The cron expression defining when to run this recurring task
        taskRequest - the taskRequest which defines the recurring task
        Returns:
        the id of this recurring task which can be used to alter or delete it
        See Also:
        Cron
      • scheduleRecurrently

        public String scheduleRecurrently​(String id,
                                          String cron,
                                          ZoneId zoneId,
                                          TaskRequest taskRequest)
        Creates a new or alters the existing recurring task based on the given id, cron expression, ZoneId and taskRequest. Carrot will try to find the TaskRequestHandler in the IoC container or else it will try to create the handler by calling the default no-arg constructor.

        An example:

        
              taskScheduler.scheduleRecurrently("my-recurring-task", Cron.daily(), ZoneId.of("Europe/Brussels"), new MyTaskRequest());
         
        Parameters:
        id - the id of this recurring task which can be used to alter or delete it
        cron - The cron expression defining when to run this recurring task
        zoneId - The zoneId (timezone) of when to run this recurring task
        taskRequest - the taskRequest which defines the recurring task
        Returns:
        the id of this recurring task which can be used to alter or delete it
        See Also:
        Cron
      • scheduleRecurrently

        public String scheduleRecurrently​(Duration duration,
                                          TaskRequest taskRequest)
        Creates a new recurring task based on the given duration and the given taskRequest. Carrot will try to find the TaskRequestHandler 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 task will happen after the given duration unless your duration is smaller or equal than your backgroundTaskServer pollInterval.

        An example:

        
              MyService service = new MyService();
              BackgroundTask.scheduleRecurrently(Duration.parse("P5D"), new MyTaskRequest());
         
        Parameters:
        duration - the duration defining the time between each instance of this recurring task.
        taskRequest - the taskRequest which defines the recurring task
        Returns:
        the id of this recurring task which can be used to alter or delete it
      • scheduleRecurrently

        public String scheduleRecurrently​(String id,
                                          Duration duration,
                                          TaskRequest taskRequest)
        Creates a new or alters the existing recurring task based on the given id, duration and taskRequest. Carrot will try to find the TaskRequestHandler 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 task will happen after the given duration unless your duration is smaller or equal than your backgroundTaskServer pollInterval.

        An example:

        
              MyService service = new MyService();
              BackgroundTask.scheduleRecurrently("my-recurring-task", Duration.parse("P5D"), new MyTaskRequest());
         
        Parameters:
        id - the id of this recurring task which can be used to alter or delete it
        duration - the duration defining the time between each instance of this recurring task
        taskRequest - the taskRequest which defines the recurring task
        Returns:
        the id of this recurring task which can be used to alter or delete it