amitsaurav
amitsaurav

Reputation: 531

ServerInterceptor lifecycle for a bidirectional streaming API

I have ServerInterceptor implementation as follows:

public class MyInterceptor implements ServerInterceptor {
  public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
            final ServerCall<ReqT, RespT> serverCall,
            final Metadata headers,
            final ServerCallHandler<ReqT, RespT> serverCallHandler) {
  
  System.out.println("Call started");

  return new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(serverCallHandler.startCall(serverCall, headers)) {
    public void onMessage(ReqT message) {
      System.out.println("onMessage");
      super.onMessage(message);
    }

    public void onCancel() {
      System.out.println("onCancel");
      super.onCancel();
    }

    public void onComplete() {
      System.out.println("onComplete");
      super.onComplete();
    };
  }
}

I am expecting that for a bidirectional streaming API call, I'll see one "Call Started", multiple "onMessage" (depending on the number of requests in the stream) and one of either "onComplete" or "onCancel".

Similarly for unary API call, I am expecting one each of "Call Started", "onMessage" and "onComplete/onCancel" per call.

Is that correct?

Upvotes: 0

Views: 776

Answers (1)

HelloWood
HelloWood

Reputation: 805

That's correct. The event order of method you can reference this picture: enter image description here

You can read this note for more detail: https://helloworlde.github.io/2021/02/20/gRPC-%E6%9C%8D%E5%8A%A1%E9%97%B4%E8%B0%83%E7%94%A8%E4%BA%8B%E4%BB%B6%E6%B5%81%E7%A8%8B/

Upvotes: 1

Related Questions