示例#1
0
 @Override
 public final synchronized void start(Listener listener) {
   Preconditions.checkState(this.listener == null, "already started");
   timerService = SharedResourceHolder.get(timerServiceResource);
   executor = SharedResourceHolder.get(executorResource);
   this.listener = Preconditions.checkNotNull(listener, "listener");
   resolve();
 }
  ManagedChannelImpl(
      String target,
      BackoffPolicy.Provider backoffPolicyProvider,
      NameResolver.Factory nameResolverFactory,
      Attributes nameResolverParams,
      LoadBalancer.Factory loadBalancerFactory,
      ClientTransportFactory transportFactory,
      DecompressorRegistry decompressorRegistry,
      CompressorRegistry compressorRegistry,
      @Nullable Executor executor,
      @Nullable String userAgent,
      List<ClientInterceptor> interceptors) {
    if (executor == null) {
      usingSharedExecutor = true;
      this.executor = SharedResourceHolder.get(GrpcUtil.SHARED_CHANNEL_EXECUTOR);
    } else {
      usingSharedExecutor = false;
      this.executor = executor;
    }
    this.backoffPolicyProvider = backoffPolicyProvider;
    this.nameResolver = getNameResolver(target, nameResolverFactory, nameResolverParams);
    this.loadBalancer = loadBalancerFactory.newLoadBalancer(nameResolver.getServiceAuthority(), tm);
    this.transportFactory = transportFactory;
    this.userAgent = userAgent;
    this.interceptorChannel = ClientInterceptors.intercept(new RealChannel(), interceptors);
    scheduledExecutor = SharedResourceHolder.get(TIMER_SERVICE);
    this.decompressorRegistry = decompressorRegistry;
    this.compressorRegistry = compressorRegistry;

    this.nameResolver.start(
        new NameResolver.Listener() {
          @Override
          public void onUpdate(List<ResolvedServerInfo> servers, Attributes config) {
            loadBalancer.handleResolvedAddresses(servers, config);
          }

          @Override
          public void onError(Status error) {
            Preconditions.checkArgument(!error.isOk(), "the error status must not be OK");
            loadBalancer.handleNameResolutionError(error);
          }
        });
  }