列出数据流作业的javaapi

huwehgph  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(264)

我正在尝试编写java代码来列出数据流作业。我引用了https://cloud.google.com/dataflow/docs/reference/rest/v1b3/projects.jobs/list 但“googlecredential”类已被弃用。我试着替换 GoogleCredentialGoogleCredentials 但在这条线下

Dataflow dataflowService = new Dataflow.Builder(httpTransport, jsonFactory, credential)
      .setApplicationName("Google Cloud Platform Sample")
      .build();

它需要httprequestinitializer而不是credential。
有人能帮助我如何使用凭证本身并解决此问题吗?

xzlaal3s

xzlaal3s1#

HttpRequestInitializer 需要在失败时执行自动重试。一个简单的 HttpRequestInitializer 可以使用 com.google.api.client.auth.oauth2.Credential 具体如下:

private HttpRequestInitializer buildInitializer(Credential credential) {
    return httpRequest -> {
      // handles abnormal HTTP responses (non 2XX responses)
      final HttpUnsuccessfulResponseHandler unsuccessfulResponseHandler =
          new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff())
              .setSleeper(Sleeper.DEFAULT);
      /*
      this is designed to work with only one HttpRequest at a time.
      Hence, a new instance of HttpBackOffIOExceptionHandler with new instance of
      BackOff is created for each instance of HttpRequest
      */
      HttpBackOffIOExceptionHandler httpBackOffIOExceptionHandler =
          new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(Sleeper.DEFAULT);
      httpRequest.setInterceptor(credential).setUnsuccessfulResponseHandler(unsuccessfulResponseHandler)
          .setIOExceptionHandler(httpBackOffIOExceptionHandler)
          .setConnectTimeout((int) TimeUnit.MINUTES.toMillis(3))
          .setReadTimeout((int) TimeUnit.MINUTES.toMillis(3))
          .setThrowExceptionOnExecuteError(false).setSuppressUserAgentSuffix(true);

    };
  }

相关问题