Class ServerCallStreamObserver<RespT>
- java.lang.Object
-
- io.grpc.stub.CallStreamObserver<RespT>
-
- io.grpc.stub.ServerCallStreamObserver<RespT>
-
- All Implemented Interfaces:
StreamObserver<RespT>
public abstract class ServerCallStreamObserver<RespT> extends CallStreamObserver<RespT>
A refinement ofCallStreamObserver
to allows for interaction with call cancellation events on the server side. An instance of this class is obtained by casting theStreamObserver
passed as an argument to service implementations.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 and interact with the server using a normal client stub.
-
-
Constructor Summary
Constructors Constructor Description ServerCallStreamObserver()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
disableAutoRequest()
Swaps to manual flow control where no message will be delivered toStreamObserver.onNext(Object)
unless it isrequest()
ed.abstract boolean
isCancelled()
Returnstrue
when the call is cancelled and the server is encouraged to abort processing to save resources, since the client will not be processing any further methods.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
setCompression(String compression)
Sets the compression algorithm to use for the call.abstract void
setMessageCompression(boolean enable)
Sets message compression for subsequent calls toStreamObserver.onNext(V)
.abstract void
setOnCancelHandler(Runnable onCancelHandler)
Sets aRunnable
to be called if the call is cancelled and the server is encouraged to abort processing to save resources, since the client will not process any further messages.void
setOnCloseHandler(Runnable onCloseHandler)
Sets aRunnable
to be executed when the call is closed cleanly from the server's point of view: eitherStreamObserver.onCompleted()
orStreamObserver.onError(Throwable)
has been called, all the messages and trailing metadata have been sent and the stream has been closed.abstract void
setOnReadyHandler(Runnable onReadyHandler)
void
setOnReadyThreshold(int numBytes)
A hint to the call that specifies how many bytes must be queued beforeisReady()
will return false.-
Methods inherited from class io.grpc.stub.CallStreamObserver
disableAutoInboundFlowControl
-
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
-
isCancelled
public abstract boolean isCancelled()
Returnstrue
when the call is cancelled and the server is encouraged to abort processing to save resources, since the client will not be processing any further methods. Cancellations can be caused by timeouts, explicit cancellation by client, network errors, and similar.This method may safely be called concurrently from multiple threads.
-
setOnCancelHandler
public abstract void setOnCancelHandler(Runnable onCancelHandler)
Sets aRunnable
to be called if the call is cancelled and the server is encouraged to abort processing to save resources, since the client will not process any further messages. Cancellations can be caused by timeouts, explicit cancellation by the client, network errors, etc.It is guaranteed that execution of the
Runnable
is serialized with calls to the 'inbound'StreamObserver
. That also means that the callback will be delayed if other callbacks are running; if one of those other callbacks runs for a significant amount of time it can pollisCancelled()
, which is not delayed.This method may only be called during the initial call to the application, before the service returns its
StreamObserver
.Setting the onCancelHandler will suppress the on-cancel exception thrown by
StreamObserver.onNext(V)
. If the caller is already handling cancellation via polling or cannot substantially benefit from observing cancellation, using a no-oponCancelHandler
is useful just to suppress theonNext()
exception.- Parameters:
onCancelHandler
- to call when client has cancelled the call.
-
setOnReadyThreshold
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/11021") public void setOnReadyThreshold(int numBytes)
A hint to the call that specifies how many bytes must be queued beforeisReady()
will return false. A call may ignore this property if unsupported. This may only be set during stream initialization before any messages are set.- Parameters:
numBytes
- The number of bytes that must be queued. Must be a positive integer.
-
setCompression
public abstract void setCompression(String compression)
Sets the compression algorithm to use for the call. May only be called before sending any messages. Default gRPC servers support the "gzip" compressor.It is safe to call this even if the client does not support the compression format chosen. The implementation will handle negotiation with the client and may fall back to no compression.
- Parameters:
compression
- the compression algorithm to use.- Throws:
IllegalArgumentException
- if the compressor name can not be found.
-
disableAutoRequest
public void disableAutoRequest()
Swaps to manual flow control where no message will be delivered toStreamObserver.onNext(Object)
unless it isrequest()
ed.It may only be called during the initial call to the application, before the service returns its
StreamObserver
.Note that for cases where the message is received before the service handler is invoked, this method will have no effect. This is true for:
MethodDescriptor.MethodType.UNARY
operations.MethodDescriptor.MethodType.SERVER_STREAMING
operations.
-
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
.- Specified by:
isReady
in classCallStreamObserver<RespT>
-
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
.May only be called during the initial call to the application, before the service returns its
StreamObserver
.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.- Specified by:
setOnReadyHandler
in classCallStreamObserver<RespT>
- Parameters:
onReadyHandler
- to call when peer is ready to receive more messages.
-
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.
- Specified by:
request
in classCallStreamObserver<RespT>
- Parameters:
count
- more messages
-
setMessageCompression
public abstract void setMessageCompression(boolean enable)
Sets message compression for subsequent calls toStreamObserver.onNext(V)
.- Specified by:
setMessageCompression
in classCallStreamObserver<RespT>
- Parameters:
enable
- whether to enable compression.
-
setOnCloseHandler
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/8467") public void setOnCloseHandler(Runnable onCloseHandler)
Sets aRunnable
to be executed when the call is closed cleanly from the server's point of view: eitherStreamObserver.onCompleted()
orStreamObserver.onError(Throwable)
has been called, all the messages and trailing metadata have been sent and the stream has been closed. Note however that the client still may have not received all the messages due to network delay, client crashes, and cancellation races.Exactly one of
onCloseHandler
andonCancelHandler
is guaranteed to be called when the RPC terminates.It is guaranteed that execution of
onCloseHandler
is serialized with calls to the 'inbound'StreamObserver
. That also means that the callback will be delayed if other callbacks are running.This method may only be called during the initial call to the application, before the service returns its
request observer
.- Parameters:
onCloseHandler
- to execute when the call has been closed cleanly.
-
-