Skip to content

Commit 2716f2d

Browse files
authored
samples: fixed flaky listmodel by increasing the timeout (#286)
* samples: fixed flaky listmodel by increasing the timeout * fixed the lint issue
1 parent 3fc0c63 commit 2716f2d

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

automl/snippets/src/main/java/com/example/automl/ListModels.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
// [START automl_list_models]
2020
import com.google.cloud.automl.v1.AutoMlClient;
21+
import com.google.cloud.automl.v1.AutoMlSettings;
2122
import com.google.cloud.automl.v1.ListModelsRequest;
2223
import com.google.cloud.automl.v1.LocationName;
2324
import com.google.cloud.automl.v1.Model;
2425
import java.io.IOException;
26+
import org.threeten.bp.Duration;
2527

2628
class ListModels {
2729

@@ -36,20 +38,33 @@ static void listModels(String projectId) throws IOException {
3638
// Initialize client that will be used to send requests. This client only needs to be created
3739
// once, and can be reused for multiple requests. After completing all of your requests, call
3840
// the "close" method on the client to safely clean up any remaining background resources.
39-
try (AutoMlClient client = AutoMlClient.create()) {
41+
AutoMlSettings.Builder autoMlSettingsBuilder = AutoMlSettings.newBuilder();
42+
43+
autoMlSettingsBuilder
44+
.listModelsSettings()
45+
.setRetrySettings(
46+
autoMlSettingsBuilder
47+
.listModelsSettings()
48+
.getRetrySettings()
49+
.toBuilder()
50+
.setTotalTimeout(Duration.ofSeconds(20))
51+
.build());
52+
AutoMlSettings autoMlSettings = autoMlSettingsBuilder.build();
53+
54+
try (AutoMlClient client = AutoMlClient.create(autoMlSettings)) {
4055
// A resource that represents Google Cloud Platform location.
4156
LocationName projectLocation = LocationName.of(projectId, "us-central1");
4257

4358
// Create list models request.
44-
ListModelsRequest listModlesRequest =
59+
ListModelsRequest listModelsRequest =
4560
ListModelsRequest.newBuilder()
4661
.setParent(projectLocation.toString())
4762
.setFilter("")
4863
.build();
4964

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

0 commit comments

Comments
 (0)