Class OrderedProcessor<ResultType>

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    @ThreadSafe
    public class OrderedProcessor<ResultType>
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Processes items in order, subject to capacity constraints.
    • Constructor Summary

      Constructors 
      Constructor Description
      OrderedProcessor​(int capacity, @NonNull java.util.concurrent.Executor executor)
      Creates a new instance of the OrderedProcessor class.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      java.util.concurrent.CompletableFuture<ResultType> execute​(@NonNull java.util.function.Supplier<java.util.concurrent.CompletableFuture<ResultType>> toRun)
      Executes the given item.
      protected void executionComplete​(java.lang.Throwable exception)
      Callback that is invoked when an item has completed execution.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • OrderedProcessor

        public OrderedProcessor​(int capacity,
                                @NonNull
                                @NonNull java.util.concurrent.Executor executor)
        Creates a new instance of the OrderedProcessor class.
        Parameters:
        capacity - The maximum number of concurrent executions.
        executor - An Executor for async invocations.
    • Method Detail

      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
      • execute

        public java.util.concurrent.CompletableFuture<ResultType> execute​(@NonNull
                                                                          @NonNull java.util.function.Supplier<java.util.concurrent.CompletableFuture<ResultType>> toRun)
        Executes the given item. * If hasCapacity() is true, the item will be executed immediately and the returned future is the actual result from toRun. * If hasCapacity() is false, the item will be queued up and it will be processed when capacity allows, after all the items added before it have been executed (whether exceptionally or with a successful outcome). * This method guarantees ordered execution as long as its invocations are serial. That is, it only guarantees that the item has begun processing or an order assigned if the method returned successfully.
        Parameters:
        toRun - A Supplier that, when invoked, will return a CompletableFuture which will indicate the outcome of the operation to execute.
        Returns:
        A CompletableFuture that, when completed, will indicate that the item has been processed. This will contain the result of the processing function applied to this item.
      • executionComplete

        protected void executionComplete​(java.lang.Throwable exception)
        Callback that is invoked when an item has completed execution.
        Parameters:
        exception - (Optional) An exception from the execution. If set, it indicates the item has not been processed successfully.