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 booleanabortedprivate booleanautoRequestEnabled(package private) ServerCall<ReqT,RespT>call(package private) booleancancelledprivate booleancompletedprivate booleanfrozenprivate java.lang.RunnableonCancelHandlerprivate java.lang.RunnableonCloseHandlerprivate java.lang.RunnableonReadyHandlerprivate booleansentHeadersprivate booleanserverStreamingOrBidi
-
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 voiddisableAutoInboundFlowControl()Disables automatic flow control where a token is returned to the peer after a call to the 'inbound'StreamObserver.onNext(Object)has completed.voiddisableAutoRequest()Swaps to manual flow control where no message will be delivered toStreamObserver.onNext(Object)unless it isrequest()ed.private voidfreeze()booleanisCancelled()Returnstruewhen 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.booleanisReady()Iftrue, indicates that the observer is capable of sending additional messages without requiring excessive buffering internally.voidonCompleted()Receives a notification of successful stream completion.voidonError(java.lang.Throwable t)Receives a terminating error from the stream.voidonNext(RespT response)Receives a value from the stream.voidrequest(int count)Requests the peer to producecountmore messages to be delivered to the 'inbound'StreamObserver.voidsetCompression(java.lang.String compression)Sets the compression algorithm to use for the call.voidsetMessageCompression(boolean enable)Sets message compression for subsequent calls toStreamObserver.onNext(V).voidsetOnCancelHandler(java.lang.Runnable onCancelHandler)Sets aRunnableto 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.voidsetOnCloseHandler(java.lang.Runnable onCloseHandler)Sets aRunnableto 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.voidsetOnReadyHandler(java.lang.Runnable r)Set aRunnablethat will be executed every time the streamServerCallStreamObserver.isReady()state changes fromfalsetotrue.voidsetOnReadyThreshold(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:ServerCallStreamObserverSets message compression for subsequent calls toStreamObserver.onNext(V).- Specified by:
setMessageCompressionin classServerCallStreamObserver<RespT>- Parameters:
enable- whether to enable compression.
-
setCompression
public void setCompression(java.lang.String compression)
Description copied from class:ServerCallStreamObserverSets 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:
setCompressionin classServerCallStreamObserver<RespT>- Parameters:
compression- the compression algorithm to use.
-
onNext
public void onNext(RespT response)
Description copied from interface:StreamObserverReceives 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:StreamObserverReceives 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
onErrorno further calls to any method are allowed.tshould be aStatusExceptionorStatusRuntimeException, but otherThrowabletypes are possible. Callers should generally convert from aStatusviaStatus.asException()orStatus.asRuntimeException(). Implementations should generally convert to aStatusviaStatus.fromThrowable(Throwable).- Parameters:
t- the error occurred on the stream
-
onCompleted
public void onCompleted()
Description copied from interface:StreamObserverReceives 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
onCompletedno further calls to any method are allowed.
-
isReady
public boolean isReady()
Description copied from class:ServerCallStreamObserverIftrue, 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:
isReadyin classServerCallStreamObserver<RespT>
-
setOnReadyHandler
public void setOnReadyHandler(java.lang.Runnable r)
Description copied from class:ServerCallStreamObserverSet aRunnablethat will be executed every time the streamServerCallStreamObserver.isReady()state changes fromfalsetotrue. 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() == falsewithin this callback. Handle "spurious" notifications by checkingisReady()'s current value instead of assuming it is nowtrue. IfisReady() == falsethe normal expectations apply, so there would be anotheronReadyHandlercallback.- Specified by:
setOnReadyHandlerin classServerCallStreamObserver<RespT>- Parameters:
r- to call when peer is ready to receive more messages.
-
isCancelled
public boolean isCancelled()
Description copied from class:ServerCallStreamObserverReturnstruewhen 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:
isCancelledin classServerCallStreamObserver<RespT>
-
setOnCancelHandler
public void setOnCancelHandler(java.lang.Runnable onCancelHandler)
Description copied from class:ServerCallStreamObserverSets aRunnableto 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
Runnableis 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-oponCancelHandleris useful just to suppress theonNext()exception.- Specified by:
setOnCancelHandlerin classServerCallStreamObserver<RespT>- Parameters:
onCancelHandler- to call when client has cancelled the call.
-
setOnReadyThreshold
public void setOnReadyThreshold(int numBytes)
Description copied from class:ServerCallStreamObserverA 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:
setOnReadyThresholdin 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:CallStreamObserverDisables 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.UNARYoperations on both the client and server.MethodDescriptor.MethodType.CLIENT_STREAMINGoperations on the client.MethodDescriptor.MethodType.SERVER_STREAMINGoperations 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:
disableAutoInboundFlowControlin classCallStreamObserver<RespT>
-
disableAutoRequest
public void disableAutoRequest()
Description copied from class:ServerCallStreamObserverSwaps 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.UNARYoperations.MethodDescriptor.MethodType.SERVER_STREAMINGoperations.
- Overrides:
disableAutoRequestin classServerCallStreamObserver<RespT>
-
request
public void request(int count)
Description copied from class:ServerCallStreamObserverRequests the peer to producecountmore messages to be delivered to the 'inbound'StreamObserver.This method is safe to call from multiple threads without external synchronization.
- Specified by:
requestin classServerCallStreamObserver<RespT>- Parameters:
count- more messages
-
setOnCloseHandler
public void setOnCloseHandler(java.lang.Runnable onCloseHandler)
Description copied from class:ServerCallStreamObserverSets aRunnableto 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
onCloseHandlerandonCancelHandleris guaranteed to be called when the RPC terminates.It is guaranteed that execution of
onCloseHandleris 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:
setOnCloseHandlerin classServerCallStreamObserver<RespT>- Parameters:
onCloseHandler- to execute when the call has been closed cleanly.
-
-