Package io.grpc.stub

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 Details

    • 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 Runnable onReadyHandler
    • onCancelHandler

      private Runnable onCancelHandler
    • aborted

      private boolean aborted
    • completed

      private boolean completed
    • onCloseHandler

      private Runnable onCloseHandler
  • Constructor Details

    • ServerCallStreamObserverImpl

      ServerCallStreamObserverImpl(ServerCall<ReqT,RespT> call, boolean serverStreamingOrBidi)
  • Method Details

    • freeze

      private void freeze()
    • setMessageCompression

      public void setMessageCompression(boolean enable)
      Description copied from class: ServerCallStreamObserver
      Sets message compression for subsequent calls to StreamObserver.onNext(V).
      Specified by:
      setMessageCompression in class ServerCallStreamObserver<RespT>
      Parameters:
      enable - whether to enable compression.
    • setCompression

      public void setCompression(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 class ServerCallStreamObserver<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) or StreamObserver.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(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 a StatusException or StatusRuntimeException, but other Throwable types are possible. Callers should generally convert from a Status via Status.asException() or Status.asRuntimeException(). Implementations should generally convert to a Status via Status.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
      If true, 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 to ServerCallStreamObserver.setOnReadyHandler(java.lang.Runnable) will be called after isReady() transitions to true.

      Specified by:
      isReady in class ServerCallStreamObserver<RespT>
    • setOnReadyHandler

      public void setOnReadyHandler(Runnable r)
      Description copied from class: ServerCallStreamObserver
      Set a Runnable that will be executed every time the stream ServerCallStreamObserver.isReady() state changes from false to true. While it is not guaranteed that the same thread will always be used to execute the Runnable, 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 checking isReady()'s current value instead of assuming it is now true. If isReady() == false the normal expectations apply, so there would be another onReadyHandler callback.

      Specified by:
      setOnReadyHandler in class ServerCallStreamObserver<RespT>
      Parameters:
      r - to call when peer is ready to receive more messages.
    • isCancelled

      public boolean isCancelled()
      Description copied from class: ServerCallStreamObserver
      Returns true 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 class ServerCallStreamObserver<RespT>
    • setOnCancelHandler

      public void setOnCancelHandler(Runnable onCancelHandler)
      Description copied from class: ServerCallStreamObserver
      Sets a Runnable 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 poll ServerCallStreamObserver.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-op onCancelHandler is useful just to suppress the onNext() exception.

      Specified by:
      setOnCancelHandler in class ServerCallStreamObserver<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 before ServerCallStreamObserver.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 class ServerCallStreamObserver<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 to CallStreamObserver.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 its StreamObserver.

      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:

      This API is being replaced, but is not yet deprecated. On server-side it being replaced with ServerCallStreamObserver.disableAutoRequest(). On client-side disableAutoRequestWithInitial(1).

      Specified by:
      disableAutoInboundFlowControl in class CallStreamObserver<RespT>
    • disableAutoRequest

      public void disableAutoRequest()
      Description copied from class: ServerCallStreamObserver
      Swaps to manual flow control where no message will be delivered to StreamObserver.onNext(Object) unless it is request()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:

      Overrides:
      disableAutoRequest in class ServerCallStreamObserver<RespT>
    • request

      public void request(int count)
      Description copied from class: ServerCallStreamObserver
      Requests the peer to produce count 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 class ServerCallStreamObserver<RespT>
      Parameters:
      count - more messages
    • setOnCloseHandler

      public void setOnCloseHandler(Runnable onCloseHandler)
      Description copied from class: ServerCallStreamObserver
      Sets a Runnable to be executed when the call is closed cleanly from the server's point of view: either StreamObserver.onCompleted() or StreamObserver.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 and onCancelHandler 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 class ServerCallStreamObserver<RespT>
      Parameters:
      onCloseHandler - to execute when the call has been closed cleanly.