Interface RemoteExecutor
runnables
and callables
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:
<pre>
// 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();
</pre>
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
<pre>
<cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
xmlns:executor="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>
</pre>
In this case, the arbitrary namespace of c
was chosen and will be used
for the examples below.
The configuration supports multiple executor types and their related configuration. See the invalid input: '<'a href=https://github.com/oracle/coherence/blob/master/prj/coherence-concurrent/src/main/resources/concurrent.xsd">schema for configuration specifics.
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
<pre>
<!-- creates a single-threaded executor named <em>Single</em> -->
<c:single>
<c:name>Single</c:name>
</c:single>
</pre>
<pre>
<!-- 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>
</pre>
<pre>
<!-- creates a cached thread pool executor named <em>Cached</em> -->
<c:cached>
<c:name>Cached</c:name>
</c:cached>
</pre>
<pre>
<!-- 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>
</pre>
An example defining a ThreadFactory
.
<pre>
<!-- 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>
</pre>
- Since:
- 21.12
- Author:
- rlubke 11.15.2021
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The name of thedefault
executor; a single-threaded executor on each member running thecoherence-concurrent
module. -
Method Summary
Modifier and TypeMethodDescriptionboolean
awaitTermination
(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.void
execute
(Remote.Runnable command) Executes the given command at some time in the future.static RemoteExecutor
Return theRemoteExecutor
for the given name.static RemoteExecutor
Return the default executor.invokeAll
(Collection<? extends Remote.Callable<T>> tasks) Executes the given tasks, returning a list of Futures holding their status and results when all complete.invokeAll
(Collection<? extends Remote.Callable<T>> tasks, 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> T
invokeAny
(Collection<? extends Remote.Callable<T>> tasks) Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do.<T> T
invokeAny
(Collection<? extends Remote.Callable<T>> tasks, 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.boolean
Returnstrue
if this executor has been shut down.boolean
Returnstrue
if all tasks have completed following shut down.<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.void
shutdown()
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.<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 thedefault
executor; a single-threaded executor on each member running thecoherence-concurrent
module.- 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
ScheduledFuture
representing the pending completion of the task and whoseget()
method will returnnull
upon completion - Throws:
RejectedExecutionException
- if the task cannot be scheduled for executionNullPointerException
- ifcallable
orunit
isnull
-
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
- ifcallable
orunit
isnull
-
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
get
on 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
ScheduledFuture
representing 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 thetask
cannot be scheduled for executionNullPointerException
- ifcallable
orunit
isnull
IllegalArgumentException
- iflcPeriod
less 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
get
on 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
ScheduledFuture
representing 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
- ifcallable
orunit
isnull
IllegalArgumentException
- iflcDelay
less than or equal to zero
-
invokeAll
<T> List<Future<T>> invokeAll(Collection<? extends Remote.Callable<T>> tasks) throws InterruptedException Executes the given tasks, returning a list of Futures holding their status and results when all complete.Future.isDone()
istrue
for 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:
tasks
- 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 arenull
RejectedExecutionException
- if any task cannot be scheduled for execution
-
invokeAll
<T> List<Future<T>> invokeAll(Collection<? extends Remote.Callable<T>> tasks, 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()
istrue
for 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:
tasks
- 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 arenull
RejectedExecutionException
- if any task cannot be scheduled for execution
-
invokeAny
<T> T invokeAny(Collection<? extends Remote.Callable<T>> tasks) 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:
tasks
- 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 isnull
IllegalArgumentException
- if tasks is emptyExecutionException
- if no task successfully completesRejectedExecutionException
- if tasks cannot be scheduled for execution
-
invokeAny
<T> T invokeAny(Collection<? extends Remote.Callable<T>> tasks, 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:
tasks
- 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 isnull
TimeoutException
- 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'sget
method 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
Executors
class includes a set of methods that can convert some other common closure-like objects, for example,PrivilegedAction
toCallable
form so they can be submitted.- Type Parameters:
T
- the type of the task's result- Parameters:
task
- the task to submit- Returns:
- a
Future
representing 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'sget
method 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
Future
representing 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'sget
method will returnnull
upon successful completion.- Parameters:
task
- the task to submit- Returns:
- a
Future
representing pending completion of the task - Throws:
RejectedExecutionException
- if the task cannot be scheduled for executionNullPointerException
- if the task isnull
-
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 theExecutor
implementation.- Parameters:
command
- the runnable task- Throws:
RejectedExecutionException
- if this task cannot be accepted for executionNullPointerException
- if command isnull
-
isShutdown
boolean isShutdown()Returnstrue
if this executor has been shut down.- Returns:
true
if this executor has been shut down
-
isTerminated
boolean isTerminated()Returnstrue
if all tasks have completed following shut down. Note thatisTerminated
is nevertrue
unless eithershutdown
orshutdownNow
was called first.- Returns:
true
if 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:
true
if this executor terminated andfalse
if 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
awaitTermination
to 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
awaitTermination
to 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 theRemoteExecutor
for the given name. Will returnnull
if noRemoteExecutor
is available by the given name.- Parameters:
sName
- theRemoteExecutor
name- Returns:
- the
RemoteExecutor
for the given name. - Throws:
NullPointerException
- ifsName
isnull
IllegalArgumentException
- ifsName
is 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
-