Skip to content

Commit

Permalink
samples: fixed flaky listmodel by increasing the timeout (#286)
Browse files Browse the repository at this point in the history
* samples: fixed flaky listmodel by increasing the timeout

* fixed the lint issue
  • Loading branch information
munkhuushmgl authored and Shabirmean committed Nov 18, 2022
1 parent 1b76b76 commit bd5241a
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions automl/snippets/src/main/java/com/example/automl/ListModels.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

// [START automl_list_models]
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.AutoMlSettings;
import com.google.cloud.automl.v1.ListModelsRequest;
import com.google.cloud.automl.v1.LocationName;
import com.google.cloud.automl.v1.Model;
import java.io.IOException;
import org.threeten.bp.Duration;

class ListModels {

Expand All @@ -36,20 +38,33 @@ static void listModels(String projectId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
AutoMlSettings.Builder autoMlSettingsBuilder = AutoMlSettings.newBuilder();

autoMlSettingsBuilder
.listModelsSettings()
.setRetrySettings(
autoMlSettingsBuilder
.listModelsSettings()
.getRetrySettings()
.toBuilder()
.setTotalTimeout(Duration.ofSeconds(20))
.build());
AutoMlSettings autoMlSettings = autoMlSettingsBuilder.build();

try (AutoMlClient client = AutoMlClient.create(autoMlSettings)) {
// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, "us-central1");

// Create list models request.
ListModelsRequest listModlesRequest =
ListModelsRequest listModelsRequest =
ListModelsRequest.newBuilder()
.setParent(projectLocation.toString())
.setFilter("")
.build();

// List all the models available in the region by applying filter.
System.out.println("List of models:");
for (Model model : client.listModels(listModlesRequest).iterateAll()) {
for (Model model : client.listModels(listModelsRequest).iterateAll()) {
// Display the model information.
System.out.format("Model name: %s\n", model.getName());
// To get the model id, you have to parse it out of the `name` field. As models Ids are
Expand Down

0 comments on commit bd5241a

Please sign in to comment.