Class TaskScheduler


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

    This TaskScheduler allows to schedule tasks by means of a Java 8 lambda which is analyzed.

    Author:
    boboweike
    • Constructor Detail

      • TaskScheduler

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

        public TaskScheduler​(PartitionedStorageProvider storageProvider,
                             List<TaskFilter> taskFilters)
        Creates a new TaskScheduler 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​(TaskLambda task)
        Creates a new fire-and-forget task based on a given lambda.

        An example:

        
                    MyService service = new MyService();
                    taskScheduler.enqueue(() -> service.doWork());
               
        Parameters:
        task - the lambda which defines the fire-and-forget task
        Returns:
        the id of the task
      • enqueue

        public TaskId enqueue​(UUID id,
                              TaskLambda task)
        Creates a new fire-and-forget task based on the given lambda. If a task with that id already exists, Carrot will not save it again.

        An example:

        
                    MyService service = new MyService();
                    taskScheduler.enqueue(id, () -> service.doWork());
               
        Parameters:
        id - the uuid with which to save the task
        task - the lambda which defines the fire-and-forget task
        Returns:
        the id of the task
      • enqueue

        public <T> void enqueue​(Stream<T> input,
                                TaskLambdaFromStream<T> taskFromStream)
        Creates new fire-and-forget tasks for each item in the input stream using the lambda passed as taskFromStream.

        An example:

        
              MyService service = new MyService();
              Stream<UUID> workStream = getWorkStream();
              taskScheduler.enqueue(workStream, (uuid) -> service.doWork(uuid));
         
        Type Parameters:
        T - generic type for TaskLambdaFromStream
        Parameters:
        input - the stream of items for which to create fire-and-forget tasks
        taskFromStream - the lambda which defines the fire-and-forget task to create for each item in the input
      • enqueue

        public <S> TaskId enqueue​(IocTaskLambda<S> iocTask)
        Creates a new fire-and-forget task based on a given lambda. The IoC container will be used to resolve MyService.

        An example:

        
                    taskScheduler.<MyService>enqueue(x -> x.doWork());
               
        Type Parameters:
        S - generic type for IocTaskLambda
        Parameters:
        iocTask - the lambda which defines the fire-and-forget task
        Returns:
        the id of the task
      • enqueue

        public <S> TaskId enqueue​(UUID id,
                                  IocTaskLambda<S> iocTask)
        Creates a new fire-and-forget task based on a given lambda. The IoC container will be used to resolve MyService. If a task with that id already exists, Carrot will not save it again.

        An example:

        
                    taskScheduler.<MyService>enqueue(id, x -> x.doWork());
               
        Type Parameters:
        S - generic type for IocTaskLambda
        Parameters:
        id - the uuid with which to save the task
        iocTask - the lambda which defines the fire-and-forget task
        Returns:
        the id of the task
      • enqueue

        public <S,​T> void enqueue​(Stream<T> input,
                                        IocTaskLambdaFromStream<S,​T> iocTaskFromStream)
        Creates new fire-and-forget tasks for each item in the input stream using the lambda passed as taskFromStream. The IoC container will be used to resolve MyService.

        An example:

        
              Stream<UUID> workStream = getWorkStream();
              taskScheduler.<MyService, UUID>enqueue(workStream, (x, uuid) -> x.doWork(uuid));
         
        Type Parameters:
        T - generic type for IocTaskLambdaFromStream
        S - generic type for IocTaskLambdaFromStream
        Parameters:
        input - the stream of items for which to create fire-and-forget tasks
        iocTaskFromStream - the lambda which defines the fire-and-forget task to create for each item in the input
      • schedule

        public TaskId schedule​(ZonedDateTime zonedDateTime,
                               TaskLambda task)
        Creates a new fire-and-forget task based on the given lambda and schedules it to be enqueued at the given moment of time.

        An example:

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

        public TaskId schedule​(UUID id,
                               ZonedDateTime zonedDateTime,
                               TaskLambda task)
        Creates a new fire-and-forget task based on the given lambda and schedules it to be enqueued at the given moment of time. If a task with that id already exists, Carrot will not save it again.

        An example:

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

        public <S> TaskId schedule​(ZonedDateTime zonedDateTime,
                                   IocTaskLambda<S> iocTask)
        Creates a new fire-and-forget task 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:

        
              taskScheduler.<MyService>schedule(ZonedDateTime.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - generic type for IocTaskLambda
        Parameters:
        zonedDateTime - the moment in time at which the task will be enqueued.
        iocTask - the lambda which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public <S> TaskId schedule​(UUID id,
                                   ZonedDateTime zonedDateTime,
                                   IocTaskLambda<S> iocTask)
        Creates a new fire-and-forget task 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 task with that id already exists, Carrot will not save it again.

        An example:

        
              taskScheduler.<MyService>schedule(id, ZonedDateTime.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - generic type for IocTaskLambda
        Parameters:
        id - the uuid with which to save the task
        zonedDateTime - the moment in time at which the task will be enqueued.
        iocTask - the lambda which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public TaskId schedule​(OffsetDateTime offsetDateTime,
                               TaskLambda task)
        Creates a new fire-and-forget task based on the given lambda and schedules it to be enqueued at the given moment of time.

        An example:

        
              MyService service = new MyService();
              taskScheduler.schedule(OffsetDateTime.now().plusHours(5), () -> service.doWork());
         
        Parameters:
        offsetDateTime - The moment in time at which the task will be enqueued.
        task - the lambda which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public TaskId schedule​(UUID id,
                               OffsetDateTime offsetDateTime,
                               TaskLambda task)
        Creates a new fire-and-forget task based on the given lambda and schedules it to be enqueued at the given moment of time. If a task with that id already exists, Carrot will not save it again.

        An example:

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

        public <S> TaskId schedule​(OffsetDateTime offsetDateTime,
                                   IocTaskLambda<S> iocTask)
        Creates a new fire-and-forget task 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:

        
              taskScheduler.<MyService>schedule(OffsetDateTime.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - generic type for IocTaskLambda
        Parameters:
        offsetDateTime - The moment in time at which the task will be enqueued.
        iocTask - the lambda which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public <S> TaskId schedule​(UUID id,
                                   OffsetDateTime offsetDateTime,
                                   IocTaskLambda<S> iocTask)
        Creates a new fire-and-forget task 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 task with that id already exists, Carrot will not save it again.

        An example:

        
              taskScheduler.<MyService>schedule(id, OffsetDateTime.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - generic type for IocTaskLambda
        Parameters:
        id - the uuid with which to save the task
        offsetDateTime - The moment in time at which the task will be enqueued.
        iocTask - the lambda which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public TaskId schedule​(LocalDateTime localDateTime,
                               TaskLambda task)
        Creates a new fire-and-forget task based on the given lambda and schedules it to be enqueued at the given moment of time.

        An example:

        
              MyService service = new MyService();
              taskScheduler.schedule(LocalDateTime.now().plusHours(5), () -> service.doWork());
         
        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
        task - the lambda which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public TaskId schedule​(UUID id,
                               LocalDateTime localDateTime,
                               TaskLambda task)
        Creates a new fire-and-forget task based on the given lambda and schedules it to be enqueued at the given moment of time. If a task with that id already exists, Carrot will not save it again.

        An example:

        
              MyService service = new MyService();
              taskScheduler.schedule(id, LocalDateTime.now().plusHours(5), () -> service.doWork());
         
        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
        task - the lambda which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public <S> TaskId schedule​(LocalDateTime localDateTime,
                                   IocTaskLambda<S> iocTask)
        Creates a new fire-and-forget task 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:

        
              taskScheduler.<MyService>schedule(LocalDateTime.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - generic type for IocTaskLambda
        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
        iocTask - the lambda which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public <S> TaskId schedule​(UUID id,
                                   LocalDateTime localDateTime,
                                   IocTaskLambda<S> iocTask)
        Creates a new fire-and-forget task 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 task with that id already exists, Carrot will not save it again.

        An example:

        
              taskScheduler.<MyService>schedule(LocalDateTime.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - generic type for IocTaskLambda
        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
        iocTask - the lambda which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public TaskId schedule​(Instant instant,
                               TaskLambda task)
        Creates a new fire-and-forget task based on the given lambda and schedules it to be enqueued at the given moment of time.

        An example:

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

        public TaskId schedule​(UUID id,
                               Instant instant,
                               TaskLambda task)
        Creates a new fire-and-forget task based on the given lambda and schedules it to be enqueued at the given moment of time. If a task with that id already exists, Carrot will not save it again.

        An example:

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

        public <S> TaskId schedule​(Instant instant,
                                   IocTaskLambda<S> iocTask)
        Creates a new fire-and-forget task 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:

        
              taskScheduler.<MyService>schedule(Instant.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - generic type for IocTaskLambda
        Parameters:
        instant - the moment in time at which the task will be enqueued.
        iocTask - the lambda which defines the fire-and-forget task
        Returns:
        the id of the Task
      • schedule

        public <S> TaskId schedule​(UUID id,
                                   Instant instant,
                                   IocTaskLambda<S> iocTask)
        Creates a new fire-and-forget task 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 task with that id already exists, Carrot will not save it again.

        An example:

        
              taskScheduler.<MyService>schedule(id, Instant.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - generic type for IocTaskLambda
        Parameters:
        id - the uuid with which to save the task
        instant - the moment in time at which the task will be enqueued.
        iocTask - the lambda which defines the fire-and-forget task
        Returns:
        the id of the Task
      • scheduleRecurrently

        public String scheduleRecurrently​(String cron,
                                          TaskLambda task)
        Creates a new recurring task based on the given lambda and the given cron expression. The tasks will be scheduled using the systemDefault timezone.

        An example:

        
              MyService service = new MyService();
              taskScheduler.scheduleRecurrently(Cron.daily(), () -> service.doWork());
         
        Parameters:
        cron - The cron expression defining when to run this recurring task
        task - the lambda 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 <S> String scheduleRecurrently​(String cron,
                                              IocTaskLambda<S> iocTask)
        Creates a new recurring task based on the given cron expression and the given lambda. The IoC container will be used to resolve MyService. The tasks will be scheduled using the systemDefault timezone.

        An example:

        
              taskScheduler.<MyService>scheduleRecurrently(Cron.daily(), x -> x.doWork());
         
        Type Parameters:
        S - generic type for IocTaskLambda
        Parameters:
        cron - The cron expression defining when to run this recurring task
        iocTask - the lambda 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,
                                          TaskLambda task)
        Creates a new or alters the existing recurring task based on the given id, cron expression and lambda. The tasks will be scheduled using the systemDefault timezone

        An example:

        
              MyService service = new MyService();
              taskScheduler.scheduleRecurrently("my-recurring-task", Cron.daily(), () -> service.doWork());
         
        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
        task - the lambda 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 <S> String scheduleRecurrently​(String id,
                                              String cron,
                                              IocTaskLambda<S> iocTask)
        Creates a new or alters the existing recurring task based on the given id, cron expression and lambda. The IoC container will be used to resolve MyService. The tasks will be scheduled using the systemDefault timezone

        An example:

        
              taskScheduler.<MyService>scheduleRecurrently("my-recurring-task", Cron.daily()),  x -> x.doWork();
         
        Type Parameters:
        S - generic type for IocTaskLambda
        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
        iocTask - the lambda 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,
                                          TaskLambda task)
        Creates a new or alters the existing recurring task based on the given id, cron expression, ZoneId and lambda.

        An example:

        
              MyService service = new MyService();
              taskScheduler.scheduleRecurrently("my-recurring-task", Cron.daily(), ZoneId.of("Europe/Brussels"), () -> service.doWork());
         
        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
        task - the lambda 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 <S> String scheduleRecurrently​(String id,
                                              String cron,
                                              ZoneId zoneId,
                                              IocTaskLambda<S> iocTask)
        Creates a new or alters the existing recurring task based on the given id, cron expression, ZoneId and lambda. The IoC container will be used to resolve MyService.

        An example:

        
              taskScheduler.<MyService>scheduleRecurrently("my-recurring-task", Cron.daily(), ZoneId.of("Europe/Brussels"), x -> x.doWork());
         
        Type Parameters:
        S - generic type for IocTaskLambda
        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
        iocTask - the lambda 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,
                                          TaskLambda task)
        Creates a new recurring task based on the given duration and the given lambda. 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"), () -> service.doWork());
         
        Parameters:
        duration - the duration defining the time between each instance of this recurring task.
        task - the lambda which defines the recurring task
        Returns:
        the id of this recurring task which can be used to alter or delete it
      • scheduleRecurrently

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

        
              BackgroundTask.<MyService>scheduleRecurrently(Duration.parse("P5D"), x -> x.doWork());
         
        Type Parameters:
        S - generic type for IocTaskLambda
        Parameters:
        duration - the duration defining the time between each instance of this recurring task
        iocTask - the lambda 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,
                                          TaskLambda task)
        Creates a new or alters the existing recurring task based on the given id, duration and lambda. 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"), () -> service.doWork());
         
        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
        task - the lambda which defines the recurring task
        Returns:
        the id of this recurring task which can be used to alter or delete it
      • scheduleRecurrently

        public <S> String scheduleRecurrently​(String id,
                                              Duration duration,
                                              IocTaskLambda<S> iocTask)
        Creates a new or alters the existing recurring task based on the given id, duration and lambda. The IoC container will be used to resolve MyService. 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:

        
              BackgroundTask.<MyService>scheduleRecurrently("my-recurring-task", Duration.parse("P5D"), x -> x.doWork());
         
        Type Parameters:
        S - generic type for IocTaskLambda
        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
        iocTask - the lambda which defines the recurring task
        Returns:
        the id of this recurring task which can be used to alter or delete it