Class ResponseHelper


  • public final class ResponseHelper
    extends Object
    A number of helper methods to handle sending responses to a StreamObserver.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> void complete​(io.grpc.stub.StreamObserver<T> observer, Runnable task, T result)
      Execute a Runnable task and on completion of the task complete the gRPC request by calling StreamObserver.onNext(Object) using the specified result and then call StreamObserver.onCompleted().
      static <T> void complete​(io.grpc.stub.StreamObserver<T> observer, Callable<T> callable)
      Complete a gRPC request based on the result of a Callable.
      static <T> void complete​(io.grpc.stub.StreamObserver<T> observer, CompletionStage<T> future)
      Complete a gRPC request based on the result of a CompletionStage.
      static <T> void complete​(io.grpc.stub.StreamObserver<T> observer, T value)
      Complete a gRPC request.
      static <T> void completeAsync​(io.grpc.stub.StreamObserver<T> observer, Runnable task, T result)
      Asynchronously execute a Runnable task and on completion of the task complete the gRPC request by calling StreamObserver.onNext(Object) using the specified result and then call StreamObserver.onCompleted().
      static <T> void completeAsync​(io.grpc.stub.StreamObserver<T> observer, Runnable task, T result, Executor executor)
      Asynchronously execute a Runnable task and on completion of the task complete the gRPC request by calling StreamObserver.onNext(Object) using the specified result and then call StreamObserver.onCompleted().
      static <T> void completeAsync​(io.grpc.stub.StreamObserver<T> observer, Callable<T> callable)
      Asynchronously complete a gRPC request based on the result of a Callable.
      static <T> void completeAsync​(io.grpc.stub.StreamObserver<T> observer, Callable<T> callable, Executor executor)
      Asynchronously complete a gRPC request based on the result of a Callable.
      static <T> void completeAsync​(io.grpc.stub.StreamObserver<T> observer, CompletionStage<T> future)
      Asynchronously complete a gRPC request based on the result of a CompletionStage.
      static <T> void completeAsync​(io.grpc.stub.StreamObserver<T> observer, CompletionStage<T> future, Executor executor)
      Asynchronously complete a gRPC request based on the result of a CompletionStage.
      static <U> BiConsumer<Void,​Throwable> completeWithoutResult​(io.grpc.stub.StreamObserver<U> observer)
      Obtain a Consumer that can be used to complete a StreamObserver.
      static <T,​U>
      BiConsumer<T,​Throwable>
      completeWithResult​(io.grpc.stub.StreamObserver<U> observer)
      Obtain a Consumer that can be used to send values to the StreamObserver.onNext(Object) method.
      static <T> Supplier<T> createSupplier​(Callable<T> callable)
      Convert a Callable to a Supplier.
      static <T> Consumer<T> stream​(io.grpc.stub.StreamObserver<T> observer, CompletionStage<Void> stage)
      Obtain a Consumer that can be used to send values to the StreamObserver.onNext(Object) method until the CompletionStage completes then call StreamObserver.onCompleted().
      static <T> void stream​(io.grpc.stub.StreamObserver<T> observer, Supplier<Stream<? extends T>> supplier)
      Send the values from a Stream to the StreamObserver.onNext(Object) method until the Stream is exhausted call StreamObserver.onCompleted().
      static <T> void stream​(io.grpc.stub.StreamObserver<T> observer, Stream<? extends T> stream)
      Send the values from a Stream to the StreamObserver.onNext(Object) method until the Stream is exhausted call StreamObserver.onCompleted().
      static <T> Consumer<T> streamAsync​(io.grpc.stub.StreamObserver<T> observer, CompletionStage<Void> stage)
      Obtain a Consumer that can be used to send values to the StreamObserver.onNext(Object) method until the CompletionStage completes then asynchronously call StreamObserver.onCompleted() using the fork-join thread pool.
      static <T> Consumer<T> streamAsync​(io.grpc.stub.StreamObserver<T> observer, CompletionStage<Void> stage, Executor executor)
      Obtain a Consumer that can be used to send values to the StreamObserver.onNext(Object) method until the CompletionStage completes then asynchronously call StreamObserver.onCompleted() using the executor thread.
      static <T> void streamAsync​(io.grpc.stub.StreamObserver<T> observer, Supplier<Stream<? extends T>> supplier, Executor executor)
      Asynchronously send the values from a Stream to the StreamObserver.onNext(Object) method until the Stream is exhausted call StreamObserver.onCompleted().
      static <T> void streamAsync​(io.grpc.stub.StreamObserver<T> observer, Stream<? extends T> stream, Executor executor)
      Asynchronously send the values from a Stream to the StreamObserver.onNext(Object) method until the Stream is exhausted call StreamObserver.onCompleted().
    • Method Detail

      • complete

        public static <T> void complete​(io.grpc.stub.StreamObserver<T> observer,
                                        T value)
        Complete a gRPC request.

        The request will be completed by calling StreamObserver.onNext(Object) using the specified value then calling StreamObserver.onCompleted().

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to complete
        value - the value to use when calling StreamObserver.onNext(Object)
      • complete

        public static <T> void complete​(io.grpc.stub.StreamObserver<T> observer,
                                        CompletionStage<T> future)
        Complete a gRPC request based on the result of a CompletionStage.

        The request will be completed by calling StreamObserver.onNext(Object) using the result obtained on completion of the specified CompletionStage and then calling StreamObserver.onCompleted().

        If the CompletionStage completes with an error then StreamObserver.onError(Throwable) will be called.

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to complete
        future - the CompletionStage to use to obtain the value to use to call StreamObserver.onNext(Object)
      • completeAsync

        public static <T> void completeAsync​(io.grpc.stub.StreamObserver<T> observer,
                                             CompletionStage<T> future)
        Asynchronously complete a gRPC request based on the result of a CompletionStage.

        The request will be completed by calling StreamObserver.onNext(Object) using the result obtained on completion of the specified CompletionStage and then calling StreamObserver.onCompleted().

        If the CompletionStage completes with an error then StreamObserver.onError(Throwable) will be called.

        The execution will take place asynchronously on the fork-join thread pool.

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to complete
        future - the CompletionStage to use to obtain the value to use to call StreamObserver.onNext(Object)
      • completeAsync

        public static <T> void completeAsync​(io.grpc.stub.StreamObserver<T> observer,
                                             CompletionStage<T> future,
                                             Executor executor)
        Asynchronously complete a gRPC request based on the result of a CompletionStage.

        The request will be completed by calling StreamObserver.onNext(Object) using the result obtained on completion of the specified CompletionStage and then calling StreamObserver.onCompleted().

        If the CompletionStage completes with an error then StreamObserver.onError(Throwable) will be called.

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to complete
        future - the CompletionStage to use to obtain the value to use to call StreamObserver.onNext(Object)
        executor - the Executor on which to execute the asynchronous request completion
      • complete

        public static <T> void complete​(io.grpc.stub.StreamObserver<T> observer,
                                        Callable<T> callable)
        Complete a gRPC request based on the result of a Callable.

        The request will be completed by calling StreamObserver.onNext(Object) using the result obtained on completion of the specified Callable and then calling StreamObserver.onCompleted().

        If the Callable.call() method throws an exception then StreamObserver.onError(Throwable) will be called.

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to complete
        callable - the Callable to use to obtain the value to use to call StreamObserver.onNext(Object)
      • completeAsync

        public static <T> void completeAsync​(io.grpc.stub.StreamObserver<T> observer,
                                             Callable<T> callable)
        Asynchronously complete a gRPC request based on the result of a Callable.

        The request will be completed by calling StreamObserver.onNext(Object) using the result obtained on completion of the specified Callable and then calling StreamObserver.onCompleted().

        If the Callable.call() method throws an exception then StreamObserver.onError(Throwable) will be called.

        The execution will take place asynchronously on the fork-join thread pool.

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to complete
        callable - the Callable to use to obtain the value to use to call StreamObserver.onNext(Object)
      • completeAsync

        public static <T> void completeAsync​(io.grpc.stub.StreamObserver<T> observer,
                                             Callable<T> callable,
                                             Executor executor)
        Asynchronously complete a gRPC request based on the result of a Callable.

        The request will be completed by calling StreamObserver.onNext(Object) using the result obtained on completion of the specified Callable and then calling StreamObserver.onCompleted().

        If the Callable.call() method throws an exception then StreamObserver.onError(Throwable) will be called.

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to complete
        callable - the Callable to use to obtain the value to use to call StreamObserver.onNext(Object)
        executor - the Executor on which to execute the asynchronous request completion
      • complete

        public static <T> void complete​(io.grpc.stub.StreamObserver<T> observer,
                                        Runnable task,
                                        T result)
        Execute a Runnable task and on completion of the task complete the gRPC request by calling StreamObserver.onNext(Object) using the specified result and then call StreamObserver.onCompleted().

        If the Runnable.run() method throws an exception then StreamObserver.onError(Throwable) will be called.

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to complete
        task - the Runnable to execute
        result - the result to pass to StreamObserver.onNext(Object)
      • completeAsync

        public static <T> void completeAsync​(io.grpc.stub.StreamObserver<T> observer,
                                             Runnable task,
                                             T result)
        Asynchronously execute a Runnable task and on completion of the task complete the gRPC request by calling StreamObserver.onNext(Object) using the specified result and then call StreamObserver.onCompleted().

        If the Runnable.run() method throws an exception then StreamObserver.onError(Throwable) will be called.

        The task and and request completion will be executed on the fork-join thread pool.

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to complete
        task - the Runnable to execute
        result - the result to pass to StreamObserver.onNext(Object)
      • completeAsync

        public static <T> void completeAsync​(io.grpc.stub.StreamObserver<T> observer,
                                             Runnable task,
                                             T result,
                                             Executor executor)
        Asynchronously execute a Runnable task and on completion of the task complete the gRPC request by calling StreamObserver.onNext(Object) using the specified result and then call StreamObserver.onCompleted().

        If the Runnable.run() method throws an exception then StreamObserver.onError(Throwable) will be called.

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to complete
        task - the Runnable to execute
        result - the result to pass to StreamObserver.onNext(Object)
        executor - the Executor on which to execute the asynchronous request completion
      • stream

        public static <T> void stream​(io.grpc.stub.StreamObserver<T> observer,
                                      Stream<? extends T> stream)
        Send the values from a Stream to the StreamObserver.onNext(Object) method until the Stream is exhausted call StreamObserver.onCompleted().

        If an error occurs whilst streaming results then StreamObserver.onError(Throwable) will be called.

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to complete
        stream - the Stream of results to send to StreamObserver.onNext(Object)
      • streamAsync

        public static <T> void streamAsync​(io.grpc.stub.StreamObserver<T> observer,
                                           Stream<? extends T> stream,
                                           Executor executor)
        Asynchronously send the values from a Stream to the StreamObserver.onNext(Object) method until the Stream is exhausted call StreamObserver.onCompleted().

        If an error occurs whilst streaming results then StreamObserver.onError(Throwable) will be called.

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to complete
        stream - the Stream of results to send to StreamObserver.onNext(Object)
        executor - the Executor on which to execute the asynchronous request completion
      • stream

        public static <T> void stream​(io.grpc.stub.StreamObserver<T> observer,
                                      Supplier<Stream<? extends T>> supplier)
        Send the values from a Stream to the StreamObserver.onNext(Object) method until the Stream is exhausted call StreamObserver.onCompleted().

        If an error occurs whilst streaming results then StreamObserver.onError(Throwable) will be called.

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to complete
        supplier - the Supplier of the Stream of results to send to StreamObserver.onNext(Object)
      • streamAsync

        public static <T> void streamAsync​(io.grpc.stub.StreamObserver<T> observer,
                                           Supplier<Stream<? extends T>> supplier,
                                           Executor executor)
        Asynchronously send the values from a Stream to the StreamObserver.onNext(Object) method until the Stream is exhausted call StreamObserver.onCompleted().

        If an error occurs whilst streaming results then StreamObserver.onError(Throwable) will be called.

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to complete
        supplier - the Supplier of the Stream of results to send to StreamObserver.onNext(Object)
        executor - the Executor on which to execute the asynchronous request completion
      • stream

        public static <T> Consumer<T> stream​(io.grpc.stub.StreamObserver<T> observer,
                                             CompletionStage<Void> stage)
        Obtain a Consumer that can be used to send values to the StreamObserver.onNext(Object) method until the CompletionStage completes then call StreamObserver.onCompleted().

        If the CompletionStage completes with an error then StreamObserver.onError(Throwable) will be called instead of StreamObserver.onCompleted().

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to send values to and complete when the CompletionStage completes
        stage - the CompletionStage to await completion of
        Returns:
        a Consumer that can be used to send values to the StreamObserver.onNext(Object) method
      • streamAsync

        public static <T> Consumer<T> streamAsync​(io.grpc.stub.StreamObserver<T> observer,
                                                  CompletionStage<Void> stage)
        Obtain a Consumer that can be used to send values to the StreamObserver.onNext(Object) method until the CompletionStage completes then asynchronously call StreamObserver.onCompleted() using the fork-join thread pool.

        If the CompletionStage completes with an error then StreamObserver.onError(Throwable) will be called instead of StreamObserver.onCompleted().

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to send values to and complete when the CompletionStage completes
        stage - the CompletionStage to await completion of
        Returns:
        a Consumer that can be used to send values to the StreamObserver.onNext(Object) method
      • streamAsync

        public static <T> Consumer<T> streamAsync​(io.grpc.stub.StreamObserver<T> observer,
                                                  CompletionStage<Void> stage,
                                                  Executor executor)
        Obtain a Consumer that can be used to send values to the StreamObserver.onNext(Object) method until the CompletionStage completes then asynchronously call StreamObserver.onCompleted() using the executor thread.

        If the CompletionStage completes with an error then StreamObserver.onError(Throwable) will be called instead of StreamObserver.onCompleted().

        Type Parameters:
        T - they type of the request result
        Parameters:
        observer - the StreamObserver to send values to and complete when the CompletionStage completes
        stage - the CompletionStage to await completion of
        executor - the Executor on which to execute the asynchronous request completion
        Returns:
        a Consumer that can be used to send values to the StreamObserver.onNext(Object) method
      • completeWithResult

        public static <T,​U> BiConsumer<T,​Throwable> completeWithResult​(io.grpc.stub.StreamObserver<U> observer)
        Obtain a Consumer that can be used to send values to the StreamObserver.onNext(Object) method.
        Type Parameters:
        T - the type of the result
        U - the type of the response
        Parameters:
        observer - the StreamObserver to complete
        Returns:
        a Consumer that can be used to send values to the StreamObserver.onNext(Object) method
      • completeWithoutResult

        public static <U> BiConsumer<Void,​Throwable> completeWithoutResult​(io.grpc.stub.StreamObserver<U> observer)
        Obtain a Consumer that can be used to complete a StreamObserver.
        Type Parameters:
        U - the type of the response
        Parameters:
        observer - the StreamObserver to complete
        Returns:
        a Consumer that can be used to complete a StreamObserver