当前位置: 首页>>代码示例>>Java>>正文


Java ClientInterceptors类代码示例

本文整理汇总了Java中io.grpc.ClientInterceptors的典型用法代码示例。如果您正苦于以下问题:Java ClientInterceptors类的具体用法?Java ClientInterceptors怎么用?Java ClientInterceptors使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


ClientInterceptors类属于io.grpc包,在下文中一共展示了ClientInterceptors类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: metadataInterceptor

import io.grpc.ClientInterceptors; //导入依赖的package包/类
private ClientInterceptor metadataInterceptor() {
  ClientInterceptor interceptor = new ClientInterceptor() {
    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        final io.grpc.MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, final Channel next) {
      return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
        @Override
        protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
            throws StatusException {
          for (ConfigProto.CallMetadataEntry entry : callConfiguration.getMetadataList()) {
            Metadata.Key<String> key = Metadata.Key.of(entry.getName(), Metadata.ASCII_STRING_MARSHALLER);
            headers.put(key, entry.getValue());
          }
          delegate().start(responseListener, headers);
        }
      };
    }
  };

  return interceptor;
}
 
开发者ID:grpc-ecosystem,项目名称:polyglot,代码行数:22,代码来源:ChannelFactory.java

示例2: interceptCall

import io.grpc.ClientInterceptors; //导入依赖的package包/类
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions,
        final Channel next) {
    return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(
            next.newCall(method, callOptions)) {
        @Override
        protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
                throws StatusException {
            Metadata cachedSaved;
            URI uri = serviceUri(next, method);
            synchronized (this) {
                Map<String, List<String>> latestMetadata = getRequestMetadata(uri);
                if (mLastMetadata == null || mLastMetadata != latestMetadata) {
                    mLastMetadata = latestMetadata;
                    mCached = toHeaders(mLastMetadata);
                }
                cachedSaved = mCached;
            }
            headers.merge(cachedSaved);
            delegate().start(responseListener, headers);
        }
    };
}
 
开发者ID:hsavaliya,项目名称:GoogleAssistantSDK,代码行数:25,代码来源:SpeechService.java

示例3: SeldonClientExample

import io.grpc.ClientInterceptors; //导入依赖的package包/类
/** Construct client for accessing RouteGuide server using the existing channel. */
public SeldonClientExample(ManagedChannelBuilder<?> channelBuilder) {
 ClientInterceptor interceptor = new HeaderClientInterceptor();
  channel = channelBuilder.build();
  Channel interceptChannel = ClientInterceptors.intercept(channel, interceptor);
  blockingStub = SeldonGrpc.newBlockingStub(interceptChannel);
  asyncStub = SeldonGrpc.newStub(interceptChannel);
}
 
开发者ID:SeldonIO,项目名称:seldon-core,代码行数:9,代码来源:SeldonClientExample.java

示例4: interceptCall

import io.grpc.ClientInterceptors; //导入依赖的package包/类
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(final MethodDescriptor<ReqT, RespT> method,
                                                           CallOptions callOptions, final Channel next) {
    return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(
            next.newCall(method, callOptions)) {
        @Override
        protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
                throws StatusException {

            Metadata cachedSaved;
            URI uri = serviceUri(next, method);
            synchronized (GoogleCredentialsInterceptor.this) {
                Map<String, List<String>> latestMetadata = getRequestMetadata(uri);
                if (mLastMetadata == null || mLastMetadata != latestMetadata) {
                    mLastMetadata = latestMetadata;
                    mCached = toHeaders(mLastMetadata);
                }
                cachedSaved = mCached;
            }
            headers.merge(cachedSaved);
            delegate().start(responseListener, headers);
        }
    };
}
 
开发者ID:brandall76,项目名称:Saiy-PS,代码行数:25,代码来源:GoogleCredentialsInterceptor.java

示例5: initServerConnection

import io.grpc.ClientInterceptors; //导入依赖的package包/类
/**
 * Initializes a connection to the gRPC server.
 * @return a boolean indicating the success.
 */
private boolean initServerConnection() {
    if(!mConnected) {
        mInterceptor = new AuthHeaderClientInterceptor(
                getUserIdToken());
        try {
            mChannelImpl = OkHttpChannelBuilder
                    .forAddress(AndroidConstants.HOST,
                            AndroidConstants.PORT)
                    .build();
            Channel mOriginChannel = ClientInterceptors
                    .intercept(mChannelImpl, mInterceptor);
            mBlockingStub = AbelanaGrpc.newBlockingStub(mOriginChannel);
            mConnected = true;
        } catch (RuntimeException e) {
            mConnected = false;
        }
    }
    return mConnected;
}
 
