@ParametersAreNonnullByDefault public class Tasks extends Object implements Startable, Stoppable, Killable
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
Stoppable.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.
|
ExecutorService |
executorService(String category)
Exposes the raw executor service for the given category.
|
<V> Promise<V> |
fork(String category,
Supplier<V> computation)
Forks the given computation and returns a
Promise for the computed value. |
Collection<AsyncExecutor> |
getExecutors()
Returns a list of all known executors.
|
int |
getPriority()
Returns the priority of this element.
|
List<Tuple<String,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<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.
|
public static final String DEFAULT
public static final int LIFECYCLE_PRIORITY
protected static final Log LOG
protected final Map<String,AsyncExecutor> executors
@Nonnull 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()
@Nonnull public ExecutorService executorService(String category)
This shouldn't be used for custom task scheduling (use executor(String)
instead) but rather for other
frameworks which need an executor service. Using this approach, all thread pools of an application are managed
and visible via central facility.
category
- the category which is used to specify the capacity of the executorprotected void execute(sirius.kernel.async.ExecutionBuilder.TaskWrapper wrapper)
public <V> Promise<V> fork(String category, 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<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,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()
Startable
public void stopped()
Stoppable
This method must not block (and wait for internals to stop). This can be done in
Killable.awaitTermination()
.
public void awaitTermination()
Killable
Stoppable.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 Killable
public int getPriority()
Priorized
getPriority
in interface Priorized
getPriority
in interface Killable
getPriority
in interface Startable
getPriority
in interface Stoppable
Copyright © 2018. All rights reserved.