Interface RemoteExecutor


  • public interface RemoteExecutor
    A RemoteExecutor allows submitting and/or scheduling 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:

    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> &lt;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"> ... &lt;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 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> &lt;!-- creates a single-threaded executor named <em>Single</em> --> &lt;c:single> &lt;c:name>Single&lt;/c:name> &lt;/c:single> </pre> <pre> &lt;!-- creates a fixed thread pool executor named <em>Fixed5</em> with five threads --> &lt;c:fixed> &lt;c:name>Fixed5&lt;/c:name> &lt;c:thread-count>5&lt;/c:thread-count> &lt;/c:fixed> </pre> <pre> &lt;!-- creates a cached thread pool executor named <em>Cached</em> --> &lt;c:cached> &lt;c:name>Cached&lt;/c:name> &lt;/c:cached> </pre> <pre> &lt;!-- creates a work-stealing thread pool executor named <em>Stealing</em> with a parallelism of five--> &lt;c:work-stealing> &lt;c:name>Stealing&lt;/c:name> &lt;c:parallelism>5&lt;/c:parallelism> &lt;/c:work-stealing> </pre> An example defining a ThreadFactory. <pre> &lt;!-- creates a fixed thread pool executor named <em>Fixed5</em> with five threads and a custom thread factory --> &lt;c:fixed> &lt;c:name>Fixed5&lt;/c:name> &lt;c:thread-count>5&lt;/c:thread-count> &lt;c:thread-factory> &lt;instance> &lt;class-name>my.custom.ThreadFactory&lt;/class-name> &lt;/instance> &lt;/c:thread-factory> &lt;/c:fixed> </pre>
    Since:
    21.12
    Author:
    rlubke 11.15.2021
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String DEFAULT_EXECUTOR_NAME
      The name of the default executor; a single-threaded executor on each member running the coherence-concurrent module.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      boolean 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 get​(String sName)
      Return the RemoteExecutor for the given name.
      static RemoteExecutor getDefault()
      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> 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 isShutdown()
      Returns true if this executor has been shut down.
      boolean isTerminated()
      Returns true 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.
      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 after initialDelay, then initialDelay + period, then initialDelay + 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.
      void shutdown()
      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 the default executor; a single-threaded executor on each member running the coherence-concurrent module.
        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 execute
        lcDelay - the time from now to delay execution
        unit - the time unit of the delay parameter
        Returns:
        a ScheduledFuture representing the pending completion of the task and whose get() method will return null upon completion
        Throws:
        RejectedExecutionException - if the task cannot be scheduled for execution
        NullPointerException - if callable or unit is null
      • 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 execute
        lcDelay - the time from now to delay execution
        unit - 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 execution
        NullPointerException - if callable or unit is null
      • 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 after initialDelay, then initialDelay + period, then initialDelay + 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 throw ExecutionException, holding the exception as its cause.
        Subsequent executions are suppressed. Subsequent calls to isDone() on the returned future will return true.

        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 execute
        lcInitialDelay - the time to delay first execution
        lcPeriod - the period between successive executions
        unit - the time unit of the initialDelay and period parameters
        Returns:
        a ScheduledFuture representing pending completion of the series of repeated tasks. The future's get() 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 execution
        NullPointerException - if callable or unit is null
        IllegalArgumentException - if lcPeriod 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 throw ExecutionException, holding the exception as its cause.
        Subsequent executions are suppressed. Subsequent calls to isDone() on the returned future will return true.
        Parameters:
        command - the task to execute
        lcInitialDelay - the time to delay first execution
        lcDelay - the delay between the termination of one execution and the commencement of the next
        unit - the time unit of the initialDelay and delay parameters
        Returns:
        a ScheduledFuture representing pending completion of the series of repeated tasks. The future's get() 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 execution
        NullPointerException - if callable or unit is null
        IllegalArgumentException - if lcDelay 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() is true 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 cancelled
        NullPointerException - if tasks or any of its elements are null
        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() is true 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 tasks
        lcTimeout - the maximum time to wait
        unit - 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 cancelled
        NullPointerException - if tasks, any of its elements, or unit are null
        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 waiting
        NullPointerException - if tasks or any element task subject to execution is null
        IllegalArgumentException - if tasks is empty
        ExecutionException - if no task successfully completes
        RejectedExecutionException - 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 tasks
        lcTimeout - the maximum time to wait
        unit - the time unit of the timeout argument
        Returns:
        the result returned by one of the tasks
        Throws:
        InterruptedException - if interrupted while waiting
        NullPointerException - if tasks, or unit, or any element task subject to execution is null
        TimeoutException - if the given timeout elapses before any task successfully completes
        ExecutionException - if no task successfully completes
        RejectedExecutionException - 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's get 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 to Callable 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 execution
        NullPointerException - if the task is null
      • 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's get method will return the given result upon successful completion.
        Type Parameters:
        T - the type of the result
        Parameters:
        task - the task to submit
        result - the result to return
        Returns:
        a Future representing pending completion of the task
        Throws:
        RejectedExecutionException - if the task cannot be scheduled for execution
        NullPointerException - if the task is null
      • submit

        Future<?> submit​(Remote.Runnable task)
        Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return null 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 execution
        NullPointerException - if the task is null
      • 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 the Executor implementation.
        Parameters:
        command - the runnable task
        Throws:
        RejectedExecutionException - if this task cannot be accepted for execution
        NullPointerException - if command is null
      • isShutdown

        boolean isShutdown()
        Returns true if this executor has been shut down.
        Returns:
        true if this executor has been shut down
      • isTerminated

        boolean isTerminated()
        Returns true if all tasks have completed following shut down. Note that isTerminated is never true unless either shutdown or shutdownNow was called first.
        Returns:
        true if all tasks have completed following shut down
      • awaitTermination

        boolean awaitTermination​(long lcTimeout,
                                 TimeUnit unit)
                          throws InterruptedException
        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 wait
        unit - the time unit of the timeout argument
        Returns:
        true if this executor terminated and false 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

        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 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
      • 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