public class DynamicTasks
extends java.lang.Object
TaskQueueingContext
to execute tasks.
Queueing is supported by some task contexts (eg DynamicSequentialTask
) to let that task
build up a complex sequence of tasks and logic. This utility class gives conveniences to allow:
Over time the last mode has been the most prevalent and get(TaskAdaptable)
is introduced here
as a convenience. If a timeout is desired then the first should be used.
Modifier and Type | Class and Description |
---|---|
static class |
DynamicTasks.TaskQueueingResult<T> |
Constructor and Description |
---|
DynamicTasks() |
Modifier and Type | Method and Description |
---|---|
static TaskQueueingContext |
drain(Duration optionalTimeout,
boolean throwFirstError)
Calls
TaskQueueingContext.drain(Duration, boolean, boolean) on the current task context |
static <T> T |
get(TaskAdaptable<T> t)
submits/queues the given task if needed, and gets the result (unchecked)
|
static <T> T |
get(TaskAdaptable<T> task,
Entity e)
queues the task if possible and waits for the result, otherwise executes synchronously as per
ExecutionContext.get(TaskAdaptable) |
static TaskQueueingContext |
getTaskQueuingContext() |
static TaskQueueingContext |
getThreadTaskQueuingContext() |
static void |
markInessential()
same as
Tasks.markInessential()
(but included here for convenience as it is often used in conjunction with DynamicTasks ) |
static <T> Task<T> |
of(java.lang.String name,
java.util.concurrent.Callable<T> body)
As
of(String, Runnable) where the task returns a result. |
static Task<java.lang.Void> |
of(java.lang.String name,
java.lang.Runnable body)
Convenience for
TaskBuilder.of(String, Runnable) creating a DynamicSequentialTask instance. |
static <T> Task<T> |
queue(java.lang.String name,
java.util.concurrent.Callable<T> job) |
static <T> Task<T> |
queue(java.lang.String name,
java.lang.Runnable job) |
static void |
queue(TaskAdaptable<?> task1,
TaskAdaptable<?> task2,
TaskAdaptable<?>... tasks) |
static void |
queue(TaskFactory<?> task1,
TaskFactory<?> task2,
TaskFactory<?>... tasks) |
static <T extends TaskAdaptable<?>> |
queue(TaskFactory<T> taskFactory) |
static <V extends TaskAdaptable<?>> |
queue(V task)
Queues the given task.
|
static <T extends TaskAdaptable<?>> |
queueIfNeeded(T task)
queues the task if needed, i.e.
|
static <T> DynamicTasks.TaskQueueingResult<T> |
queueIfPossible(TaskAdaptable<T> task)
Tries to add the task to the current addition context if there is one, otherwise does nothing.
|
static <T> DynamicTasks.TaskQueueingResult<T> |
queueIfPossible(TaskFactory<? extends TaskAdaptable<T>> task) |
static <T> Task<T> |
queueInTaskHierarchy(Task<T> task)
adds the given task to the nearest task addition context,
either set as a thread-local, or in the current task, or the submitter of the task, etc
|
static void |
removeTaskQueueingContext() |
static void |
setTaskQueueingContext(TaskQueueingContext newTaskQC) |
static <T> Task<T> |
submit(TaskAdaptable<T> task,
Entity entity)
queues the task if possible, otherwise submits it asynchronously; returns the task for callers to
Task.getUnchecked() or Task.blockUntilEnded() |
static <T> Task<T> |
submitTopLevelTask(TaskAdaptable<T> task,
Entity entity)
Breaks the parent-child relation between Tasks.current() and the task passed,
making the new task a top-level one at the target entity.
|
static void |
swallowChildrenFailures()
as
Tasks.swallowChildrenFailures() but requiring a TaskQueueingContext . |
static Task<?> |
waitForLast()
As
drain(Duration, boolean) waiting forever and throwing the first error
(excluding errors in inessential tasks),
then returning the last task in the queue (which is guaranteed to have finished without error,
if this method returns without throwing) |
public static void setTaskQueueingContext(TaskQueueingContext newTaskQC)
public static TaskQueueingContext getThreadTaskQueuingContext()
public static TaskQueueingContext getTaskQueuingContext()
public static void removeTaskQueueingContext()
public static Task<java.lang.Void> of(java.lang.String name, java.lang.Runnable body)
TaskBuilder.of(String, Runnable)
creating a DynamicSequentialTask
instance.
See also Tasks.of(String, Runnable)
.public static <T> Task<T> of(java.lang.String name, java.util.concurrent.Callable<T> body)
of(String, Runnable)
where the task returns a result.public static <T> DynamicTasks.TaskQueueingResult<T> queueIfPossible(TaskAdaptable<T> task)
DynamicTasks.TaskQueueingResult.orSubmitAsync()
on the returned
TaskQueueingResult
to handle execution of tasks in a
BasicExecutionContext
.public static <T> DynamicTasks.TaskQueueingResult<T> queueIfPossible(TaskFactory<? extends TaskAdaptable<T>> task)
queueIfPossible(TaskAdaptable)
public static <T> Task<T> queueInTaskHierarchy(Task<T> task)
throws if it cannot add or addition/execution would fail including if calling thread is interrupted
public static <V extends TaskAdaptable<?>> V queue(V task)
queueIfPossible(TaskAdaptable)
and DynamicTasks.TaskQueueingResult.orSubmitAsync()
if the calling context is a basic task.task
- The task to queuejava.lang.IllegalStateException
- if no task queueing context is availablepublic static void queue(TaskAdaptable<?> task1, TaskAdaptable<?> task2, TaskAdaptable<?>... tasks)
public static <T extends TaskAdaptable<?>> T queue(TaskFactory<T> taskFactory)
public static void queue(TaskFactory<?> task1, TaskFactory<?> task2, TaskFactory<?>... tasks)
public static <T> Task<T> queue(java.lang.String name, java.util.concurrent.Callable<T> job)
public static <T> Task<T> queue(java.lang.String name, java.lang.Runnable job)
public static <T extends TaskAdaptable<?>> T queueIfNeeded(T task)
public static <T> T get(TaskAdaptable<T> t)
public static Task<?> waitForLast()
drain(Duration, boolean)
waiting forever and throwing the first error
(excluding errors in inessential tasks),
then returning the last task in the queue (which is guaranteed to have finished without error,
if this method returns without throwing)public static TaskQueueingContext drain(Duration optionalTimeout, boolean throwFirstError)
TaskQueueingContext.drain(Duration, boolean, boolean)
on the current task contextpublic static void swallowChildrenFailures()
Tasks.swallowChildrenFailures()
but requiring a TaskQueueingContext
.public static void markInessential()
Tasks.markInessential()
(but included here for convenience as it is often used in conjunction with DynamicTasks
)public static <T> Task<T> submit(TaskAdaptable<T> task, Entity entity)
Task.getUnchecked()
or Task.blockUntilEnded()
public static <T> T get(TaskAdaptable<T> task, Entity e)
ExecutionContext.get(TaskAdaptable)
public static <T> Task<T> submitTopLevelTask(TaskAdaptable<T> task, Entity entity)