Java Code Examples for io.grpc.Channel#newCall()

The following examples show how to use io.grpc.Channel#newCall() . 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: BinaryLogProviderTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void wrapChannel_methodDescriptor() throws Exception {
  final AtomicReference<MethodDescriptor<?, ?>> methodRef =
      new AtomicReference<>();
  Channel channel = new Channel() {
    @Override
    public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(
        MethodDescriptor<RequestT, ResponseT> method, CallOptions callOptions) {
      methodRef.set(method);
      return new NoopClientCall<>();
    }

    @Override
    public String authority() {
      throw new UnsupportedOperationException();
    }
  };
  Channel wChannel = binlogProvider.wrapChannel(channel);
  ClientCall<String, Integer> unusedClientCall = wChannel.newCall(method, CallOptions.DEFAULT);
  validateWrappedMethod(methodRef.get());
}
 
Example 2
Source File: AltusMetadataInterceptor.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Override
public <R, S> ClientCall<R, S> interceptCall(
        MethodDescriptor<R, S> method,
        CallOptions callOptions,
        Channel next) {
    return new SimpleForwardingClientCall<R, S>(
            next.newCall(method, callOptions)) {
        @Override
        public void start(
                Listener<S> responseListener,
                Metadata headers) {
            Metadata metadata = new Metadata();
            metadata.put(REQUEST_ID_METADATA_KEY, requestId);
            metadata.put(ACTOR_CRN_METADATA_KEY, actorCrn);
            headers.merge(metadata);
            super.start(responseListener, headers);
        }
    };
}
 
Example 3
Source File: CensusStatsModule.java    From grpc-nebula-java 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) {
  // New RPCs on client-side inherit the tag context from the current Context.
  TagContext parentCtx = tagger.getCurrentTagContext();
  final ClientCallTracer tracerFactory =
      newClientCallTracer(parentCtx, method.getFullMethodName(),
          recordStartedRpcs, recordFinishedRpcs);
  ClientCall<ReqT, RespT> call =
      next.newCall(method, callOptions.withStreamTracerFactory(tracerFactory));
  return new SimpleForwardingClientCall<ReqT, RespT>(call) {
    @Override
    public void start(Listener<RespT> responseListener, Metadata headers) {
      delegate().start(
          new SimpleForwardingClientCallListener<RespT>(responseListener) {
            @Override
            public void onClose(Status status, Metadata trailers) {
              tracerFactory.callEnded(status);
              super.onClose(status, trailers);
            }
          },
          headers);
    }
  };
}
 
Example 4
Source File: HeaderServerInterceptorTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void serverHeaderDeliveredToClient() {
  class SpyingClientInterceptor implements ClientInterceptor {
    ClientCall.Listener<?> spyListener;

    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
      return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
        @Override
        public void start(Listener<RespT> responseListener, Metadata headers) {
          spyListener = responseListener =
              mock(ClientCall.Listener.class, delegatesTo(responseListener));
          super.start(responseListener, headers);
        }
      };
    }
  }

  SpyingClientInterceptor clientInterceptor = new SpyingClientInterceptor();
  GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(channel)
      .withInterceptors(clientInterceptor);
  ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);

  blockingStub.sayHello(HelloRequest.getDefaultInstance());

  assertNotNull(clientInterceptor.spyListener);
  verify(clientInterceptor.spyListener).onHeaders(metadataCaptor.capture());
  assertEquals(
      "customRespondValue",
      metadataCaptor.getValue().get(HeaderServerInterceptor.CUSTOM_HEADER_KEY));
}
 
Example 5
Source File: GrpcCacheClientTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> interceptCall(
    MethodDescriptor<RequestT, ResponseT> method, CallOptions callOptions, Channel next) {
  assertThat(callOptions.getCredentials()).isEqualTo(credentials);
  // Remove the call credentials to allow testing with dummy ones.
  return next.newCall(method, callOptions.withCallCredentials(null));
}
 
Example 6
Source File: MetricCollectingClientInterceptor.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
@Override
public <Q, A> ClientCall<Q, A> interceptCall(final MethodDescriptor<Q, A> methodDescriptor,
        final CallOptions callOptions, final Channel channel) {
    final MetricSet metrics = metricsFor(methodDescriptor);
    return new MetricCollectingClientCall<>(
            channel.newCall(methodDescriptor, callOptions),
            this.registry,
            metrics.getRequestCounter(),
            metrics.getResponseCounter(),
            metrics.getTimerFunction());
}
 
Example 7
Source File: LoggingInterceptor.java    From bazel 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) {
  ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
  @SuppressWarnings("unchecked") // handler matches method, but that type is inexpressible
  LoggingHandler<ReqT, RespT> handler = selectHandler(method);
  if (handler != null) {
    return new LoggingForwardingCall<>(call, handler, method);
  } else {
    return call;
  }
}
 
Example 8
Source File: UnaryCallDeadlineInterceptor.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) {
    if (MethodDescriptor.MethodType.UNARY == method.getType()) {
        if (logger.isDebugEnabled()) {
            logger.debug("interceptCall {}", method.getFullMethodName());
        }
        return next.newCall(method, callOptions.withDeadlineAfter(timeoutMillis, TimeUnit.MILLISECONDS));
    } else {
        return next.newCall(method, callOptions);
    }
}
 
Example 9
Source File: CompressionTest.java    From grpc-java 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) {
  if (clientEncoding && serverAcceptEncoding) {
    callOptions = callOptions.withCompression("fzip");
  }
  ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);

  return new ClientCompressor<>(call);
}
 
