Package pl.morgwai.base.grpc.utils
Class ConcurrentRequestObserver<RequestT,ResponseT>
- java.lang.Object
-
- pl.morgwai.base.grpc.utils.ConcurrentRequestObserver<RequestT,ResponseT>
-
- All Implemented Interfaces:
StreamObserver<RequestT>
- Direct Known Subclasses:
OrderedConcurrentRequestObserver
public class ConcurrentRequestObserver<RequestT,ResponseT> extends Object implements StreamObserver<RequestT>
A requestStreamObserver
for bi-di streaming methods that dispatch work to multiple threads and don't care about the order of responses. Handles all the synchronization and manual flow control to maintain desired number of request messages processed concurrently and prevent excessive buffering.Typical usage:
public StreamObserver<RequestMessage> myBiDiMethod( StreamObserver<ResponseMessage> responseObserver) { return new ConcurrentRequestObserver<RequestMessage, ResponseMessage>( (ServerCallStreamObserver<ResponseMessage>) responseObserver, executor.getMaximumPoolSize(), (requestMessage, singleRequestMessageResponseObserver) -> { executor.execute(() -> { var responseMessage = process(requestMessage); singleRequestMessageResponseObserver.onNext(responseMessage); singleRequestMessageResponseObserver.onCompleted(); }); }, (error) -> log.info(error) ); }
Once response observers for all request messages are closed and the client closes his request stream,
responseObserver.onCompleted()
is called automatically.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
ConcurrentRequestObserver.SingleRequestMessageResponseObserver
Observer of responses to 1 particular request message.
-
Field Summary
Fields Modifier and Type Field Description protected Consumer<Throwable>
errorHandler
Called byonError(Throwable)
.protected Object
lock
protected BiConsumer<RequestT,CallStreamObserver<ResponseT>>
requestHandler
Called byonRequestMessage(Object, CallStreamObserver)
.
-
Constructor Summary
Constructors Modifier Constructor Description protected
ConcurrentRequestObserver(ServerCallStreamObserver<ResponseT> responseObserver, int numberOfConcurrentRequests)
Configures flow control.ConcurrentRequestObserver(ServerCallStreamObserver<ResponseT> responseObserver, int numberOfConcurrentRequests, BiConsumer<RequestT,CallStreamObserver<ResponseT>> requestHandler, Consumer<Throwable> errorHandler)
InitializesrequestHandler
anderrorHandler
and callsConcurrentRequestObserver(ServerCallStreamObserver, int)
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected ConcurrentRequestObserver.SingleRequestMessageResponseObserver
newSingleRequestMessageResponseObserver()
Handles construction of newConcurrentRequestObserver.SingleRequestMessageResponseObserver
s for subclasses to override.void
onCompleted()
void
onError(Throwable t)
Default implementation callserrorHandler
.void
onNext(RequestT request)
protected void
onRequestMessage(RequestT requestMessage, CallStreamObserver<ResponseT> singleRequestMessageResponseObserver)
Produces response messages to the givenrequestMessage
and submits them to thesingleRequestMessageResponseObserver
(associated with thisrequestMessage
).
-
-
-
Field Detail
-
requestHandler
protected BiConsumer<RequestT,CallStreamObserver<ResponseT>> requestHandler
Called byonRequestMessage(Object, CallStreamObserver)
. Supplied via the param ofConcurrentRequestObserver(ServerCallStreamObserver, int, BiConsumer, Consumer)
constructor.
-
errorHandler
protected Consumer<Throwable> errorHandler
Called byonError(Throwable)
. Supplied via the param ofConcurrentRequestObserver(ServerCallStreamObserver, int, BiConsumer, Consumer)
constructor.
-
lock
protected Object lock
-
-
Constructor Detail
-
ConcurrentRequestObserver
public ConcurrentRequestObserver(ServerCallStreamObserver<ResponseT> responseObserver, int numberOfConcurrentRequests, BiConsumer<RequestT,CallStreamObserver<ResponseT>> requestHandler, Consumer<Throwable> errorHandler)
InitializesrequestHandler
anderrorHandler
and callsConcurrentRequestObserver(ServerCallStreamObserver, int)
.- Parameters:
responseObserver
- response observer of the given gRPC method.requestHandler
- stored onrequestHandler
to be called byonRequestMessage(Object, CallStreamObserver)
.errorHandler
- stored onerrorHandler
to be called byonError(Throwable)
.
-
ConcurrentRequestObserver
protected ConcurrentRequestObserver(ServerCallStreamObserver<ResponseT> responseObserver, int numberOfConcurrentRequests)
Configures flow control. Constructor for those who prefer to overrideonRequestMessage(Object, CallStreamObserver)
andonError(Throwable)
in a subclass instead of providing lambdas.
-
-
Method Detail
-
onRequestMessage
protected void onRequestMessage(RequestT requestMessage, CallStreamObserver<ResponseT> singleRequestMessageResponseObserver)
Produces response messages to the givenrequestMessage
and submits them to thesingleRequestMessageResponseObserver
(associated with thisrequestMessage
). usingConcurrentRequestObserver.SingleRequestMessageResponseObserver.onNext(Object)
method. Once all response messages to the givenrequestMessage
are submitted, callssingleRequestMessageResponseObserver.onComplete()
.singleRequestMessageResponseObserver
is thread-safe and implementations of this method may freely dispatch work to several other threads.To avoid excessive buffering, implementations should respect
singleRequestMessageResponseObserver
's readiness withsingleRequestMessageResponseObserver.isReady()
andsingleRequestMessageResponseObserver.setOnReadyHandler(...)
methods.
Consider usingDispatchingOnReadyHandler
orStreamObservers.copyWithFlowControl(Iterable, CallStreamObserver)
.Default implementation calls
requestHandler
.
-
onError
public void onError(Throwable t)
Default implementation callserrorHandler
.- Specified by:
onError
in interfaceStreamObserver<RequestT>
-
onCompleted
public final void onCompleted()
- Specified by:
onCompleted
in interfaceStreamObserver<RequestT>
-
onNext
public final void onNext(RequestT request)
- Specified by:
onNext
in interfaceStreamObserver<RequestT>
-
newSingleRequestMessageResponseObserver
protected ConcurrentRequestObserver.SingleRequestMessageResponseObserver newSingleRequestMessageResponseObserver()
Handles construction of newConcurrentRequestObserver.SingleRequestMessageResponseObserver
s for subclasses to override. Called byonNext(Object)
.
-
-