Home | History | Annotate | Download | only in stub
      1 /*
      2  * Copyright 2016 The gRPC Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package io.grpc.stub;
     18 
     19 import io.grpc.ExperimentalApi;
     20 
     21 /**
     22  * A refinement of {@link CallStreamObserver} to allows for interaction with call
     23  * cancellation events on the server side.
     24  *
     25  * <p>Like {@code StreamObserver}, implementations are not required to be thread-safe; if multiple
     26  * threads will be writing to an instance concurrently, the application must synchronize its calls.
     27  *
     28  * <p>DO NOT MOCK: The API is too complex to reliably mock. Use InProcessChannelBuilder to create
     29  * "real" RPCs suitable for testing and interact with the server using a normal client stub.
     30  */
     31 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1788")
     32 public abstract class ServerCallStreamObserver<V> extends CallStreamObserver<V> {
     33 
     34   /**
     35    * If {@code true} indicates that the call has been cancelled by the remote peer.
     36    *
     37    * <p>This method may safely be called concurrently from multiple threads.
     38    */
     39   public abstract boolean isCancelled();
     40 
     41   /**
     42    * Set a {@link Runnable} that will be called if the calls  {@link #isCancelled()} state
     43    * changes from {@code false} to {@code true}. It is guaranteed that execution of the
     44    * {@link Runnable} are serialized with calls to the 'inbound' {@link StreamObserver}.
     45    *
     46    * <p>Note that the handler may be called some time after {@link #isCancelled} has transitioned to
     47    * {@code true} as other callbacks may still be executing in the 'inbound' observer.
     48    *
     49    * @param onCancelHandler to call when client has cancelled the call.
     50    */
     51   public abstract void setOnCancelHandler(Runnable onCancelHandler);
     52 
     53   /**
     54    * Sets the compression algorithm to use for the call.  May only be called before sending any
     55    * messages.
     56    *
     57    * @param compression the compression algorithm to use.
     58    */
     59   public abstract void setCompression(String compression);
     60 }
     61