Class ServerCalls.ServerCallStreamObserverImpl<ReqT,RespT>
- java.lang.Object
-
- io.grpc.stub.CallStreamObserver<RespT>
-
- io.grpc.stub.ServerCallStreamObserver<RespT>
-
- io.grpc.stub.ServerCalls.ServerCallStreamObserverImpl<ReqT,RespT>
-
- All Implemented Interfaces:
StreamObserver<RespT>
- Enclosing class:
- ServerCalls
private static final class ServerCalls.ServerCallStreamObserverImpl<ReqT,RespT> extends ServerCallStreamObserver<RespT>
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
aborted
private boolean
autoRequestEnabled
(package private) ServerCall<ReqT,RespT>
call
(package private) boolean
cancelled
private boolean
completed
private boolean
frozen
private java.lang.Runnable
onCancelHandler
private java.lang.Runnable
onCloseHandler
private java.lang.Runnable
onReadyHandler
private boolean
sentHeaders
private boolean
serverStreamingOrBidi
-
Constructor Summary
Constructors Constructor Description ServerCallStreamObserverImpl(ServerCall<ReqT,RespT> call, boolean serverStreamingOrBidi)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.void
disableAutoRequest()
Swaps to manual flow control where no message will be delivered toStreamObserver.onNext(Object)
unless it isrequest()
ed.private void
freeze()
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.boolean
isReady()
Iftrue
, indicates that the observer is capable of sending additional messages without requiring excessive buffering internally.void
onCompleted()
Receives a notification of successful stream completion.void
onError(java.lang.Throwable t)
Receives a terminating error from the stream.void
onNext(RespT response)
Receives a value from the stream.void
request(int count)
Requests the peer to producecount
more messages to be delivered to the 'inbound'StreamObserver
.void
setCompression(java.lang.String compression)
Sets the compression algorithm to use for the call.void
setMessageCompression(boolean enable)
Sets message compression for subsequent calls toStreamObserver.onNext(V)
.void
setOnCancelHandler(java.lang.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(java.lang.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.void
setOnReadyHandler(java.lang.Runnable r)
Set aRunnable
that will be executed every time the streamServerCallStreamObserver.isReady()
state changes fromfalse
totrue
.void
setOnReadyThreshold(int numBytes)
A hint to the call that specifies how many bytes must be queued beforeServerCallStreamObserver.isReady()
will return false.
-
-
-
Field Detail
-
call
final ServerCall<ReqT,RespT> call
-
serverStreamingOrBidi
private final boolean serverStreamingOrBidi
-
cancelled
volatile boolean cancelled
-
frozen
private boolean frozen
-
autoRequestEnabled
private boolean autoRequestEnabled
-
sentHeaders
private boolean sentHeaders
-
onReadyHandler
private java.lang.Runnable onReadyHandler
-
onCancelHandler
private java.lang.Runnable onCancelHandler
-
aborted
private boolean aborted
-
completed
private boolean completed
-
onCloseHandler
private java.lang.Runnable onCloseHandler
-
-
Constructor Detail
-
ServerCallStreamObserverImpl
ServerCallStreamObserverImpl(ServerCall<ReqT,RespT> call, boolean serverStreamingOrBidi)
-
-
Method Detail
-
freeze
private void freeze()
-
setMessageCompression
public void setMessageCompression(boolean enable)
Description copied from class:ServerCallStreamObserver
Sets message compression for subsequent calls toStreamObserver.onNext(V)
.- Specified by:
setMessageCompression
in classServerCallStreamObserver<RespT>
- Parameters:
enable
- whether to enable compression.
-
setCompression
public void setCompression(java.lang.String compression)
Description copied from class:ServerCallStreamObserver
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.
- Specified by:
setCompression
in classServerCallStreamObserver<RespT>
- Parameters:
compression
- the compression algorithm to use.
-
onNext
public void onNext(RespT response)
Description copied from interface:StreamObserver
Receives a value from the stream.Can be called many times but is never called after
StreamObserver.onError(Throwable)
orStreamObserver.onCompleted()
are called.Unary calls must invoke onNext at most once. Clients may invoke onNext at most once for server streaming calls, but may receive many onNext callbacks. Servers may invoke onNext at most once for client streaming calls, but may receive many onNext callbacks.
If an exception is thrown by an implementation the caller is expected to terminate the stream by calling
StreamObserver.onError(Throwable)
with the caught exception prior to propagating it.- Parameters:
response
- the value passed to the stream
-
onError
public void onError(java.lang.Throwable t)
Description copied from interface:StreamObserver
Receives a terminating error from the stream.May only be called once and if called it must be the last method called. In particular if an exception is thrown by an implementation of
onError
no further calls to any method are allowed.t
should be aStatusException
orStatusRuntimeException
, but otherThrowable
types are possible. Callers should generally convert from aStatus
viaStatus.asException()
orStatus.asRuntimeException()
. Implementations should generally convert to aStatus
viaStatus.fromThrowable(Throwable)
.- Parameters:
t
- the error occurred on the stream
-
onCompleted
public void onCompleted()
Description copied from interface:StreamObserver
Receives a notification of successful stream completion.May only be called once and if called it must be the last method called. In particular if an exception is thrown by an implementation of
onCompleted
no further calls to any method are allowed.
-
isReady
public boolean isReady()
Description copied from class:ServerCallStreamObserver
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 toServerCallStreamObserver.setOnReadyHandler(java.lang.Runnable)
will be called afterisReady()
transitions totrue
.- Specified by:
isReady
in classServerCallStreamObserver<RespT>
-
setOnReadyHandler
public void setOnReadyHandler(java.lang.Runnable r)
Description copied from class:ServerCallStreamObserver
Set aRunnable
that will be executed every time the streamServerCallStreamObserver.isReady()
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 classServerCallStreamObserver<RespT>
- Parameters:
r
- to call when peer is ready to receive more messages.
-
isCancelled
public boolean isCancelled()
Description copied from class:ServerCallStreamObserver
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.
- Specified by:
isCancelled
in classServerCallStreamObserver<RespT>
-
setOnCancelHandler
public void setOnCancelHandler(java.lang.Runnable onCancelHandler)
Description copied from class:ServerCallStreamObserver
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 pollServerCallStreamObserver.isCancelled()
, 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.- Specified by:
setOnCancelHandler
in classServerCallStreamObserver<RespT>
- Parameters:
onCancelHandler
- to call when client has cancelled the call.
-
setOnReadyThreshold
public void setOnReadyThreshold(int numBytes)
Description copied from class:ServerCallStreamObserver
A hint to the call that specifies how many bytes must be queued beforeServerCallStreamObserver.isReady()
will return false. A call may ignore this property if unsupported. This may only be set during stream initialization before any messages are set.- Overrides:
setOnReadyThreshold
in classServerCallStreamObserver<RespT>
- Parameters:
numBytes
- The number of bytes that must be queued. Must be a positive integer.
-
disableAutoInboundFlowControl
public void disableAutoInboundFlowControl()
Description copied from class:CallStreamObserver
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 toCallStreamObserver.request(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)
.- Specified by:
disableAutoInboundFlowControl
in classCallStreamObserver<RespT>
-
disableAutoRequest
public void disableAutoRequest()
Description copied from class:ServerCallStreamObserver
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.
- Overrides:
disableAutoRequest
in classServerCallStreamObserver<RespT>
-
request
public void request(int count)
Description copied from class:ServerCallStreamObserver
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 classServerCallStreamObserver<RespT>
- Parameters:
count
- more messages
-
setOnCloseHandler
public void setOnCloseHandler(java.lang.Runnable onCloseHandler)
Description copied from class:ServerCallStreamObserver
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
.- Overrides:
setOnCloseHandler
in classServerCallStreamObserver<RespT>
- Parameters:
onCloseHandler
- to execute when the call has been closed cleanly.
-
-