高度な使用方法

このガイドでは、Java クライアント ライブラリの高度な機能をカスタマイズする方法の概要を説明します。一般的なパターンでは、これらの機能の多くは、標準のメソッドではなく、基盤となる Callable に依存しています。通常、この呼び出し可能関数は、ここに記載されていない他の RPC ごとの機能を探すのに適しています。

タイムアウト

Java ライブラリには、呼び出しごとにタイムアウトを設定するためのサーフェスが用意されています。デフォルト値は、googleads_grpc_service_config.jsonmethod_config/timeout の設定に基づいて設定されます。API 呼び出しの最大時間の上限を短くする必要がある場合は、より低い値を設定します。

この機能を使用するには、呼び出し可能オブジェクトを直接使用する必要があります。たとえば、GoogleAdsService.searchStream() を呼び出す場合、タイムアウトは次のように設定されます。

try (GoogleAdsServiceClient googleAdsServiceClient =
    googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
  // Constructs the SearchGoogleAdsStreamRequest.
  SearchGoogleAdsStreamRequest request = ...

  // Executes the API call, with a timeout of 5 minutes.
  ServerStream<SearchGoogleAdsStreamResponse> result = googleAdsServiceClient
      .searchStreamCallable()
      .call(request,
          GrpcCallContext.createDefault().withTimeout(Duration.of(5, ChronoUnit.MINUTES)));
}

タイムアウトを 2 時間以上に設定できますが、API が非常に長時間実行リクエストをタイムアウトして DEADLINE_EXCEEDED エラーを返す場合があります。これが問題になる場合は、通常、クエリを分割してチャンクを並行して実行することをおすすめします。これにより、長時間実行リクエストが失敗する状況が回避され、回復する唯一の方法は、最初からリクエストを再度トリガーすることです。

再試行の設定

Java ライブラリには、呼び出しごとに再試行設定を構成するサーフェスも用意されています。この機能を使用するには、呼び出し可能オブジェクトを直接使用する必要があります。たとえば、GoogleAdsService.searchStream() を呼び出す場合、再試行設定は次のように構成されます。

// Creates a context object with the custom retry settings.
GrpcCallContext context = GrpcCallContext.createDefault()
    .withRetrySettings(RetrySettings.newBuilder()
    .setInitialRetryDelay(Duration.ofMillis(10L))
    .setMaxRetryDelay(Duration.ofSeconds(10L))
    .setRetryDelayMultiplier(1.4)
    .setMaxAttempts(10)
    .setLogicalTimeout(Duration.ofSeconds(30L))
    .build());

// Creates and issues a search Google Ads stream request.
ServerStream<SearchGoogleAdsStreamResponse> stream =
    googleAdsServiceClient.searchStreamCallable().call(request, context);

起動時のパフォーマンスの最適化

GoogleAdsClient インスタンスが初めて作成されたときに、わずかな遅延が発生することがあります。これは、すべての API クラスを一度に読み込む、サービスの fluent インターフェース(GoogleAdsClient.getVersionXX())により、サービスクラスの作成をより便利なメカニズムとして利用できます。

最初のリクエストのパフォーマンスがアプリケーションのクリティカル パスにある場合は、次の操作を行います。

  1. 起動時に GoogleAdsClient を作成してから、ユーザー リクエストを処理します。

  2. プロセスが最初に開始したときに、Google Ads API にウォームアップ リクエストをいくつか送信します。次に例を示します。

    // Runs some warm-up requests.
    try (GoogleAdsServiceClient googleAdsServiceClient =
        googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
      // Runs 5 warm-up requests. In our profiling we see that 90% of performance
      // loss is only experienced on the first API call. After 3 subsequent calls we
      // saw a negligible improvement in performance.
      for (int i = 0; i < 5; ++i) {
        // Warm-up queries are run with a nonexistent CID so the calls will fail. If
        // you have a CID that you know will be accessible with the OAuth
        // credentials provided you may want to provide that instead and avoid the
        // try-catch.
        try {
          googleAdsServiceClient.search("-1", "Warm-up query");
        } catch (GoogleAdsException ex) {
          // Do nothing, we're expecting this to fail.
        }
      }
    }
    

ウォームアップ リクエストは、プロセスごとに 1 回だけ実行する必要があります。その後にサービス クライアントが作成されるたびに、プリロードされたクラスが自動的に再利用されます。

サービス クライアントの再利用

GoogleAdsClient.getVersionXXX().createYYYServiceClient() を呼び出すたびに新しい TCP 接続が作成されるため、サービス クライアント インスタンスは可能な限り再利用する必要があります。

不要になったクライアントは必ず閉じる必要があります。これを行うには、try-with-resources ブロックを使用するか、サービス クライアントで close() を呼び出します。

閉じたサービス クライアントを使用して API リクエストを実行しようとすると、サービス クライアント メソッドは java.util.concurrent.RejectedExecutionException をスローします。

JAR が 32 MB を超える場合 App Engine をデプロイできない

App Engine には、アップロードされたファイルごとに 32 MB の割り当てがあります。google-ads の JAR は、これよりもかなり大きく、シェード/シャドウ jar デプロイを使用するとさらに大きくなります。jar を手動でデプロイすると、次のようなエラーが発生することがあります。

ERROR: (gcloud.app.deploy) Cannot upload file [<your-app>/WEB-INF/lib/google-ads-14.0.0.jar],
which has size [66095767] (greater than maximum allowed size of [33554432])

代わりに、App Engine Gradle プラグインまたは Maven プラグインを使用してデプロイします。どちらにも enableJarSplitting のオプションがあり、各 jar を 10 MB のチャンクに分割してアップロードします。

シャドー依存関係

プロジェクトにライブラリの依存関係と競合する依存関係がある場合は、次のいずれかのコマンドを使用してプロジェクトの依存関係を調査し、必要に応じてプロジェクトの依存関係を変更する必要があります。

Maven

mvn dependency:tree

Gradle

./gradlew dependencies

依存関係の競合を解決できない場合は、ライブラリのshadedされたバージョンを使用することができます。

Maven

<dependency>
  <groupId>com.google.api-ads</groupId>
  <artifactId>google-ads-shadowjar</artifactId>
  <version>31.0.0</version>
</dependency>

Gradle

implementation 'com.google.api-ads:google-ads-shadowjar:31.0.0'