Class CallStreamObserver<V>
- java.lang.Object
-
- io.grpc.stub.CallStreamObserver<V>
-
- Type Parameters:
V
- type of outbound message.
- All Implemented Interfaces:
StreamObserver<V>
- Direct Known Subclasses:
ClientCallStreamObserver
,ServerCallStreamObserver
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/8499") public abstract class CallStreamObserver<V> extends Object implements StreamObserver<V>
A refinement of StreamObserver provided by the GRPC runtime to the application (the client or the server) that allows for more complex interactions with call behavior.In any call there are logically four
StreamObserver
implementations:- 'inbound', client-side - which the GRPC runtime calls when it receives messages from the server. This is implemented by the client application and passed into a service method on a stub object.
- 'outbound', client-side - which the GRPC runtime provides to the client application and the
client uses this
StreamObserver
to send messages to the server. - 'inbound', server-side - which the GRPC runtime calls when it receives messages from the client. This is implemented by the server application and returned from service implementations of client-side streaming and bidirectional streaming methods.
- 'outbound', server-side - which the GRPC runtime provides to the server application and
the server uses this
StreamObserver
to send messages (responses) to the client.
Implementations of this class represent the 'outbound' message streams. The client-side one is
ClientCallStreamObserver
and the service-side one isServerCallStreamObserver
.Like
StreamObserver
, implementations are not required to be thread-safe; if multiple threads will be writing to an instance concurrently, the application must synchronize its calls.DO NOT MOCK: The API is too complex to reliably mock. Use InProcessChannelBuilder to create "real" RPCs suitable for testing.
-
-
Constructor Summary
Constructors Constructor Description CallStreamObserver()
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description abstract void
disableAutoInboundFlowControl()
Disables automatic flow control where a token is returned to the peer after a call to the 'inbound'StreamObserver.onNext(Object)
has completed.abstract boolean
isReady()
Iftrue
, indicates that the observer is capable of sending additional messages without requiring excessive buffering internally.abstract void
request(int count)
Requests the peer to producecount
more messages to be delivered to the 'inbound'StreamObserver
.abstract void
setMessageCompression(boolean enable)
Sets message compression for subsequent calls toStreamObserver.onNext(V)
.abstract void
setOnReadyHandler(Runnable onReadyHandler)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.grpc.stub.StreamObserver
onCompleted, onError, onNext
-
-
-
-
Method Detail
-
isReady
public abstract boolean isReady()
Iftrue
, indicates that the observer is capable of sending additional messages without requiring excessive buffering internally. This value is just a suggestion and the application is free to ignore it, however doing so may result in excessive buffering within the observer.If
false
, the runnable passed tosetOnReadyHandler(java.lang.Runnable)
will be called afterisReady()
transitions totrue
.
-
setOnReadyHandler
public abstract void setOnReadyHandler(Runnable onReadyHandler)
Set aRunnable
that will be executed every time the streamisReady()
state changes fromfalse
totrue
. While it is not guaranteed that the same thread will always be used to execute theRunnable
, it is guaranteed that executions are serialized with calls to the 'inbound'StreamObserver
.On client-side this method may only be called during
ClientResponseObserver.beforeStart(io.grpc.stub.ClientCallStreamObserver<ReqT>)
. On server-side it may only be called during the initial call to the application, before the service returns itsStreamObserver
.Because there is a processing delay to deliver this notification, it is possible for concurrent writes to cause
isReady() == false
within this callback. Handle "spurious" notifications by checkingisReady()
's current value instead of assuming it is nowtrue
. IfisReady() == false
the normal expectations apply, so there would be anotheronReadyHandler
callback.- Parameters:
onReadyHandler
- to call when peer is ready to receive more messages.
-
disableAutoInboundFlowControl
public abstract void disableAutoInboundFlowControl()
Disables automatic flow control where a token is returned to the peer after a call to the 'inbound'StreamObserver.onNext(Object)
has completed. If disabled an application must make explicit calls torequest(int)
to receive messages.On client-side this method may only be called during
ClientResponseObserver.beforeStart(io.grpc.stub.ClientCallStreamObserver<ReqT>)
. On server-side it may only be called during the initial call to the application, before the service returns itsStreamObserver
.Note that for cases where the runtime knows that only one inbound message is allowed calling this method will have no effect and the runtime will always permit one and only one message. This is true for:
MethodDescriptor.MethodType.UNARY
operations on both the client and server.MethodDescriptor.MethodType.CLIENT_STREAMING
operations on the client.MethodDescriptor.MethodType.SERVER_STREAMING
operations on the server.
This API is being replaced, but is not yet deprecated. On server-side it being replaced with
ServerCallStreamObserver.disableAutoRequest()
. On client-sidedisableAutoRequestWithInitial(1)
.
-
request
public abstract void request(int count)
Requests the peer to producecount
more messages to be delivered to the 'inbound'StreamObserver
.This method is safe to call from multiple threads without external synchronization.
- Parameters:
count
- more messages
-
setMessageCompression
public abstract void setMessageCompression(boolean enable)
Sets message compression for subsequent calls toStreamObserver.onNext(V)
.- Parameters:
enable
- whether to enable compression.
-
-