Java Code Examples for io.grpc.ForwardingClientCall#SimpleForwardingClientCall

The following examples show how to use io.grpc.ForwardingClientCall#SimpleForwardingClientCall . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MySecondClientInterceptor.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
        CallOptions callOptions, Channel next) {
    return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {

        @Override
        public void start(Listener<RespT> responseListener, Metadata headers) {
            super.start(
                    new ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT>(responseListener) {

                        @Override
                        protected Listener<RespT> delegate() {
                            callTime = System.nanoTime();
                            return super.delegate();
                        }
                    }, headers);
        }
    };
}
 
Example 2
Source File: MyFirstClientInterceptor.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
        CallOptions callOptions, Channel next) {
    return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {

        @Override
        public void start(Listener<RespT> responseListener, Metadata headers) {
            super.start(
                    new ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT>(responseListener) {

                        @Override
                        protected Listener<RespT> delegate() {
                            callTime = System.nanoTime();
                            return super.delegate();
                        }
                    }, headers);
        }
    };
}
 
Example 3
Source File: AuthClientInterceptor.java    From modeldb with Apache License 2.0 6 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions, Channel channel) {
  return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(
      channel.newCall(methodDescriptor, callOptions)) {
    @Override
    public void start(Listener<RespT> responseListener, Metadata headers) {
      // TODO: Here set request metadata
      Metadata.Key<String> email_key =
          Metadata.Key.of("email", Metadata.ASCII_STRING_MARSHALLER);
      Metadata.Key<String> dev_key =
          Metadata.Key.of("developer_key", Metadata.ASCII_STRING_MARSHALLER);
      Metadata.Key<String> source_key =
          Metadata.Key.of("source", Metadata.ASCII_STRING_MARSHALLER);

      headers.put(email_key, client1Email);
      headers.put(dev_key, client1DevKey);
      headers.put(source_key, "PythonClient");
      super.start(responseListener, headers);
    }
  };
}
 
Example 4
Source File: AuthClientInterceptor.java    From modeldb with Apache License 2.0 6 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions, Channel channel) {
  return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(
      channel.newCall(methodDescriptor, callOptions)) {
    @Override
    public void start(Listener<RespT> responseListener, Metadata headers) {
      // TODO: Here set request metadata
      Metadata.Key<String> email_key =
          Metadata.Key.of("email", Metadata.ASCII_STRING_MARSHALLER);
      Metadata.Key<String> dev_key =
          Metadata.Key.of("developer_key", Metadata.ASCII_STRING_MARSHALLER);
      Metadata.Key<String> source_key =
          Metadata.Key.of("source", Metadata.ASCII_STRING_MARSHALLER);

      headers.put(email_key, client2Email);
      headers.put(dev_key, client2DevKey);
      headers.put(source_key, "PythonClient");
      super.start(responseListener, headers);
    }
  };
}
 
Example 5
Source File: HelloWorldClient.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT,RespT> method, CallOptions callOptions, Channel next) {
  LOGGER.info("Intercepted " + method.getFullMethodName());
  ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);

  call = new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(call) {
    @Override
    public void start(Listener<RespT> responseListener, Metadata headers) {
      if (apiKey != null && !apiKey.isEmpty()) {
        LOGGER.info("Attaching API Key: " + apiKey);
        headers.put(API_KEY_HEADER, apiKey);
      }
      super.start(responseListener, headers);
    }
  };
  return call;
}
 
Example 6
Source File: BookstoreClient.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT,RespT> method, CallOptions callOptions, Channel next) {
  LOGGER.info("Intercepted " + method.getFullMethodName());
  ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);

  call = new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(call) {
    @Override
    public void start(Listener<RespT> responseListener, Metadata headers) {
      if (apiKey != null && !apiKey.isEmpty()) {
        LOGGER.info("Attaching API Key: " + apiKey);
        headers.put(API_KEY_HEADER, apiKey);
      }
      if (authToken != null && !authToken.isEmpty()) {
        System.out.println("Attaching auth token");
        headers.put(AUTHORIZATION_HEADER, "Bearer " + authToken);
      }
      super.start(responseListener, headers);
    }
  };
  return call;
}
 
