Class BlockingManagerImpl

    • Constructor Detail

      • BlockingManagerImpl

        public BlockingManagerImpl()
    • Method Detail

      • start

        protected void start()
      • runBlocking

        public CompletionStage<Void> runBlocking​(Runnable runnable,
                                                 Object traceId)
        Description copied from interface: BlockingManager
        Replacement for CompletionStage.runAsync() that invokes the Runnable in a blocking thread if the current thread is non-blocking or in the current thread if the current thread is blocking. The returned stage, if not complete, resumes any chained stage on the non-blocking executor.

        Note that if the current thread is blocking, the task is invoked in the current thread, meaning the stage is always completed when returned, so any chained stage is also invoked on the current thread.

        Specified by:
        runBlocking in interface BlockingManager
        Parameters:
        runnable - blocking operation that runs some code.
        traceId - an identifier that can be used to tell in a trace when an operation moves between threads.
        Returns:
        a stage that is completed after the runnable is done or throws an exception.
      • subscribeBlockingConsumer

        public <E> CompletionStage<Void> subscribeBlockingConsumer​(org.reactivestreams.Publisher<E> publisher,
                                                                   Consumer<E> consumer,
                                                                   Object traceId)
        Description copied from interface: BlockingManager
        Subscribes to the provided publisher on the invoking thread. Published values are observed on a blocking thread one a time passed to the provided consumer. The returned stage if not complete will resume any chained stage on the non blocking executor.

        If no values are published the returned stage will be completed upon return of this method and require no thread context switches

        Note that if the current thread is blocking everything including subscription, publication and consumption of values will be done on the current thread.

        Specified by:
        subscribeBlockingConsumer in interface BlockingManager
        Type Parameters:
        E - the type of entries
        Parameters:
        publisher - publisher of values to consume
        consumer - consumer to handle the values
        traceId - an identifier that can be used to tell in a trace when an operation moves between threads
        Returns:
        a stage that is completed after all values are consumed
      • subscribeBlockingCollector

        public <T,​A,​R> CompletionStage<R> subscribeBlockingCollector​(org.reactivestreams.Publisher<T> publisher,
                                                                                 Collector<? super T,​A,​R> collector,
                                                                                 Object traceId)
        Description copied from interface: BlockingManager
        Subscribes to the provided publisher on the invoking thread. Published values are observed on a blocking thread one a time passed to the provided collector. The returned stage if not complete will resume any chained stage on the non blocking executor.

        If no values are published the returned stage will be completed upon return of this method and require no thread context switches

        Note that if the current thread is blocking everything including subscription, publication and collection of values will be done on the current thread.

        Specified by:
        subscribeBlockingCollector in interface BlockingManager
        Type Parameters:
        T - the type of entries
        A - accumulator type of the entries
        R - final value type
        Parameters:
        publisher - publisher of values to collect
        collector - collector of the values
        traceId - an identifier that can be used to tell in a trace when an operation moves between threads
        Returns:
        a stage that when complete contains the collected values as a single value
      • supplyBlocking

        public <V> CompletionStage<V> supplyBlocking​(Supplier<V> supplier,
                                                     Object traceId)
        Description copied from interface: BlockingManager
        Replacement for CompletionStage.supplyAsync() that invokes the Supplier in a blocking thread (if the current thread is non-blocking) or in the current thread (if the current thread is blocking). The returned stage, if not complete, resumes any chained stage on the non-blocking executor.

        Note that if the current thread is blocking, the task is invoked in the current thread meaning the stage is always completed when returned, so any chained stage is also invoked on the current thread.

        Specified by:
        supplyBlocking in interface BlockingManager
        Type Parameters:
        V - the supplied type.
        Parameters:
        supplier - blocking operation that returns a value.
        traceId - an identifier that can be used to tell in a trace when an operation moves between threads.
        Returns:
        a stage that, when complete, contains the value returned from the supplier or a throwable.
      • handleBlocking

        public <I,​O> CompletionStage<O> handleBlocking​(CompletionStage<? extends I> stage,
                                                             BiFunction<? super I,​Throwable,​? extends O> function,
                                                             Object traceId)
        Description copied from interface: BlockingManager
        Replacement for CompletionStage.handleAsync() that invokes the BiFunction in a blocking thread (if the current thread is non-blocking) or in the current thread (if the current thread is blocking). The returned stage, if not complete, resumes any chained stage on the non-blocking executor.

        Note that if the current thread is blocking, the task is invoked in the current thread meaning the stage is always completed when returned, so any chained stage is also invoked on the current thread.

        Specified by:
        handleBlocking in interface BlockingManager
        Type Parameters:
        I - input value type to the function.
        O - output value type after being transformed via function.
        Parameters:
        stage - stage, that may or may not be complete, to handle.
        function - the blocking function.
        traceId - an identifier that can be used to tell in a trace when an operation moves between threads.
        Returns:
        a stage that, when complete, contains the value returned from the function or a throwable.
      • thenApplyBlocking

        public <I,​O> CompletionStage<O> thenApplyBlocking​(CompletionStage<? extends I> stage,
                                                                Function<? super I,​? extends O> function,
                                                                Object traceId)
        Description copied from interface: BlockingManager
        Replacement for CompletionStage.thenApplyAsync() that invokes the Function in a blocking thread (if the current thread is non-blocking) or in the current thread (if the current thread is blocking). The returned stage, if not complete, resumes any chained stage on the non-blocking executor.

        Note that if the current thread is blocking, the task is invoked in the current thread meaning the stage is always completed when returned, so any chained stage is also invoked on the current thread.

        Specified by:
        thenApplyBlocking in interface BlockingManager
        Type Parameters:
        I - input value type to the function.
        O - output value type after being transformed via function.
        Parameters:
        stage - stage, that may or may not be complete, to apply.
        function - the blocking function.
        traceId - an identifier that can be used to tell in a trace when an operation moves between threads.
        Returns:
        a stage that, when complete, contains the value returned from the function or a throwable.
      • whenCompleteBlocking

        public <V> CompletionStage<V> whenCompleteBlocking​(CompletionStage<V> stage,
                                                           BiConsumer<? super V,​? super Throwable> biConsumer,
                                                           Object traceId)
        Description copied from interface: BlockingManager
        Replacement for CompletionStage.whenCompleteAsync() that invokes the BiConsumer in a blocking thread (if the current thread is non-blocking) or in the current thread (if the current thread is blocking). The returned stage, if not complete, resumes any chained stage on the non-blocking executor.

        Note that if the current thread is blocking, the task is invoked in the current thread meaning the stage is always completed when returned, so any chained stage is also invoked on the current thread.

        Specified by:
        whenCompleteBlocking in interface BlockingManager
        Type Parameters:
        V - stage value type.
        Parameters:
        stage - stage, that may or may not be complete, to apply.
        biConsumer - the blocking biConsumer.
        traceId - an identifier that can be used to tell in a trace when an operation moves between threads.
        Returns:
        a stage that is complete when the biConsumer is complete, but retains the results from the original stage.
      • continueOnNonBlockingThread

        public <V> CompletionStage<V> continueOnNonBlockingThread​(CompletionStage<V> delay,
                                                                  Object traceId)
        Description copied from interface: BlockingManager
        When the provided stage is complete, continue the completion chain of the returned CompletionStage on the supplied executor. If tracing is enabled, a trace message is printed using the object as an identifier to more easily track the transition between threads.

        This method is useful when an asynchronous computation completes and you do not want to run further processing on the thread that returned it. An example may be that some blocking operation is performed on a special blocking thread pool. However when the blocking operation completes we want to continue processing that result in a thread pool that is for computational tasks.

        If the supplied stage is already completed when invoking this command, it returns an already completed stage, which means any additional dependent stages are run in the invoking thread.

        Specified by:
        continueOnNonBlockingThread in interface BlockingManager
        Type Parameters:
        V - return value type of the supplied stage.
        Parameters:
        delay - the stage to delay the continuation until complete.
        traceId - the identifier to print when tracing is enabled.
        Returns:
        a CompletionStage that, when depended upon, runs any callback in the supplied executor.
      • blockingPublisher

        public <V> org.reactivestreams.Publisher<V> blockingPublisher​(org.reactivestreams.Publisher<V> publisher)
        Description copied from interface: BlockingManager
        Provided a publisher that is known to block when subscribed to, this ensures that the publisher is subscribed on the blocking executor and any values published are observed on a non blocking thread. Note that if a blocking thread subscribes to the publisher these additional threads are not used and the entire Publisher is subscribed and observed on the invoking thread.
        Specified by:
        blockingPublisher in interface BlockingManager
        Type Parameters:
        V - the published entry types.
        Parameters:
        publisher - the publisher that, when subscribed to, blocks the current thread.
        Returns:
        publisher that does not block the current thread.