Package cn.boboweike.carrot.scheduling
Class BackgroundTaskRequest
- java.lang.Object
-
- cn.boboweike.carrot.scheduling.BackgroundTaskRequest
-
public class BackgroundTaskRequest 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 theTaskRequestSchedulerwhich exposes the same methods.- Author:
- Ronald Dehuysser
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voiddelete(TaskId taskId)static voiddelete(String id)Deletes the recurring task based on the given id.static voiddelete(UUID id)Deletes a task and sets its state to DELETED.static TaskIdenqueue(TaskRequest taskRequest)Creates a new fire-and-forget task based on a given taskRequest.static voidenqueue(Stream<? extends TaskRequest> input)Creates new fire-and-forget tasks for each item in the input stream.static TaskIdenqueue(UUID id, TaskRequest taskRequest)Creates a new fire-and-forget task based on a given taskRequest.static 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.static 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.static 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.static 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.static 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.static 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.static 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.static 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.static StringscheduleRecurrently(String cron, TaskRequest taskRequest)Creates a new recurring task based on the given cron expression and the given taskRequest.static 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.static 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.static StringscheduleRecurrently(String id, Duration duration, TaskRequest taskRequest)Creates a new or alters the existing recurring task based on the given id, duration and taskRequest.static StringscheduleRecurrently(Duration duration, TaskRequest taskRequest)Creates a new recurring task based on the given duration and the given taskRequest.static voidsetTaskRequestScheduler(TaskRequestScheduler taskRequestScheduler)
-
-
-
Method Detail
-
enqueue
public static 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:
BackgroundTaskRequest.enqueue(new MyTaskRequest());- Parameters:
taskRequest- the taskRequest which defines the fire-and-forget task.- Returns:
- the id of the task
-
enqueue
public static 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:
BackgroundTaskRequest.enqueueTaskRequest(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 static 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(); BackgroundTaskRequest.enqueue(workStream);- Parameters:
input- the stream of taskRequests for which to create fire-and-forget tasks
-
schedule
public static 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:
BackgroundTaskRequest.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 static 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:
BackgroundTaskRequest.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 static 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:
BackgroundTaskRequest.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 static 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:
BackgroundTaskRequest.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 static 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:
BackgroundTaskRequest.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 static 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:
BackgroundTaskRequest.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 static 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:
BackgroundTaskRequest.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 static 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:
BackgroundTaskRequest.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
-
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, 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:
BackgroundTaskRequest.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 static 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:
BackgroundTaskRequest.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 static 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:
BackgroundTaskRequest.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 static String scheduleRecurrently(Duration duration, TaskRequest taskRequest)
Creates a new recurring task based on the given duration and the given taskRequest. 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 tasktaskRequest- 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 static 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. 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 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
-
delete
public static void delete(String id)
Deletes the recurring task based on the given id.An example:
BackgroundTaskRequest.delete("my-recurring-task"));- Parameters:
id- the id of the recurring task to delete
-
setTaskRequestScheduler
public static void setTaskRequestScheduler(TaskRequestScheduler taskRequestScheduler)
-
-