Example 7
Source File: HelloWorldClientStream.java    From opentelemetry-java with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions, Channel channel) {
  return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(
      channel.newCall(methodDescriptor, callOptions)) {
    @Override
    public void start(Listener<RespT> responseListener, Metadata headers) {
      // Inject the request with the current context
      textFormat.inject(Context.current(), headers, setter);
      // Perform the gRPC request
      super.start(responseListener, headers);
    }
  };
}
 
Example 8
Source File: HelloWorldClient.java    From opentelemetry-java with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions, Channel channel) {
  return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(
      channel.newCall(methodDescriptor, callOptions)) {
    @Override
    public void start(Listener<RespT> responseListener, Metadata headers) {
      // Inject the request with the current context
      textFormat.inject(Context.current(), headers, setter);
      // Perform the gRPC request
      super.start(responseListener, headers);
    }
  };
}
 
Example 9
Source File: ClientAuthInterceptor.java    From startup-os with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions, Channel channel) {
  return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(
      channel.newCall(methodDescriptor, callOptions)) {
    @Override
    public void start(Listener<RespT> listener, Metadata metadata) {
      metadata.put(Metadata.Key.of("token", ASCII_STRING_MARSHALLER), tokenValue);
      super.start(listener, metadata);
    }
  };
}
 
Example 10
Source File: SupportCommandCodeClientInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
    final ClientCall<ReqT, RespT> clientCall = next.newCall(method, callOptions);
    final ClientCall<ReqT, RespT> forwardingClientCall = new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(clientCall) {
        @Override
        public void start(Listener<RespT> responseListener, Metadata headers) {
            for (Short code : supportCommandCodes) {
                headers.put(Header.SUPPORT_COMMAND_CODE, String.valueOf(code));
            }
            super.start(responseListener, headers);
        }

    };
    return forwardingClientCall;
}
 
Example 11
Source File: ClientHeaderClientInterceptor.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
                                                           CallOptions callOptions, Channel next) {
    //这里和下面不在一个线程
    if (RpcRunningState.isDebugMode()) {
        LOGGER.info("[1]header send from client:");
    }
    return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(
        method, callOptions)) {

        @Override
        public void start(Listener<RespT> responseListener, Metadata requestHeader) {

            RpcInvokeContext context = RpcInvokeContext.getContext();
            SofaRequest sofaRequest = (SofaRequest) context.get(TripleContants.SOFA_REQUEST_KEY);

            ConsumerConfig consumerConfig = (ConsumerConfig) context.get(TripleContants.SOFA_CONSUMER_CONFIG_KEY);
            TripleTracerAdapter.beforeSend(sofaRequest, consumerConfig, requestHeader);
            if (RpcRunningState.isDebugMode()) {
                LOGGER.info("[2]prepare to send from client:{}", requestHeader);
            }
            super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
                @Override
                public void onHeaders(Metadata responseHeader) {
                    // 客户端收到响应Header
                    if (RpcRunningState.isDebugMode()) {
                        LOGGER.info("[3]response header received from server:{}", responseHeader);
                    }
                    super.onHeaders(responseHeader);
                }

                @Override
                public void onMessage(RespT message) {
                    if (RpcRunningState.isDebugMode()) {
                        LOGGER.info("[4]response message received from server:{}", message);
                    }
                    super.onMessage(message);
                }

                @Override
                public void onClose(Status status, Metadata trailers) {
                    if (RpcRunningState.isDebugMode()) {
                        LOGGER.info("[5]response close received from server:{},trailers:{}", status, trailers);
                    }
                    super.onClose(status, trailers);
                }

                @Override
                public void onReady() {
                    if (RpcRunningState.isDebugMode()) {
                        LOGGER.info("[5]client is ready");
                    }
                    super.onReady();
                }
            }, requestHeader);
        }

    };
}