Example 10
Source File: ClientConnectionManager.java    From jetcd 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) {
    return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
        @Override
        public void start(Listener<RespT> responseListener, Metadata headers) {
            String token = getToken(next);
            if (token != null) {
                headers.put(TOKEN, token);
            }
            super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
                @Override
                public void onClose(Status status, Metadata trailers) {
                    if (isInvalidTokenError(status)) {
                        try {
                            refreshToken(next);
                        } catch (Exception e) {
                            // don't throw any error here.
                            // rpc will retry on expired auth token.
                        }
                    }
                    super.onClose(status, trailers);
                }
            }, headers);
        }
    };
}
 
Example 11
Source File: CompressionTest.java    From grpc-nebula-java 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) {
  if (clientEncoding && serverAcceptEncoding) {
    callOptions = callOptions.withCompression("fzip");
  }
  ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);

  return new ClientCompressor<ReqT, RespT>(call);
}
 
Example 12
Source File: MyClientStreamInterceptor.java    From dubbo-samples 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) {

    final ClientCall<ReqT, RespT> wrappedCall = channel.newCall(methodDescriptor, callOptions);
    return new StreamRequestClientCall<>(wrappedCall);

}
 
Example 13
Source File: CancellableInterceptor.java    From grpc-java 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) {
  ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
  this.call = call;
  return call;
}
 
Example 14
Source File: OrderedClientInterceptorConfiguration.java    From grpc-spring-boot-starter with MIT License 4 votes vote down vote up
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
    return next.newCall(method, callOptions);
}
 
Example 15
Source File: MetadataUtils.java    From grpc-nebula-java 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) {
  return new HeaderAttachingClientCall<ReqT, RespT>(next.newCall(method, callOptions));
}
 
Example 16
Source File: MetadataUtils.java    From grpc-java 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) {
  return new MetadataCapturingClientCall<>(next.newCall(method, callOptions));
}
 
Example 17
Source File: OrcaMetricReportingServerInterceptorTest.java    From grpc-java 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) {
  return new TrailersCapturingClientCall<>(next.newCall(method, callOptions));
}
 
Example 18
Source File: OrderedClientInterceptorConfiguration.java    From grpc-spring-boot-starter with MIT License 4 votes vote down vote up
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
    return next.newCall(method, callOptions);
}
 
Example 19
Source File: OrderedClientInterceptorConfiguration.java    From grpc-spring-boot-starter with MIT License 4 votes vote down vote up
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
    return next.newCall(method, callOptions);
}
 
Example 20
Source File: ByteStreamServiceTest.java    From bazel-buildfarm with Apache License 2.0 4 votes vote down vote up
@Test
public void writeCanBeResumed() throws IOException, InterruptedException {
  ByteString helloWorld = ByteString.copyFromUtf8("Hello, World!");
  Digest digest = DIGEST_UTIL.compute(helloWorld);
  String uuid = UUID.randomUUID().toString();
  String resourceName = createBlobUploadResourceName(uuid, digest);

  Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build();
  ClientCall<WriteRequest, WriteResponse> initialCall =
      channel.newCall(ByteStreamGrpc.getWriteMethod(), CallOptions.DEFAULT);
  ByteString initialData = helloWorld.substring(0, 6);
  ClientCall.Listener<WriteResponse> initialCallListener =
      new ClientCall.Listener<WriteResponse>() {
        boolean complete = false;
        boolean callHalfClosed = false;

        @Override
        public void onReady() {
          while (initialCall.isReady()) {
            if (complete) {
              if (!callHalfClosed) {
                initialCall.halfClose();
                callHalfClosed = true;
              }
              return;
            }

            initialCall.sendMessage(
                WriteRequest.newBuilder()
                    .setResourceName(resourceName)
                    .setData(initialData)
                    .build());
            complete = true;
          }
        }
      };

  initialCall.start(initialCallListener, new Metadata());
  initialCall.request(1);

  ByteStreamBlockingStub service = ByteStreamGrpc.newBlockingStub(channel);
  QueryWriteStatusResponse response =
      service.queryWriteStatus(
          QueryWriteStatusRequest.newBuilder().setResourceName(resourceName).build());
  assertThat(response.getCommittedSize()).isEqualTo(initialData.size());
  assertThat(response.getComplete()).isFalse();

  ClientCall<WriteRequest, WriteResponse> finishCall =
      channel.newCall(ByteStreamGrpc.getWriteMethod(), CallOptions.DEFAULT);
  ClientCall.Listener<WriteResponse> finishCallListener =
      new ClientCall.Listener<WriteResponse>() {
        boolean complete = false;
        boolean callHalfClosed = false;

        @Override
        public void onReady() {
          while (finishCall.isReady()) {
            if (complete) {
              if (!callHalfClosed) {
                finishCall.halfClose();
                callHalfClosed = true;
              }
              return;
            }

            finishCall.sendMessage(
                WriteRequest.newBuilder()
                    .setResourceName(resourceName)
                    .setWriteOffset(initialData.size())
                    .setData(helloWorld.substring(initialData.size()))
                    .setFinishWrite(true)
                    .build());
            complete = true;
          }
        }
      };

  finishCall.start(finishCallListener, new Metadata());
  finishCall.request(1);

  ArgumentCaptor<InputStream> inputStreamCaptor = ArgumentCaptor.forClass(InputStream.class);

  verify(simpleBlobStore, times(1))
      .put(eq(digest.getHash()), eq(digest.getSizeBytes()), inputStreamCaptor.capture());
  InputStream inputStream = inputStreamCaptor.getValue();
  assertThat(inputStream.available()).isEqualTo(helloWorld.size());
  byte[] data = new byte[helloWorld.size()];
  assertThat(inputStream.read(data)).isEqualTo(helloWorld.size());
  assertThat(data).isEqualTo(helloWorld.toByteArray());
}