Interface RemoteExecutor
runnables, callables,
and tasks for execution within a Coherence cluster.
Using a RemoteExecutor
A RemoteExecutor may be obtained by a known name:RemoteExecutor.get(“executorName”
Once a reference to a RemoteExecutor has been obtained,
similar to an ExecutorService, tasks may be submitted:
// obtain the named RemoteExecutor (defined in xml configuration; see below)
RemoteExecutor executor = RemoteExecutor.get("MyExecutor");
// submit a simple runnable to the cluster but only to the executors
// named "MyExecutor"
Future future = executor.submit(() -> System.out.println("EXECUTED));
// block until completed
future.get();
A RemoteExecutor allows scheduling of tasks independent of the
underlying thread pool (more about that below); See:
schedule(Remote.Runnable, long, TimeUnit)schedule(Remote.Callable, long, TimeUnit)scheduleAtFixedRate(Remote.Runnable, long, long, TimeUnit)scheduleWithFixedDelay(Remote.Runnable, long, long, TimeUnit)
In order to use an executor, it must first be configured within
the application's cache configuration. To begin configuring
executors, developers must include a reference
to the coherence-concurrent module's NamespaceHandler:
Configuring RemoteExecutors
The configuration supports multiple executor types and their related configuration. In order to support executor definitions within the cache-configuration resource, the document namespaces should be updated to add a reference to the executor namespace handler:
<cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
xmlns:c="class://com.oracle.coherence.concurrent.config.NamespaceHandler"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd
class://com.oracle.coherence.concurrent.config.NamespaceHandler concurrent.xsd">
...
<cache-config>
In this case, the arbitrary namespace of c was chosen and will be used
for the examples below.
It should be noted, that it will be normal to have the same executor configured on multiple Coherence cluster members. When dispatching a task, it will be sent to one of the executors matching the configured name for processing. Thus, if a member fails, the tasks will fail over to the remaining named executors still present in the cluster.
The lifecycle of registered executors is tied to that of the owning Coherence cluster member, thus if a cluster member is brought down gracefully, the remaining tasks running on the executor local to that member will continue to completion.
Example configurations
<!-- creates a single-threaded executor named <em>Single</em> -->
<c:single>
<c:name>Single</c:nam>
</c:single>
<!-- creates a fixed thread pool executor named <em>Fixed5</em> with five threads -->
<c:fixed>
<c:name>Fixed5</c:name>
<c:thread-count>5</c:thread-count>
</c:fixed>
<!-- creates a cached thread pool executor named <em>Cached</em> -->
<c:cached>
<c:name>Cached</c:name>
</c:cached>
<!-- creates a work-stealing thread pool executor named <em>Stealing</em> with a parallelism of five-->
<c:work-stealing>
<c:name>Stealing</c:name>
<c:parallelism>5</c:parallelism>
</c:work-stealing>
An example defining a ThreadFactory:
<!-- creates a fixed thread pool executor named <em>Fixed5</em> with five threads and a custom thread factory -->
<c:fixed>
<c:name>Fixed5</c:name>
<c:thread-count>5</c:thread-count>
<c:thread-factory>
<instance>
<class-name>my.custom.ThreadFactory</class-name>
</instance>
</c:thread-factory>
</c:fixed>
If not ThreadFactory is defined, a default factory will be used.
The threads will be named CES:[executor-name]-[incrementing-counter].
For example, if the executor is named Fixed5, the threads name
would be CES:Fixed5-1, CES:Fixed5-2, etc.
Task Orchestration
In addition to theExecutorService-like
functionality offered by this class, it also provides the ability to
orchestrate tasks concurrently or sequentially
across multiple Coherence cluster members and collect the produced results
(if any).
There are several concepts that should be understood when using orchestrations:
- The
Taskinterface; similar in concept to a Callable - The
Task.Orchestrationinterface; controls how and where aTaskwill be run - The
Task.Coordinatorinterface; handles the publishing or collected results and notifying any subscribers - The
Task.Subscriberinterface; a receiver ofTaskresults - The
Task.Propertiesinterface; properties available to any task (of the same orchestration) no matter where it is executing. Useful for storing intermediate task execution state in case of cluster fail-over - The
Task.Collectorinterface; defines logic for collection and yielding task results
Orchestration Examples
This simplest example is orchestrating aTask across all members
where the named executor is defined:
RemoteExecutor executor = RemoteExecutor.getDefault();
// WaitingSubscriber is an implementation of the
// com.oracle.coherence.concurrent.executor.Task.Subscriber interface
// that has a get() method that blocks until Subscriber.onComplete() is
// called and will return the results received by onNext()
WaitingSubscriber subscriber = new WaitingSubscriber();
// ValueTask is an implementation of the
// com.oracle.coherence.concurrent.executor.Task interface
Task.Coordinator<String> coordinator = executor.submit(new ValueTask("Hello World"));
coordinator.subscribe(subscriber);
// wait for the task to complete
// if this was run on four cluster members, the returned
// Collection will have four results
Collection<String> results = subscriber.get();
If running the tasks on all similarly named executors is not desirable,
it is possible to limit where the tasks are run in a couple of ways.
First is by setting a limit on
the orchestration:
// The task will be executed by a single executor on one of the owning
// cluster members
Task.Orchestration<String> orchestration =
executor.orchestrate(new ValueTask("Hello World"))
.limit(1)
.subscribe(subscriber)
.submit();
or by filtering
which executor(s) will run on:
// The task will be executed on all cluster members matching the role
// of 'storage'
Task.Orchestration<String> orchestration =
executor.orchestrate(new ValueTask("Hello World"))
.filter(Predicates.role("storage"))
.subscribe(subscriber)
.submit();
There are several predicates available for use, however,
in the case none apply to the target use case, simply implement the
Remote.Predicate interface. Both limits and filters can be applied
simultaneously.
Collection of results and how they are presented to the subscriber
can be customized by using collect and until:
// orchestrate the task, collecting the first non-null result,
// subscribe, and submit
Task.Orchestration<String> orchestration =
executor.orchestrate(new MayReturnNullTask())
.collect(TaskCollectors.firstOf())
.until(Predicates.nonNullValue())
.subscribe(subscriber)
.submit();
// wait for the task to complete
// the first non-result returned will be the one provided to the
// subscriber
Collection<String> results = subscriber.get();
Several collectors are provided, however, in the
case none apply to the target use case, implement the
Task.Collector interface.
- Since:
- 21.12
- Author:
- rlubke 11.15.2021
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe name of thedefaultexecutor; a single-threaded executor on each member running thecoherence-concurrentmodule. -
Method Summary
Modifier and TypeMethodDescription<T> Task.Coordinator<T> Attempts to acquire theTask.Coordinatorfor a previously submittedTask.booleanawaitTermination(long lcTimeout, TimeUnit unit) Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.voidexecute(Remote.Runnable command) Executes the given command at some time in the future.static RemoteExecutorReturn theRemoteExecutorfor the given name.static RemoteExecutorReturn the default executor.invokeAll(Collection<? extends Remote.Callable<T>> colTasks) Executes the given tasks, returning a list of Futures holding their status and results when all complete.invokeAll(Collection<? extends Remote.Callable<T>> colTasks, long lcTimeout, TimeUnit unit) Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first.<T> TinvokeAny(Collection<? extends Remote.Callable<T>> colTasks) Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do.<T> TinvokeAny(Collection<? extends Remote.Callable<T>> colTasks, long lcTimeout, TimeUnit unit) Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do before the given timeout elapses.booleanReturnstrueif this executor has been shut down.booleanReturnstrueif all tasks have completed following shut down.<T> Task.Orchestration<T> orchestrate(Task<T> task) Creates a pendingTask.Orchestrationfor aTask.<V> ScheduledFuture<V> schedule(Remote.Callable<V> callable, long lcDelay, TimeUnit unit) Submits a value-returning one-shot task that becomes enabled after the given delay.schedule(Remote.Runnable command, long lcDelay, TimeUnit unit) Submits a one-shot task that becomes enabled after the given delay.scheduleAtFixedRate(Remote.Runnable command, long lcInitialDelay, long lcPeriod, TimeUnit unit) Submits a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is, executions will commence afterinitialDelay, theninitialDelay + period, theninitialDelay + 2 * period, and so on.scheduleWithFixedDelay(Remote.Runnable command, long lcInitialDelay, long lcDelay, TimeUnit unit) Submits a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.voidshutdown()Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.default <T> Task.Coordinator<T> Submits theTaskfor execution by theRemoteExecutor.<T> Future<T> submit(Remote.Callable<T> task) Submits a value-returning task for execution and returns a Future representing the pending results of the task.Future<?> submit(Remote.Runnable task) Submits a Runnable task for execution and returns a Future representing that task.<T> Future<T> submit(Remote.Runnable task, T result) Submits a Runnable task for execution and returns a Future representing that task.
-
Field Details
-
DEFAULT_EXECUTOR_NAME
The name of thedefaultexecutor; a single-threaded executor on each member running thecoherence-concurrentmodule.- See Also:
-
-
Method Details
-
schedule
Submits a one-shot task that becomes enabled after the given delay.- Parameters:
command- the task to executelcDelay- the time from now to delay executionunit- the time unit of the delay parameter- Returns:
- a
ScheduledFuturerepresenting the pending completion of the task and whoseget()method will returnnullupon completion - Throws:
RejectedExecutionException- if the task cannot be scheduled for executionNullPointerException- ifcallableorunitisnull
-
schedule
Submits a value-returning one-shot task that becomes enabled after the given delay.- Type Parameters:
V- the type of the callable's result- Parameters:
callable- the function to executelcDelay- the time from now to delay executionunit- the time unit of the delay parameter- Returns:
- a ScheduledFuture that can be used to extract result or cancel
- Throws:
RejectedExecutionException- if the task cannot be scheduled for executionNullPointerException- ifcallableorunitisnull
-
scheduleAtFixedRate
ScheduledFuture<?> scheduleAtFixedRate(Remote.Runnable command, long lcInitialDelay, long lcPeriod, TimeUnit unit) Submits a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is, executions will commence afterinitialDelay, theninitialDelay + period, theninitialDelay + 2 * period, and so on.The sequence of task executions continues indefinitely until one of the following exceptional completions occur:
- The task is explicitly cancelled via the returned future.
- The executor terminates, also resulting in task cancellation.
- An execution of the task throws an exception. In this case
calling
geton the returned future will throwExecutionException, holding the exception as its cause.
isDone()on the returned future will returntrue.If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.
- Parameters:
command- the task to executelcInitialDelay- the time to delay first executionlcPeriod- the period between successive executionsunit- the time unit of the initialDelay and period parameters- Returns:
- a
ScheduledFuturerepresenting pending completion of the series of repeated tasks. The future'sget()method will never return normally, and will throw an exception upon task cancellation or abnormal termination of a task execution. - Throws:
RejectedExecutionException- if thetaskcannot be scheduled for executionNullPointerException- ifcallableorunitisnullIllegalArgumentException- iflcPeriodless than or equal to zero
-
scheduleWithFixedDelay
ScheduledFuture<?> scheduleWithFixedDelay(Remote.Runnable command, long lcInitialDelay, long lcDelay, TimeUnit unit) Submits a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.The sequence of task executions continues indefinitely until one of the following exceptional completions occur:
- The task is explicitly cancelled via the returned future.
- The executor terminates, also resulting in task cancellation.
- An execution of the task throws an exception. In this case
calling
geton the returned future will throwExecutionException, holding the exception as its cause.
isDone()on the returned future will returntrue.- Parameters:
command- the task to executelcInitialDelay- the time to delay first executionlcDelay- the delay between the termination of one execution and the commencement of the nextunit- the time unit of the initialDelay and delay parameters- Returns:
- a
ScheduledFuturerepresenting pending completion of the series of repeated tasks. The future'sget()method will never return normally, and will throw an exception upon task cancellation or abnormal termination of a task execution. - Throws:
RejectedExecutionException- if the task cannot be scheduled for executionNullPointerException- ifcallableorunitisnullIllegalArgumentException- iflcDelayless than or equal to zero
-
invokeAll
<T> List<Future<T>> invokeAll(Collection<? extends Remote.Callable<T>> colTasks) throws InterruptedException Executes the given tasks, returning a list of Futures holding their status and results when all complete.Future.isDone()istruefor each element of the returned list. Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.- Type Parameters:
T- the type of the values returned from the tasks- Parameters:
colTasks- the collection of tasks- Returns:
- a list of Futures representing the tasks, in the same sequential order as produced by the iterator for the given task list, each of which has completed
- Throws:
InterruptedException- if interrupted while waiting, in which case unfinished tasks are cancelledNullPointerException- if tasks or any of its elements arenullRejectedExecutionException- if any task cannot be scheduled for execution
-
invokeAll
<T> List<Future<T>> invokeAll(Collection<? extends Remote.Callable<T>> colTasks, long lcTimeout, TimeUnit unit) throws InterruptedException Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first.Future.isDone()istruefor each element of the returned list. Upon return, tasks that have not completed are cancelled. Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.- Type Parameters:
T- the type of the values returned from the tasks- Parameters:
colTasks- the collection of taskslcTimeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- a list of Futures representing the tasks, in the same sequential order as produced by the iterator for the given task list. If the operation did not time out, each task will have completed. If it did time out, some of these tasks will not have completed.
- Throws:
InterruptedException- if interrupted while waiting, in which case unfinished tasks are cancelledNullPointerException- if tasks, any of its elements, or unit arenullRejectedExecutionException- if any task cannot be scheduled for execution
-
invokeAny
<T> T invokeAny(Collection<? extends Remote.Callable<T>> colTasks) throws InterruptedException, ExecutionException Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do. Upon normal or exceptional return, tasks that have not completed are cancelled. The results of this method are undefined if the given collection is modified while this operation is in progress.- Type Parameters:
T- the type of the values returned from the tasks- Parameters:
colTasks- the collection of tasks- Returns:
- the result returned by one of the tasks
- Throws:
InterruptedException- if interrupted while waitingNullPointerException- if tasks or any element task subject to execution isnullExecutionException- if no task successfully completesRejectedExecutionException- if tasks cannot be scheduled for execution
-
invokeAny
<T> T invokeAny(Collection<? extends Remote.Callable<T>> colTasks, long lcTimeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do before the given timeout elapses. Upon normal or exceptional return, tasks that have not completed are cancelled. The results of this method are undefined if the given collection is modified while this operation is in progress.- Type Parameters:
T- the type of the values returned from the tasks- Parameters:
colTasks- the collection of taskslcTimeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- the result returned by one of the tasks
- Throws:
InterruptedException- if interrupted while waitingNullPointerException- if tasks, or unit, or any element task subject to execution isnullTimeoutException- if the given timeout elapses before any task successfully completesExecutionException- if no task successfully completesRejectedExecutionException- if tasks cannot be scheduled for execution
-
submit
Submits a value-returning task for execution and returns a Future representing the pending results of the task. The Future'sgetmethod will return the task's result upon successful completion.If you would like to immediately block waiting for a task, you can use constructions of the form
result = exec.submit(aCallable).get();Note: The
Executorsclass includes a set of methods that can convert some other common closure-like objects, for example,PrivilegedActiontoCallableform so they can be submitted.- Type Parameters:
T- the type of the task's result- Parameters:
task- the task to submit- Returns:
- a
Futurerepresenting pending completion of the task - Throws:
RejectedExecutionException- if the task cannot be scheduled for executionNullPointerException- if the task isnull
-
submit
Submits a Runnable task for execution and returns a Future representing that task. The Future'sgetmethod will return the given result upon successful completion.- Type Parameters:
T- the type of the result- Parameters:
task- the task to submitresult- the result to return- Returns:
- a
Futurerepresenting pending completion of the task - Throws:
RejectedExecutionException- if the task cannot be scheduled for executionNullPointerException- if the task isnull
-
submit
Submits a Runnable task for execution and returns a Future representing that task. The Future'sgetmethod will returnnullupon successful completion.- Parameters:
task- the task to submit- Returns:
- a
Futurerepresenting pending completion of the task - Throws:
RejectedExecutionException- if the task cannot be scheduled for executionNullPointerException- if the task isnull
-
submit
Submits theTaskfor execution by theRemoteExecutor. The submitted task will be invoked, in parallel, across all cluster members where this executor is registered.The default implementation is
orchestrate(task).submit()- Type Parameters:
T- the type result produced by theTask- Parameters:
task- theTask- Returns:
- a
Task.Coordinatorfor theTask - Throws:
RejectedExecutionException- if this task cannot be accepted for executionNullPointerException- if task isnull- Since:
- 14.1.2.0.0
- See Also:
-
execute
Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of theExecutorimplementation.- Parameters:
command- the runnable task- Throws:
RejectedExecutionException- if this task cannot be accepted for executionNullPointerException- if command isnull
-
orchestrate
Creates a pendingTask.Orchestrationfor aTask.- Type Parameters:
T- the type result produced by theTask- Parameters:
task- theTask- Returns:
- an
Task.Orchestration - Throws:
NullPointerException- if task isnull- Since:
- 14.1.2.0.0
-
acquire
Attempts to acquire theTask.Coordinatorfor a previously submittedTask.- Type Parameters:
T- the type result produced by theTask- Parameters:
taskId- the unique identity originally allocated to theTask(available by callingTask.Coordinator.getTaskId())- Returns:
- the
Task.Coordinatorfor the specifiedTaskornullif theTaskis unknown - Throws:
NullPointerException- ifsTaskIdisnull- Since:
- 14.1.2.0.0
-
isShutdown
boolean isShutdown()Returnstrueif this executor has been shut down.- Returns:
trueif this executor has been shut down
-
isTerminated
boolean isTerminated()Returnstrueif all tasks have completed following shut down. Note thatisTerminatedis nevertrueunless eithershutdownorshutdownNowwas called first.- Returns:
trueif all tasks have completed following shut down
-
awaitTermination
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.- Parameters:
lcTimeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
trueif this executor terminated andfalseif the timeout elapsed before termination- Throws:
InterruptedException- if interrupted while waiting
-
shutdown
void shutdown()Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.This method does not wait for previously submitted tasks to complete execution. Use
awaitTerminationto do that. -
shutdownNow
Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.This method does not wait for actively executing tasks to terminate. Use
awaitTerminationto do that.There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via
Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.- Returns:
- list of tasks that never commenced execution
-
get
Return theRemoteExecutorfor the given name. Will returnnullif noRemoteExecutoris available by the given name.- Parameters:
sName- theRemoteExecutorname- Returns:
- the
RemoteExecutorfor the given name. - Throws:
NullPointerException- ifsNameisnullIllegalArgumentException- ifsNameis zero-length
-
getDefault
Return the default executor. This is a single-threaded executor service that is registered at service start.- Returns:
- the default executor; a single-threaded executor service that is registered at service start
-