开发者ID:googlearchive,项目名称:abelana,代码行数:24,代码来源:AbelanaClient.java

示例6: clientHeaderDeliveredToServer

import io.grpc.ClientInterceptors; //导入依赖的package包/类
@Test
public void clientHeaderDeliveredToServer() {
  grpcServerRule.getServiceRegistry()
      .addService(ServerInterceptors.intercept(new GreeterImplBase() {}, mockServerInterceptor));
  GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(
      ClientInterceptors.intercept(grpcServerRule.getChannel(), new HeaderClientInterceptor()));
  ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);

  try {
    blockingStub.sayHello(HelloRequest.getDefaultInstance());
    fail();
  } catch (StatusRuntimeException expected) {
    // expected because the method is not implemented at server side
  }

  verify(mockServerInterceptor).interceptCall(
      Matchers.<ServerCall<HelloRequest, HelloReply>>any(),
      metadataCaptor.capture(),
      Matchers.<ServerCallHandler<HelloRequest, HelloReply>>any());
  assertEquals(
      "customRequestValue",
      metadataCaptor.getValue().get(HeaderClientInterceptor.CUSTOM_HEADER_KEY));
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:24,代码来源:HeaderClientInterceptorTest.java

示例7: setUp

import io.grpc.ClientInterceptors; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
  faker = new Faker();
  Injector injector = Guice.createInjector();
  EchoService echoService = injector.getInstance(EchoService.class);
  ServiceInterceptor serviceInterceptor = injector.getInstance(ServiceInterceptor.class);
  CallerInterceptor callerInterceptor = injector.getInstance(CallerInterceptor.class);

  grpcServerRule.getServiceRegistry().addService(ServerInterceptors.intercept(echoService, serviceInterceptor));
  Channel channel = ClientInterceptors.intercept(
      grpcServerRule.getChannel(),
      callerInterceptor);
  stub = EchoServiceGrpc.newBlockingStub(channel);
}
 
开发者ID:email2liyang,项目名称:grpc-mate,代码行数:15,代码来源:EchoServiceTest.java

示例8: CustomHeaderClient

import io.grpc.ClientInterceptors; //导入依赖的package包/类
/**
 * A custom client.
 */
private CustomHeaderClient(String host, int port) {
  originChannel = ManagedChannelBuilder.forAddress(host, port)
      .usePlaintext(true)
      .build();
  ClientInterceptor interceptor = new HeaderClientInterceptor();
  Channel channel = ClientInterceptors.intercept(originChannel, interceptor);
  blockingStub = GreeterGrpc.newBlockingStub(channel);
}
 
开发者ID:lrtdc,项目名称:book_ldrtc,代码行数:12,代码来源:CustomHeaderClient.java

示例9: getChannel

import io.grpc.ClientInterceptors; //导入依赖的package包/类
/** Return {@link io.grpc.Channel} which is used by Cloud Pub/Sub gRPC API's. */
public static Channel getChannel() throws IOException {
  ManagedChannel channelImpl =
      NettyChannelBuilder.forAddress(ENDPOINT, 443).negotiationType(NegotiationType.TLS).build();
  final ClientAuthInterceptor interceptor =
      new ClientAuthInterceptor(
          GoogleCredentials.getApplicationDefault().createScoped(CPS_SCOPE),
          Executors.newCachedThreadPool());
  return ClientInterceptors.intercept(channelImpl, interceptor);
}
 
开发者ID:GoogleCloudPlatform,项目名称:pubsub,代码行数:11,代码来源:ConnectorUtils.java

示例10: createGatheringListener

import io.grpc.ClientInterceptors; //导入依赖的package包/类
/**
 * Wrap a Listener that will record the final Call status in onClose.
 */
Listener<ResponseT> createGatheringListener(Listener<ResponseT> responseListener) {
  return new ClientInterceptors.ForwardingListener<ResponseT>(responseListener) {
    @Override
    public void onClose(Status status, Metadata.Trailers trailers) {
      callCompletionStatuses.add(new CallCompletionStatus(method, status));
      super.onClose(status, trailers);
    }};
}
 
开发者ID:dmmcerlean,项目名称:cloud-bigtable-client,代码行数:12,代码来源:CallCompletionStatusInterceptor.java

示例11: HelloWorldClient

