Skip to content

Instantly share code, notes, and snippets.

@DKbyo
Last active May 18, 2022 19:27
Show Gist options
  • Select an option

  • Save DKbyo/9ad5bdba572e00f0223cfc5061e60fb8 to your computer and use it in GitHub Desktop.

Select an option

Save DKbyo/9ad5bdba572e00f0223cfc5061e60fb8 to your computer and use it in GitHub Desktop.
google-java-sdk-retries
package com.example.app;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.compute.Compute;
import com.google.api.services.compute.model.Instance;
import com.google.api.services.compute.model.InstanceList;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
public class App{
public static void main(String args[]) throws IOException, GeneralSecurityException {
// Project ID for this request.
String project = "my-project"; // TODO: Update placeholder value.
// The name of the zone for this request.
String zone = "my-zone"; // TODO: Update placeholder value.
Compute computeService = createComputeService();
Compute.Instances.List request = computeService.instances().list(project, zone);
InstanceList response;
do {
response = request.execute();
if (response.getItems() == null) {
continue;
}
for (Instance instance : response.getItems()) {
// TODO: Change code below to process each `instance` resource:
System.out.println(instance);
}
request.setPageToken(response.getNextPageToken());
} while (response.getNextPageToken() != null);
}
public static Compute createComputeService() throws IOException, GeneralSecurityException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = GoogleCredential.getApplicationDefault();
if (credential.createScopedRequired()) {
credential =
credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
}
final GoogleCredential finalCredential = credential;
return new Compute.Builder(httpTransport, jsonFactory, new com.google.api.client.http.HttpRequestInitializer(){
@Override
public void initialize(HttpRequest request) throws IOException {
finalCredential.intercept(request);
//Set number of retries
request.setNumberOfRetries(0);
//If status 403 or 423 don't retry https://cloud.google.com/docs/quota#quota_errors
request.setUnsuccessfulResponseHandler( new HttpUnsuccessfulResponseHandler() {
@Override
public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry)
throws IOException {
return !(response.getStatusCode() == 413 || response.getStatusCode() == 429);
}
});
}
})
.setApplicationName("Google-ComputeSample/0.1")
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment