Interface RemoteExecutor
-
public interface RemoteExecutorA RemoteExecutor allows submitting and/or schedulingrunnablesandcallablesfor execution within a Coherence cluster.Using a RemoteExecutor
A RemoteExecutor may be obtained by a known name:RemoteExecutor.get(“executorName”Once a reference to aRemoteExecutorhas been obtained, similar to anExecutorService, 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
RemoteExecutorallows 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-concurrentmodule'sNamespaceHandler: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 ofcwas chosen and will be used for the examples below.The configuration supports multiple executor types and their related configuration. See the 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 aThreadFactory.<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
Fields Modifier and Type Field Description static StringDEFAULT_EXECUTOR_NAMEThe name of thedefaultexecutor; a single-threaded executor on each member running thecoherence-concurrentmodule.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description 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 RemoteExecutorget(String sName)Return theRemoteExecutorfor the given name.static RemoteExecutorgetDefault()Return the default executor.<T> List<Future<T>>invokeAll(Collection<? extends Remote.Callable<T>> tasks)Executes the given tasks, returning a list of Futures holding their status and results when all complete.<T> List<Future<T>>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> TinvokeAny(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> TinvokeAny(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.booleanisShutdown()Returnstrueif this executor has been shut down.booleanisTerminated()Returnstrueif 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.ScheduledFuture<?>schedule(Remote.Runnable command, long lcDelay, TimeUnit unit)Submits a one-shot task that becomes enabled after the given delay.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.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.voidshutdown()Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.List<Runnable>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.<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 Detail
-
DEFAULT_EXECUTOR_NAME
static final String DEFAULT_EXECUTOR_NAME
The name of thedefaultexecutor; a single-threaded executor on each member running thecoherence-concurrentmodule.- See Also:
- Constant Field Values
-
-
Method Detail
-
schedule
ScheduledFuture<?> schedule(Remote.Runnable command, long lcDelay, TimeUnit unit)
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
<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.- 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>> tasks) 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:
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 arenullRejectedExecutionException- 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()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:
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 arenullRejectedExecutionException- 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 isnullIllegalArgumentException- 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 isnullTimeoutException- if the given timeout elapses before any task successfully completesExecutionException- if no task successfully completesRejectedExecutionException- if tasks cannot be scheduled for execution
-
submit
<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. 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
<T> Future<T> submit(Remote.Runnable task, T result)
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
Future<?> submit(Remote.Runnable task)
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
-
execute
void execute(Remote.Runnable command)
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
-
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
boolean awaitTermination(long lcTimeout, TimeUnit unit) throws InterruptedExceptionBlocks 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
List<Runnable> 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
static RemoteExecutor get(String sName)
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
static RemoteExecutor 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
-
-