public class Engine extends Object implements Orchestrator
| Modifier and Type | Method and Description |
|---|---|
<R> CompletableFuture<R> |
cond(CompletableFuture<Boolean> condition,
CompletableFuture<R> thenFuture,
boolean thenActivate,
CompletableFuture<R> elseFuture,
boolean elseActivate)
Variant of
Orchestrator.cond(CompletableFuture, CompletableFuture, CompletableFuture) with additional boolean
arguments indicating whether to proactively start executing thenFuture and/or elseFuture when
condition is activated. |
CompletableFuture<Void> |
cond(CompletableFuture<Boolean> condition,
CompletableFuture<Void> thenFuture,
boolean thenActivate)
Variant of
Orchestrator.cond(CompletableFuture, CompletableFuture) with an additional boolean argument
indicating whether to proactively start executing thenFuture at the same time as condition. |
void |
execute(long timeoutMs,
CompletionStage<?>... futures)
Like
Orchestrator.execute(CompletionStage[]), but establishes a timeout which will affect execution
as defined by the TimeoutStrategy in effect. |
void |
executeAndWait(long timeoutMs,
CompletableFuture<?>... futures)
Variant of
Orchestrator.executeAndWait(CompletableFuture[]), but establishes a timeout which will affect
execution as defined by the TimeoutStrategy in effect. |
CompletableFuture<Boolean> |
fate(CompletableFuture<?>... cfs)
Ties the 'fates' of the supplied CompletableFutures together, which means that as soon as there is a fault on
any one of them, back-pressure is applied to prevent any of the remaining tasks or their predecessors (as
determined recursively) from starting if they have not already been started, and forcing a
TaskNotStartedException on any CompletableFuture that was prevented
from starting in that manner. |
void |
firstInterceptWith(TaskRunner taskRunner)
Adds a TaskRunner that will be processed before any existing TaskRunner.
|
int |
getCountOfThreadsSpawned()
Returns the number of threads that have been spawned by this Orchestrator, which is a non-deterministic result
since the inherent timing variations across threads may result in different spawning decisions on different
runs of the same execution request.
|
ExecutorService |
getExecutorService() |
String |
getName()
Name as set from
Orchestrator.create(String) or Orchestrator.setName(String). |
int |
getNumberOfInterceptors()
Returns the current number of interceptors, as set by
CommonConfig.firstInterceptWith(TaskRunner),
CommonConfig.lastInterceptWith(TaskRunner), or for Orchestrators as set from current global settings. |
SpawnMode |
getSpawnMode()
Gets the most recently set mode.
|
TaskMeta |
getTaskMeta(CompletableFuture<?> cf)
Returns details for any future previously registered with
Orchestrator.execute(CompletionStage[]) (directly or
through an operation on a CompletableFuture return value). |
long |
getTimeoutMs()
Gets the current timeout.
|
TimeoutStrategy |
getTimeoutStrategy()
Gets the current timeout strategy, default is
TimeoutStrategy.PREVENT_NEW. |
void |
lastInterceptWith(TaskRunner taskRunner)
Adds a TaskRunner that will be processed after any existing TaskRunner.
|
void |
removeAllTaskRunners()
Removes all interceptors from the current list.
|
void |
removeInterceptor(TaskRunner taskRunner)
Removes the given TaskRunner from the current list, if it is there.
|
void |
restoreConfigurationDefaults(Object arg)
Restore default settings.
|
void |
restoreDefaultExecutorService() |
void |
setExecutorService(ExecutorService executorService)
Resets the service used by this framework for spawning threads.
|
void |
setName(String name)
Sets name that will become part of the name for any threads spawned by this Orchestrator.
|
void |
setSpawnMode(SpawnMode mode)
Changes the thread-spawning logic to be consistent with the specified mode.
|
void |
setTimeoutMs(long ms)
Sets the current timeout.
|
void |
setTimeoutStrategy(TimeoutStrategy strategy)
Sets the strategy to apply if a timeout greater than zero is in effect and is exceeded.
|
<BASE,SUB extends TaskInterface<BASE>> |
task(SUB t)
Creates a task wrapper around any user POJO with the requirement that that POJO implements an interface X
that in turn implements TaskInterface<X>.The result is a wrapper object that has the same signature
as its pojo argument, such that any CompletableFuture-returning task methods invoked on this wrapper are not
executed right away --they will be executed if/when any read operation on the returned CompletableFuture is
performed or by passing that CompletableFuture to
Orchestrator.execute(CompletionStage[]) or any of its variants. |
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitcond, cond, create, create, create, execute, execute, executeAndWait, executeAndWait, fn, fn, fn, fn, fn, fn, fn, fnTask, fnTask, fnTask, fnTask, fnTask, fnTask, fnTask, task, vfn, vfn, vfn, vfn, vfn, vfnTask, vfnTask, vfnTask, vfnTask, vfnTask, voidTasksetTimeoutpublic String getName()
OrchestratorOrchestrator.create(String) or Orchestrator.setName(String).getName in interface Orchestratorpublic void setName(String name)
OrchestratorsetName in interface Orchestratorname - to setpublic int getCountOfThreadsSpawned()
OrchestratorNote that a physical thread may have been returned to the thread pool and retrieved again, but these would count as separate logical threads as far as this return value is concerned.
getCountOfThreadsSpawned in interface Orchestratorpublic void restoreConfigurationDefaults(Object arg)
CommonConfigrestoreConfigurationDefaults in interface CommonConfigpublic SpawnMode getSpawnMode()
CommonConfigSpawnMode.WHEN_NEEDED. The Orchestrator
default is null, which results in using the global setting.getSpawnMode in interface CommonConfigpublic void setSpawnMode(SpawnMode mode)
CommonConfigsetSpawnMode in interface CommonConfigmode - to setpublic long getTimeoutMs()
CommonConfiggetTimeoutMs in interface CommonConfigpublic void setTimeoutMs(long ms)
CommonConfigCommonConfig.getTimeoutStrategy() will apply.setTimeoutMs in interface CommonConfigms - duration in milliseconds, zero means no timeout will be appliedpublic TimeoutStrategy getTimeoutStrategy()
CommonConfigTimeoutStrategy.PREVENT_NEW.getTimeoutStrategy in interface CommonConfigCommonConfig.setTimeoutStrategy(TimeoutStrategy)public void setTimeoutStrategy(TimeoutStrategy strategy)
CommonConfigsetTimeoutStrategy in interface CommonConfigstrategy - to setpublic ExecutorService getExecutorService()
getExecutorService in interface CommonConfigpublic void setExecutorService(ExecutorService executorService)
CommonConfigGlobalOrchestratorConfig.DEFAULT_FIXED_THREADPOOL_SIZE.setExecutorService in interface CommonConfigexecutorService - to set as new defaultpublic void restoreDefaultExecutorService()
restoreDefaultExecutorService in interface CommonConfigpublic void execute(long timeoutMs,
CompletionStage<?>... futures)
OrchestratorOrchestrator.execute(CompletionStage[]), but establishes a timeout which will affect execution
as defined by the TimeoutStrategy in effect.execute in interface OrchestratortimeoutMs - timout in millisecondsfutures - to executepublic void executeAndWait(long timeoutMs,
CompletableFuture<?>... futures)
OrchestratorOrchestrator.executeAndWait(CompletableFuture[]), but establishes a timeout which will affect
execution as defined by the TimeoutStrategy in effect.executeAndWait in interface OrchestratortimeoutMs - timout in millisecondsfutures - to executepublic TaskMeta getTaskMeta(CompletableFuture<?> cf)
OrchestratorOrchestrator.execute(CompletionStage[]) (directly or
through an operation on a CompletableFuture return value).getTaskMeta in interface Orchestratorcf - to mappublic <BASE,SUB extends TaskInterface<BASE>> BASE task(SUB t)
OrchestratorOrchestrator.execute(CompletionStage[]) or any of its variants.
Because of that lazy evaluation, tasks can be added with little performance penalty while only later choosing
which ones are actually needed.
The userTask argument can be freely wrapped any number of times by calling this method (as well as similar methods). In other words, there is a many-to-one relationship between these wrappers and the target user POJO, which may be of interest for stateful user POJO tasks or simply for avoiding the overhead of repeatedly creating user task instances. In the following example, 4 task wrappers around the same user POJO task instance are created:
MyTask myTask = new MyTask();
CompletableFuture f1 = $.task(myTask).doSomething();
CompletableFuture f2 = $.task(myTask).doSomething(); // or doSomethingElse()
MyTask wrapper = $.task(myTask);
CompletableFuture f3 = wrapper.doSomething();
CompletableFuture f4 = wrapper.doSomething(); // or doSomethingElse()
Note that a 'read operation' in this context refers to standard CompletableFuture access operations that
initiate execution. This includes simple operations such as CompletableFuture.get() as well as any
composition operations such as CompletableFuture.thenApply(Function) with the exception of the
'compose' variations such as CompletableFuture.thenCompose(Function) whose _fn_ argument can only be
started with Orchestrator.execute(CompletionStage[]).
If a task method returns anything other than a CompletableFuture, it is executed right away, with any predecessors executed in the same thread-spawning manner as occurs when CompletableFutures are activated.
task in interface OrchestratorBASE - the interface for the taskSUB - the implementing class typet - any userTask with an interface that extends TaskInterfacepublic CompletableFuture<Void> cond(CompletableFuture<Boolean> condition, CompletableFuture<Void> thenFuture, boolean thenActivate)
OrchestratorOrchestrator.cond(CompletableFuture, CompletableFuture) with an additional boolean argument
indicating whether to proactively start executing thenFuture at the same time as condition.
Executing thenFuture in that way may be wasteful since condition may eventually evaluate to false,
but the overall result will be faster when condition evaluates to true.
Note that this method will not be activated until an access operation is performed on the return value, even though it is a void result.
cond in interface Orchestratorcondition - to first evaluatethenFuture - to execute if condition evaluates to truethenActivate - iff true then start executing at same time as conditionpublic <R> CompletableFuture<R> cond(CompletableFuture<Boolean> condition, CompletableFuture<R> thenFuture, boolean thenActivate, CompletableFuture<R> elseFuture, boolean elseActivate)
OrchestratorOrchestrator.cond(CompletableFuture, CompletableFuture, CompletableFuture) with additional boolean
arguments indicating whether to proactively start executing thenFuture and/or elseFuture when
condition is activated. Executing either of those futures in that way may be wasteful since the eventual
condition result may choose the alternate, but the overall result will be faster when condition chooses
a proactively activated choice.cond in interface OrchestratorR - type of return resultcondition - to first evaluatethenFuture - chosen if condition evaluates to truethenActivate - iff true then start executing at same time as conditionelseFuture - chosen if condition evaluates to falseelseActivate - iff true then start executing at same time as conditionpublic CompletableFuture<Boolean> fate(CompletableFuture<?>... cfs)
OrchestratorTaskNotStartedException on any CompletableFuture that was prevented
from starting in that manner.
A return result of 'true' can be used to initiate compensating actions on any task that previously
completed. Any such action can know whether any previous CompletableFuture was completed or not by calling
CompletableFuture.isCompletedExceptionally().
Note that this method is only activated if the return value is activated.
fate in interface Orchestratorcfs - to tie togetherpublic void firstInterceptWith(TaskRunner taskRunner)
CommonConfigfirstInterceptWith in interface CommonConfigtaskRunner - to addpublic void lastInterceptWith(TaskRunner taskRunner)
CommonConfiglastInterceptWith in interface CommonConfigtaskRunner - to addpublic int getNumberOfInterceptors()
CommonConfigCommonConfig.firstInterceptWith(TaskRunner),
CommonConfig.lastInterceptWith(TaskRunner), or for Orchestrators as set from current global settings.getNumberOfInterceptors in interface CommonConfigpublic void removeInterceptor(TaskRunner taskRunner)
CommonConfigremoveInterceptor in interface CommonConfigtaskRunner - to removepublic void removeAllTaskRunners()
CommonConfigremoveAllTaskRunners in interface CommonConfigCopyright © 2021. All rights reserved.