Package cn.boboweike.carrot.scheduling
Class TaskRequestScheduler
- java.lang.Object
-
- cn.boboweike.carrot.scheduling.AbstractTaskScheduler
-
- cn.boboweike.carrot.scheduling.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
TaskRequestSchedulerallows to schedule tasks by means of an implementation of aTaskRequest.- Author:
- Ronald Dehuysser
-
-
Constructor Summary
Constructors Constructor Description TaskRequestScheduler(PartitionedStorageProvider storageProvider)Creates a new TaskRequestScheduler using the provided storageProviderTaskRequestScheduler(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
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TaskIdenqueue(TaskRequest taskRequest)Creates a new fire-and-forget task based on a given taskRequest.voidenqueue(Stream<? extends TaskRequest> input)Creates new fire-and-forget tasks for each item in the input stream.TaskIdenqueue(UUID id, TaskRequest taskRequest)Creates a new fire-and-forget task based on a given taskRequest.TaskIdschedule(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.TaskIdschedule(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.TaskIdschedule(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.TaskIdschedule(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.TaskIdschedule(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.TaskIdschedule(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.TaskIdschedule(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.TaskIdschedule(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.StringscheduleRecurrently(String cron, TaskRequest taskRequest)Creates a new recurring task based on the given cron expression and the given taskRequest.StringscheduleRecurrently(String id, String cron, TaskRequest taskRequest)Creates a new or alters the existing recurring task based on the given id, cron expression and taskRequest.StringscheduleRecurrently(String id, String cron, ZoneId zoneId, TaskRequest taskRequest)Creates a new or alters the existing recurring task based on the given id, cron expression,ZoneIdand taskRequest.StringscheduleRecurrently(String id, Duration duration, TaskRequest taskRequest)Creates a new or alters the existing recurring task based on the given id, duration and taskRequest.StringscheduleRecurrently(Duration duration, TaskRequest taskRequest)Creates a new recurring task based on the given duration and the given taskRequest.
-
-
-
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 usetaskFilters- 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 tasktaskRequest- 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 taskzonedDateTime- 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 taskoffsetDateTime- 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 InstanttaskRequest- 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 tasklocalDateTime- the moment in time at which the task will be enqueued. It will use the systemDefault ZoneId to transform it to an UTC InstanttaskRequest- 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 taskinstant- 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 tasktaskRequest- 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 timezoneAn 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 itcron- The cron expression defining when to run this recurring tasktaskRequest- 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,ZoneIdand 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 itcron- The cron expression defining when to run this recurring taskzoneId- The zoneId (timezone) of when to run this recurring tasktaskRequest- 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 itduration- the duration defining the time between each instance of this recurring tasktaskRequest- the taskRequest which defines the recurring task- Returns:
- the id of this recurring task which can be used to alter or delete it
-
-