Class BackgroundTask


  • public class BackgroundTask
    extends Object
    Provides static methods for creating fire-and-forget, delayed and recurring tasks as well as to delete existing background tasks. If you prefer not to use a static accessor, you can inject the TaskScheduler which exposes the same methods.
    Author:
    Ronald Dehuysser
    • Method Detail

      • enqueue

        public static TaskId enqueue​(TaskLambda task)
        Creates a new fire-and-forget task based on a given lambda.

        An example:

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

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

        An example:

        
                    MyService service = new MyService();
                    BackgroundTask.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 static <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();
              BackgroundTask.enqueue(workStream, (uuid) -> service.doWork(uuid));
         
        Type Parameters:
        T - generic type for the Stream
        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 static <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:

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

        public static <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:

        
                    BackgroundTask.<MyService>enqueue(id, x -> x.doWork());
               
        Type Parameters:
        S - generic type for the 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 static <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();
              BackgroundTask.<MyService, UUID>enqueue(workStream, (x, uuid) -> x.doWork(uuid));
         
        Type Parameters:
        S - generic type for the IocTaskLambdaFromStream
        T - generic type for the 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 static 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();
              BackgroundTask.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 static 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();
              BackgroundTask.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 static <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:

        
              BackgroundTask.<MyService>schedule(ZonedDateTime.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - generic type for the 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 static <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:

        
              BackgroundTask.<MyService>schedule(id, ZonedDateTime.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - generic type for the 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 static 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();
              BackgroundTask.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 static 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();
              BackgroundTask.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 static <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:

        
              BackgroundTask.<MyService>schedule(id, OffsetDateTime.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - generic type for the 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 static <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:

        
              BackgroundTask.<MyService>schedule(id, OffsetDateTime.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - generic type for the 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 static 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();
              BackgroundTask.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 static 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();
              BackgroundTask.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 static <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:

        
              BackgroundTask.<MyService>schedule(LocalDateTime.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - generic type for the 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 static <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:

        
              BackgroundTask.<MyService>schedule(id, LocalDateTime.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - generic type for the 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 static 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();
              BackgroundTask.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 static 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();
              BackgroundTask.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 static <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:

        
              BackgroundTask.<MyService>schedule(Instant.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - Generic type for the 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 static <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:

        
              BackgroundTask.<MyService>schedule(id, Instant.now().plusHours(5), x -> x.doWork());
         
        Type Parameters:
        S - Generic type for the 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
      • delete

        public static void delete​(UUID id)
        Deletes a task and sets its state to DELETED. If the task is being processed, it will be interrupted.
        Parameters:
        id - the id of the task
      • delete

        public static void delete​(TaskId taskId)
        Parameters:
        taskId - the id of the task
        See Also:
        delete(UUID)
      • scheduleRecurrently

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

        An example:

        
              MyService service = new MyService();
              BackgroundTask.scheduleRecurrently(Cron.daily(), () -> service.doWork());
         
        Parameters:
        cron - The cron expression defining when to run this recurring task
        task - the lambda which defines the fire-and-forget task
        Returns:
        the id of this recurring task which can be used to alter or delete it
        See Also:
        Cron
      • scheduleRecurrently

        public static <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:

        
              BackgroundTask.<MyService>scheduleRecurrently(Cron.daily(), x -> x.doWork());
         
        Type Parameters:
        S - Generic type for the IocTaskLambda
        Parameters:
        cron - The cron expression defining when to run this recurring task
        iocTask - the lambda which defines the fire-and-forget task
        Returns:
        the id of this recurring task which can be used to alter or delete it
        See Also:
        Cron
      • scheduleRecurrently

        public static 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();
              BackgroundTask.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 fire-and-forget task
        Returns:
        the id of this recurring task which can be used to alter or delete it
        See Also:
        Cron
      • scheduleRecurrently

        public static <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:

        
              BackgroundTask.<MyService>scheduleRecurrently("my-recurring-task", Cron.daily(), x -> x.doWork());
         
        Type Parameters:
        S - Generic type for the 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 fire-and-forget task
        Returns:
        the id of this recurring task which can be used to alter or delete it
        See Also:
        Cron
      • scheduleRecurrently

        public static 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();
              BackgroundTask.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 fire-and-forget task
        Returns:
        the id of this recurring task which can be used to alter or delete it
        See Also:
        Cron
      • scheduleRecurrently

        public static <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:

        
              BackgroundTask.<MyService>scheduleRecurrently("my-recurring-task", Cron.daily(), ZoneId.of("Europe/Brussels"), x -> x.doWork());
         
        Type Parameters:
        S - Generic type for the 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 fire-and-forget task
        Returns:
        the id of this recurring task which can be used to alter or delete it
        See Also:
        Cron
      • scheduleRecurrently

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

        public static <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 the IocTaskLambda
        Parameters:
        duration - the duration defining the time between each instance of this recurring task
        iocTask - the lambda which defines the fire-and-forget task
        Returns:
        the id of this recurring task which can be used to alter or delete it
      • scheduleRecurrently

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

        public static <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 the 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 fire-and-forget task
        Returns:
        the id of this recurring task which can be used to alter or delete it
      • delete

        public static void delete​(String id)
        Deletes the recurring task based on the given id.

        An example:

        
              BackgroundTask.delete("my-recurring-task"));
         
        Parameters:
        id - the id of the recurring task to delete
      • setTaskScheduler

        public static void setTaskScheduler​(TaskScheduler taskScheduler)