import io.grpc.ClientInterceptors; //导入依赖的package包/类
/** Construct client connecting to HelloWorld server at {@code host:port}. */
public HelloWorldClient(String address, String apiKey) {
  channel = ManagedChannelBuilder.forTarget(address)
      // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
      // needing certificates.
      .usePlaintext(true)
      .build();
  Channel ch = ClientInterceptors.intercept(channel,  new Interceptor(apiKey));

  blockingStub = GreeterGrpc.newBlockingStub(ch);
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:12,代码来源:HelloWorldClient.java

示例12: createBookstoreStub

import io.grpc.ClientInterceptors; //导入依赖的package包/类
static BookstoreGrpc.BookstoreBlockingStub createBookstoreStub(
    String address, String apiKey, String authToken) {
  Channel channel = ManagedChannelBuilder.forTarget(address)
      .usePlaintext(true)
      .build();

  channel = ClientInterceptors.intercept(channel,  new Interceptor(apiKey, authToken));

  return BookstoreGrpc.newBlockingStub(channel);
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:11,代码来源:BookstoreClient.java

示例13: main

import io.grpc.ClientInterceptors; //导入依赖的package包/类
public static void main(final String[] args) throws Exception {

        if (args.length == 0) {
            System.err.println("Please specify your project name.");
            System.exit(1);
        }
        final String project = args[0];
        ManagedChannelImpl channelImpl = NettyChannelBuilder
            .forAddress("pubsub.googleapis.com", 443)
            .negotiationType(NegotiationType.TLS)
            .build();
        GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
        // Down-scope the credential to just the scopes required by the service
        creds = creds.createScoped(Arrays.asList("https://www.googleapis.com/auth/pubsub"));
        // Intercept the channel to bind the credential
        ExecutorService executor = Executors.newSingleThreadExecutor();
        ClientAuthInterceptor interceptor = new ClientAuthInterceptor(creds, executor);
        Channel channel = ClientInterceptors.intercept(channelImpl, interceptor);
        // Create a stub using the channel that has the bound credential
        PublisherGrpc.PublisherBlockingStub publisherStub = PublisherGrpc.newBlockingStub(channel);
        ListTopicsRequest request = ListTopicsRequest.newBuilder()
                .setPageSize(10)
                .setProject("projects/" + project)
                .build();
        ListTopicsResponse resp = publisherStub.listTopics(request);
        System.out.println("Found " + resp.getTopicsCount() + " topics.");
        for (Topic topic : resp.getTopicsList()) {
            System.out.println(topic.getName());
        }
    }
 
开发者ID:GoogleCloudPlatform,项目名称:cloud-pubsub-samples-java,代码行数:31,代码来源:Main.java

示例14: newClient

import io.grpc.ClientInterceptors; //导入依赖的package包/类
private GrpcRemoteCache newClient() throws IOException {
  AuthAndTLSOptions authTlsOptions = Options.getDefaults(AuthAndTLSOptions.class);
  authTlsOptions.useGoogleDefaultCredentials = true;
  authTlsOptions.googleCredentials = "/exec/root/creds.json";
  authTlsOptions.googleAuthScopes = ImmutableList.of("dummy.scope");

  GenericJson json = new GenericJson();
  json.put("type", "authorized_user");
  json.put("client_id", "some_client");
  json.put("client_secret", "foo");
  json.put("refresh_token", "bar");
  Scratch scratch = new Scratch();
  scratch.file(authTlsOptions.googleCredentials, new JacksonFactory().toString(json));

  CallCredentials creds =
      GoogleAuthUtils.newCallCredentials(
          scratch.resolve(authTlsOptions.googleCredentials).getInputStream(),
          authTlsOptions.googleAuthScopes);
  RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
  RemoteRetrier retrier =
      new RemoteRetrier(
          remoteOptions, RemoteRetrier.RETRIABLE_GRPC_ERRORS, Retrier.ALLOW_ALL_CALLS);
  return new GrpcRemoteCache(
      ClientInterceptors.intercept(
          InProcessChannelBuilder.forName(fakeServerName).directExecutor().build(),
          ImmutableList.of(new CallCredentialsInterceptor(creds))),
      creds,
      remoteOptions,
      retrier,
      DIGEST_UTIL);
}
 
开发者ID:bazelbuild,项目名称:bazel,代码行数:32,代码来源:GrpcRemoteCacheTest.java

示例15: setUp

import io.grpc.ClientInterceptors; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
  grpcServerRule
      .getServiceRegistry()
      .addService(
          ServerInterceptors.intercept(greeterServiceImpl, injectCacheControlInterceptor));
  grpcServerRule.getServiceRegistry().addService(anotherGreeterServiceImpl);
  baseChannel = grpcServerRule.getChannel();

  SafeMethodCachingInterceptor interceptor =
      SafeMethodCachingInterceptor.newSafeMethodCachingInterceptor(cache);

  channelToUse = ClientInterceptors.intercept(baseChannel, interceptor);
}
 
开发者ID:grpc,项目名称:grpc-java,代码行数:15,代码来源:SafeMethodCachingInterceptorTest.java


注:本文中的io.grpc.ClientInterceptors类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。