Package pl.morgwai.base.grpc.utils
Class BlockingResponseObserver<T>
- java.lang.Object
-
- pl.morgwai.base.grpc.utils.BlockingResponseObserver<T>
-
- All Implemented Interfaces:
StreamObserver<T>
public class BlockingResponseObserver<T> extends Object implements StreamObserver<T>
Response observer for a client side that blocks until response is completed with eitheronCompleted()
oronError(Throwable)
.Typical usage:
var responseObserver = new BlockingResponseObserver<ResponseMessage>(response -> { // handle ResponseMessage response here... }); myGrpcServiceStub.myRemoteProcedure(someRequest, responseObserver); try { responseObserver.awaitCompletion(); // continue positive flow here... } catch (InterruptedException e) { // often unreachable code } catch (ErrorReportedException e) { Throwable reportedError = e.getCause(); // handle error that was reported via onError(reportedError) here... }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BlockingResponseObserver.ErrorReportedException
Thrown byawaitCompletion(long)
.
-
Field Summary
Fields Modifier and Type Field Description protected Consumer<T>
responseHandler
Called byonNext(Object)
.
-
Constructor Summary
Constructors Modifier Constructor Description protected
BlockingResponseObserver()
Constructor for those who prefer to overrideonNext(Object)
in a subclass instead of providing a lambda.BlockingResponseObserver(Consumer<T> responseHandler)
InitializesresponseHandler
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
awaitCompletion()
CallsawaitCompletion(0l)
.boolean
awaitCompletion(long timeoutMillis)
Throwable
getError()
IfonError(Throwable)
has been called, returns its argument, otherwisenull
.boolean
isCompleted()
void
onCompleted()
Notifies the thread that calledawaitCompletion(long)
.void
onError(Throwable error)
CausesawaitCompletion(long)
to throw aBlockingResponseObserver.ErrorReportedException
.void
onNext(T response)
CallsresponseHandler
.
-
-
-
Field Detail
-
responseHandler
protected Consumer<T> responseHandler
Called byonNext(Object)
.
-
-
Constructor Detail
-
BlockingResponseObserver
public BlockingResponseObserver(Consumer<T> responseHandler)
InitializesresponseHandler
.
-
BlockingResponseObserver
protected BlockingResponseObserver()
Constructor for those who prefer to overrideonNext(Object)
in a subclass instead of providing a lambda.
-
-
Method Detail
-
onNext
public void onNext(T response)
CallsresponseHandler
.- Specified by:
onNext
in interfaceStreamObserver<T>
-
isCompleted
public boolean isCompleted()
-
getError
public Throwable getError()
IfonError(Throwable)
has been called, returns its argument, otherwisenull
.
-
awaitCompletion
public boolean awaitCompletion(long timeoutMillis) throws BlockingResponseObserver.ErrorReportedException, InterruptedException
Awaits up totimeoutMillis
foronCompleted()
oronError(Throwable)
to be called. IftimeoutMillis
is0
then waits without a timeout.- Returns:
true
ifonCompleted()
was called,false
if timeout passed- Throws:
BlockingResponseObserver.ErrorReportedException
- ifonError(Throwable)
was called.getCause()
will return the throwable passed as argument.InterruptedException
-
awaitCompletion
public void awaitCompletion() throws BlockingResponseObserver.ErrorReportedException, InterruptedException
CallsawaitCompletion(0l)
.
-
onCompleted
public void onCompleted()
Notifies the thread that calledawaitCompletion(long)
.- Specified by:
onCompleted
in interfaceStreamObserver<T>
-
onError
public void onError(Throwable error)
CausesawaitCompletion(long)
to throw aBlockingResponseObserver.ErrorReportedException
.- Specified by:
onError
in interfaceStreamObserver<T>
-
-