|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.ads.googleads.examples.misc; |
| 16 | + |
| 17 | +import com.beust.jcommander.Parameter; |
| 18 | +import com.google.ads.googleads.examples.utils.ArgumentNames; |
| 19 | +import com.google.ads.googleads.examples.utils.CodeSampleParams; |
| 20 | +import com.google.ads.googleads.lib.GoogleAdsClient; |
| 21 | +import com.google.ads.googleads.v6.errors.GoogleAdsError; |
| 22 | +import com.google.ads.googleads.v6.errors.GoogleAdsException; |
| 23 | +import com.google.ads.googleads.v6.services.GoogleAdsRow; |
| 24 | +import com.google.ads.googleads.v6.services.GoogleAdsServiceClient; |
| 25 | +import com.google.ads.googleads.v6.services.GoogleAdsServiceClient.SearchPagedResponse; |
| 26 | +import com.google.ads.googleads.v6.services.SearchGoogleAdsRequest; |
| 27 | +import com.google.ads.googleads.v6.services.SearchGoogleAdsStreamRequest; |
| 28 | +import com.google.ads.googleads.v6.services.SearchGoogleAdsStreamResponse; |
| 29 | +import com.google.api.gax.grpc.GrpcCallContext; |
| 30 | +import com.google.api.gax.rpc.ServerStream; |
| 31 | +import com.google.api.gax.rpc.StatusCode.Code; |
| 32 | +import java.io.FileNotFoundException; |
| 33 | +import java.io.IOException; |
| 34 | +import org.threeten.bp.Duration; |
| 35 | + |
| 36 | +/** |
| 37 | + * Illustrates the use of custom client timeouts in the context of server streaming and unary calls. |
| 38 | + * |
| 39 | + * <p>For more information about the concepts, see this documentation: |
| 40 | + * https://grpc.io/docs/what-is-grpc/core-concepts/#rpc-life-cycle. |
| 41 | + */ |
| 42 | +public class SetCustomClientTimeouts { |
| 43 | + private static final int CLIENT_TIMEOUT_MILLIS = 5 * 60 * 1000; // 5 minutes in milliseconds. |
| 44 | + |
| 45 | + private static class SetCustomClientTimeoutsParams extends CodeSampleParams { |
| 46 | + |
| 47 | + @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true) |
| 48 | + private Long customerId; |
| 49 | + } |
| 50 | + |
| 51 | + public static void main(String[] args) throws IOException { |
| 52 | + SetCustomClientTimeoutsParams params = new SetCustomClientTimeoutsParams(); |
| 53 | + if (!params.parseArguments(args)) { |
| 54 | + |
| 55 | + // Either pass the required parameters for this example on the command line, or insert them |
| 56 | + // into the code here. See the parameter class definition above for descriptions. |
| 57 | + params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); |
| 58 | + } |
| 59 | + |
| 60 | + GoogleAdsClient googleAdsClient = null; |
| 61 | + try { |
| 62 | + googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); |
| 63 | + } catch (FileNotFoundException fnfe) { |
| 64 | + System.err.printf( |
| 65 | + "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); |
| 66 | + System.exit(1); |
| 67 | + } catch (IOException ioe) { |
| 68 | + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); |
| 69 | + System.exit(1); |
| 70 | + } |
| 71 | + |
| 72 | + try { |
| 73 | + new SetCustomClientTimeouts().runExample(googleAdsClient, params.customerId); |
| 74 | + } catch (GoogleAdsException gae) { |
| 75 | + // GoogleAdsException is the base class for most exceptions thrown by an API request. |
| 76 | + // Instances of this exception have a message and a GoogleAdsFailure that contains a |
| 77 | + // collection of GoogleAdsErrors that indicate the underlying causes of the |
| 78 | + // GoogleAdsException. |
| 79 | + System.err.printf( |
| 80 | + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", |
| 81 | + gae.getRequestId()); |
| 82 | + int i = 0; |
| 83 | + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { |
| 84 | + System.err.printf(" Error %d: %s%n", i++, googleAdsError); |
| 85 | + } |
| 86 | + System.exit(1); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** Runs the example. */ |
| 91 | + private void runExample(GoogleAdsClient googleAdsClient, Long customerId) { |
| 92 | + makeServerStreamingCall(googleAdsClient, customerId); |
| 93 | + makeUnaryCall(googleAdsClient, customerId); |
| 94 | + } |
| 95 | + |
| 96 | + /** Makes a server streaming call using a custom client timeout. */ |
| 97 | + // [START SetCustomClientTimeouts] |
| 98 | + private void makeServerStreamingCall(GoogleAdsClient googleAdsClient, Long customerId) { |
| 99 | + StringBuilder output = new StringBuilder(); |
| 100 | + try (GoogleAdsServiceClient serviceClient = |
| 101 | + googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) { |
| 102 | + // Issues a search stream request by setting a custom client timeout |
| 103 | + ServerStream<SearchGoogleAdsStreamResponse> stream = |
| 104 | + serviceClient |
| 105 | + .searchStreamCallable() |
| 106 | + .call( |
| 107 | + SearchGoogleAdsStreamRequest.newBuilder() |
| 108 | + .setCustomerId(String.valueOf(customerId)) |
| 109 | + .setQuery("SELECT campaign.id FROM campaign") |
| 110 | + .build(), |
| 111 | + // Sets the timeout to use. |
| 112 | + GrpcCallContext.createDefault() |
| 113 | + .withTimeout(Duration.ofMillis(CLIENT_TIMEOUT_MILLIS))); |
| 114 | + // Iterates over all rows in all messages and collects the campaign IDs. |
| 115 | + for (SearchGoogleAdsStreamResponse response : stream) { |
| 116 | + for (GoogleAdsRow googleAdsRow : response.getResultsList()) { |
| 117 | + output.append(" ").append(googleAdsRow.getCampaign().getId()); |
| 118 | + } |
| 119 | + } |
| 120 | + System.out.println("The server streaming call completed before the timeout"); |
| 121 | + } catch (GoogleAdsException ex) { |
| 122 | + if (ex.getStatusCode().getCode() == Code.DEADLINE_EXCEEDED) { |
| 123 | + System.out.println("The server streaming call did not complete before the timeout."); |
| 124 | + } else { |
| 125 | + // Bubbles up if the exception is not about timeout. |
| 126 | + throw ex; |
| 127 | + } |
| 128 | + } finally { |
| 129 | + System.out.printf("All campaign IDs retrieved: %s.%n", output.toString()); |
| 130 | + } |
| 131 | + } |
| 132 | + // [END SetCustomClientTimeouts] |
| 133 | + |
| 134 | + /** Makes an unary call using a custom client timeout. */ |
| 135 | + // [START SetCustomClientTimeouts_1] |
| 136 | + private void makeUnaryCall(GoogleAdsClient googleAdsClient, Long customerId) { |
| 137 | + StringBuilder output = new StringBuilder(); |
| 138 | + try (GoogleAdsServiceClient serviceClient = |
| 139 | + googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) { |
| 140 | + // Issues a search paged request by setting a custom client timeout |
| 141 | + SearchPagedResponse pagedResponse = |
| 142 | + serviceClient |
| 143 | + .searchPagedCallable() |
| 144 | + .call( |
| 145 | + SearchGoogleAdsRequest.newBuilder() |
| 146 | + .setCustomerId(String.valueOf(customerId)) |
| 147 | + .setQuery("SELECT campaign.id FROM campaign") |
| 148 | + .build(), |
| 149 | + // Sets the timeout to use. |
| 150 | + GrpcCallContext.createDefault() |
| 151 | + .withTimeout(Duration.ofMillis(CLIENT_TIMEOUT_MILLIS))); |
| 152 | + // Iterates over all rows in all messages and collects the campaign IDs. |
| 153 | + for (GoogleAdsRow row : pagedResponse.iterateAll()) { |
| 154 | + output.append(" ").append(row.getCampaign().getId()); |
| 155 | + } |
| 156 | + System.out.println("The search paged call completed before the timeout"); |
| 157 | + } catch (GoogleAdsException ex) { |
| 158 | + if (ex.getStatusCode().getCode() == Code.DEADLINE_EXCEEDED) { |
| 159 | + System.out.println("The search paged call did not complete before the timeout."); |
| 160 | + } else { |
| 161 | + // Bubbles up if the exception is not about timeout. |
| 162 | + throw ex; |
| 163 | + } |
| 164 | + } finally { |
| 165 | + System.out.printf("All campaign IDs retrieved: %s.%n", output.toString()); |
| 166 | + } |
| 167 | + } |
| 168 | + // [END SetCustomClientTimeouts_1] |
| 169 | +} |
0 commit comments