@ParametersAreNonnullByDefault public class Tasks extends Object implements Lifecycle
Provides various helper methods to execute tasks in another thread and to provide interaction via instances of
Promise
.
Scheduling tasks via executor(String)
or defaultExecutor()
provides externally configured
thread-pools (via async.executor) as well as auto transfer of the current CallContext
to the
called thread.
Additionally helper-methods for creating and aggregating instances Promise
are provided, which are the
main interaction model when dealing with async and non-blocking execution.
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT
Contains the name of the default executor.
|
protected Map<String,AsyncExecutor> |
executors |
static int |
LIFECYCLE_PRIORITY
Contains the priority of this
Lifecycle |
protected static Log |
LOG |
DEFAULT_PRIORITY
Constructor and Description |
---|
Tasks() |
Modifier and Type | Method and Description |
---|---|
void |
awaitTermination()
Called after
Lifecycle.stopped() has been called to wait until all tasks are fully finished. |
ExecutionBuilder |
defaultExecutor()
Returns the default executor.
|
protected void |
execute(sirius.kernel.async.ExecutionBuilder.TaskWrapper wrapper) |
ExecutionBuilder |
executor(String category)
Returns the executor for the given category.
|
static <V> Promise<V> |
fail(Throwable ex)
Generates a promise which has immediately failed.
|
<V> Promise<V> |
fork(String category,
java.util.function.Supplier<V> computation)
Forks the given computation and returns a
Promise for the computed value. |
static Future |
future()
Creates a new future, which is just an untyped Promise where the completion is more important than
the actual result of the computation.
|
Collection<AsyncExecutor> |
getExecutors()
Returns a list of all known executors.
|
String |
getName()
Returns a short name for this lifecycle.
|
int |
getPriority()
Returns the priority of this element.
|
List<Tuple<String,java.time.LocalDateTime>> |
getScheduledTasks()
Returns a list containing the name and estimated execution time of all scheduled tasks which
are waiting for their execution.
|
boolean |
isRunning()
Determines if the application is still running.
|
static <V> Promise<V> |
promise()
Creates a new promise of the given type.
|
static <V> Promise<List<V>> |
sequence(List<Promise<V>> list)
Turns a list of promises into a promise for a list of values.
|
void |
started()
Invoked when the framework starts up.
|
void |
stopped()
Invoked when the framework shuts down.
|
static <V> Promise<V> |
success(V value)
Generates a promise which is immediately successful.
|
public static final String DEFAULT
public static final int LIFECYCLE_PRIORITY
Lifecycle
protected static final Log LOG
protected final Map<String,AsyncExecutor> executors
public ExecutionBuilder executor(String category)
The configuration for this executor is taken from async.executor.[category]. If no config is found, the default values are used.
category
- the category of the task to be executed, which implies the executor to use.public ExecutionBuilder defaultExecutor()
protected void execute(sirius.kernel.async.ExecutionBuilder.TaskWrapper wrapper)
public <V> Promise<V> fork(String category, java.util.function.Supplier<V> computation)
Promise
for the computed value.
Forks the computation, which means that the current CallContext is transferred to the new thread, and returns the result of the computation as promise.
If the executor for the given category is saturated (all threads are active and the queue is full) this will drop the computation and the promise will be sent a RejectedExecutionException.
V
- the type of the resulting valuecategory
- the category which implies which executor to use.computation
- the computation which eventually yields the resulting valuepublic static <V> Promise<V> promise()
V
- the type of the result which is promised.public static Future future()
public static <V> Promise<V> success(@Nullable V value)
V
- the type of the promise (or its promised value)value
- the value which is used as promised resultpublic static <V> Promise<V> fail(Throwable ex)
V
- the type of the promise (or its promised value)ex
- the error which is used as failurepublic static <V> Promise<List<V>> sequence(List<Promise<V>> list)
Note that all values need to have the same type.
If only the completion of all promises matters in contrast to their actual result, a Barrier
can also
be used. This permits to wait for promises of different types.
V
- the type of each promise.list
- the list of promises to convert.public Collection<AsyncExecutor> getExecutors()
public boolean isRunning()
Can be used for long loops in async tasks to determine if a computation should be interrupted.
public List<Tuple<String,java.time.LocalDateTime>> getScheduledTasks()
A scheduled task in this case is one which has
ExecutionBuilder.minInterval(Object, Duration)
or
ExecutionBuilder.frequency(Object, double)
set.
public void started()
Lifecycle
public void stopped()
Lifecycle
This method must not block (and wait for internals to stop). This can be done in Lifecycle.awaitTermination()
.
public void awaitTermination()
Lifecycle
Lifecycle.stopped()
has been called to wait until all tasks are fully finished.
This method may block for a certain amount of time to permit the subsystem to shut down properly. However, it should not block infinitely...
awaitTermination
in interface Lifecycle
public String getName()
Lifecycle
public int getPriority()
Priorized
getPriority
in interface Priorized
getPriority
in interface Lifecycle
Copyright © 2016. All rights reserved.