Skip to content

Commit e12e2d4

Browse files
aribraygcf-owl-bot[bot]
authored andcommitted
docs(samples): add try blocks to clean up resources in samples and sample tests (#521)
* add try block to clean up resources Change-Id: Ie68f4715646942d2e78189b3477cb9a5a31713b8 * docs: add try block to clean up resources Change-Id: Ibaa2bd6a9f687b67af61a5f958c37e2faf087434 * add try block to clean up resources Change-Id: I60fff4d6c83d42b8921b4ed4ce6c0eea76a952c6 * add try block to clean up resources Change-Id: I2facb88a81c1d76d5b2316da04cebdffba6634b9 * add Thread.sleep to try blocks, fix checkstyle Change-Id: I0eab8286ac21a8becf64b541100050581016ca5d * add Thread.sleep Change-Id: I94553a4501a6bdfbdcad1360833cd5e4352d9a77 * add Thread.sleep Change-Id: I123c577193306dc4625d2d56637e71e13cd06f70 * add try block to clean up resources Change-Id: I38d51da44e19b7c50d09d3e32aa6b5378c8fc0e2 * change fulfillmentInfo tag Change-Id: I8f0b7e31a2283c5dae7c6b377861ee03e8cbe621 * add try block to clean up resources Change-Id: I3ee67b4c3ce45b18138d9382fd18f5f4646372db * add ; Change-Id: I10cb37ff93f4c28c1f94d8e221c893fe4b74835a * remove try block Change-Id: I9b5f96d7c0ee75745084ed2a277fb9dc8b76a98f * move try block Change-Id: I17eeaf75673cb24682552dd59916c0ad5d68b89a * remove try block Change-Id: I001e1bd68b5c0953d2c634239828d81bf2576a25 * change fulfillmentInfo tag Change-Id: I1454736df9212b36afcd322ef3ce8c677b3500e8 * checkstyle Change-Id: I10d3750668a4acb9ff18f58fdb4f1a19faf6eacc * add try block to clean up resources Change-Id: I2a56b4f9b558fbebb816fb409fdabff4ee194f2f * add catch Change-Id: I7dd46f9543256427e9d86ab4f60b4806bdb8bd9a * remove println Change-Id: Idb0f4d75d0e7902a666248c48ed4fc54aeef5e59 * checkstyle Change-Id: Ie28023c2bf6422a50dc5e09128ede1e49679c95d * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * add try-with-resources comment to client object creation lines in snippets Change-Id: Ic1acaa6097e972b2db35cca2f5928e826ef08964 * add string.format Change-Id: I25e7e803c8d0b40e6503b6ed8c5fd1c872c4506b * update parentPath Change-Id: Id387695b0d86c30c74f796e7f57db6e1f8be43bd * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 07b8703 commit e12e2d4

29 files changed

+157
-78
lines changed

dialogflow-cx/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ public void service(HttpRequest request, HttpResponse response) throws Exception
5757
Gson gson = new GsonBuilder().setPrettyPrinting().create();
5858
String jsonResponseObject = gson.toJson(webhookResponse);
5959

60-
System.out.println("Response Object: \n");
61-
System.out.println(jsonResponseObject.toString());
62-
6360
/* {
6461
* "page_info": {
6562
* "form_info": {

dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateAgent.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public static Agent createAgent(String parent, String displayName) throws IOExce
3939
String apiEndpoint = "global-dialogflow.googleapis.com:443";
4040

4141
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
42+
// Note: close() needs to be called on the AgentsClient object to clean up resources
43+
// such as threads. In the example below, try-with-resources is used,
44+
// which automatically calls close().
4245
try (AgentsClient client = AgentsClient.create(agentsSettings)) {
4346
// Set the details of the Agent to create
4447
Builder build = Agent.newBuilder();

dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateFlow.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ public static Flow createFlow(
5050
}
5151
FlowsSettings flowsSettings = flowsSettingsBuilder.build();
5252

53-
// Instantiates a client
53+
// Instantiates a client.
54+
// Note: close() needs to be called on the FlowsClient object to clean up resources
55+
// such as threads. In the example below, try-with-resources is used,
56+
// which automatically calls close().
5457
try (FlowsClient flowsClient = FlowsClient.create(flowsSettings)) {
5558
// Set the project agent name using the projectID (my-project-id), locationID (global), and
5659
// agentID (UUID).

dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateIntent.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public static Intent createIntent(
4848
IntentsSettings intentsSettings = intentsSettingsBuilder.build();
4949

5050
// Instantiates a client
51+
// Note: close() needs to be called on the IntentsClient object to clean up resources
52+
// such as threads. In the example below, try-with-resources is used,
53+
// which automatically calls close().
5154
try (IntentsClient intentsClient = IntentsClient.create(intentsSettings)) {
5255
// Set the project agent name using the projectID (my-project-id), locationID (global), and
5356
// agentID (UUID).

dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreatePage.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public static Page createPage(
5252
PagesSettings pagesSettings = pagesSettingsBuilder.build();
5353

5454
// Instantiates a client
55+
// Note: close() needs to be called on the PagesClient object to clean up resources
56+
// such as threads. In the example below, try-with-resources is used,
57+
// which automatically calls close().
5558
try (PagesClient pagesClient = PagesClient.create(pagesSettings)) {
5659
// Set the flow name using the projectID (my-project-id), locationID (global), agentID (UUID)
5760
// and flowID (UUID).

dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateSimplePage.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public static Page createPage(
5959
.setPage(pageBuilder);
6060

6161
// Make API request to create page
62+
// Note: close() needs to be called on the PagesClient object to clean up resources
63+
// such as threads. In the example below, try-with-resources is used,
64+
// which automatically calls close().
6265
try (PagesClient client = PagesClient.create()) {
6366
response = client.createPage(createRequestBuilder.build());
6467
System.out.println("Successfully created page!");

dialogflow-cx/snippets/src/main/java/dialogflow/cx/DeletePage.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public static void main(String[] args) throws IOException {
4040
public static void deletePage(
4141
String projectId, String agentId, String flowId, String pageId, String location)
4242
throws IOException {
43+
44+
// Note: close() needs to be called on the PagesClient object to clean up resources
45+
// such as threads. In the example below, try-with-resources is used,
46+
// which automatically calls close().
4347
try (PagesClient client = PagesClient.create()) {
4448
Builder deleteRequestBuilder = DeletePageRequest.newBuilder();
4549

dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntent.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ public static Map<String, QueryResult> detectIntent(
5252
SessionsSettings sessionsSettings = sessionsSettingsBuilder.build();
5353

5454
Map<String, QueryResult> queryResults = Maps.newHashMap();
55-
// Instantiates a client
55+
// Instantiates a client.
56+
57+
// Note: close() needs to be called on the SessionsClient object to clean up resources
58+
// such as threads. In the example below, try-with-resources is used,
59+
// which automatically calls close().
5660
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
5761
// Set the session name using the projectID (my-project-id), locationID (global), agentID
5862
// (UUID), and sessionId (UUID).

dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentAudioInput.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public static void detectIntent(
7676

7777
// Instantiates a client by setting the session name.
7878
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
79+
80+
// Note: close() needs to be called on the SessionsClient object to clean up resources
81+
// such as threads. In the example below, try-with-resources is used,
82+
// which automatically calls close().
7983
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
8084
SessionName session =
8185
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);

dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentDisableWebhook.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public static Map<String, QueryResult> detectIntent(
6868

6969
// Instantiates a client by setting the session name.
7070
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
71+
72+
// Note: close() needs to be called on the SessionsClient object to clean up resources
73+
// such as threads. In the example below, try-with-resources is used,
74+
// which automatically calls close().
7175
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
7276
SessionName session =
7377
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);

dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentEventInput.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public static void detectIntent(
6262

6363
// Instantiates a client by setting the session name.
6464
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
65+
66+
// Note: close() needs to be called on the SessionsClient object to clean up resources
67+
// such as threads. In the example below, try-with-resources is used,
68+
// which automatically calls close().
6569
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
6670
SessionName session =
6771
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);

dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentIntentInput.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public static void detectIntent(
6262

6363
// Instantiates a client by setting the session name.
6464
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
65+
66+
// Note: close() needs to be called on the SessionsClient object to clean up resources
67+
// such as threads. In the example below, try-with-resources is used,
68+
// which automatically calls close().
6569
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
6670
SessionName session =
6771
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);

dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentSentimentAnalysis.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public static Map<String, QueryResult> detectIntent(
6868

6969
// Instantiates a client by setting the session name.
7070
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
71+
72+
// Note: close() needs to be called on the SessionsClient object to clean up resources
73+
// such as threads. In the example below, try-with-resources is used,
74+
// which automatically calls close().
7175
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
7276
SessionName session =
7377
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);

dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentStream.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public static void detectIntentStream(
5656
// Instantiates a client by setting the session name.
5757
// Format: `projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
5858
// Using the same `sessionId` between requests allows continuation of the conversation.
59+
60+
// Note: close() needs to be called on the SessionsClient object to clean up resources
61+
// such as threads. In the example below, try-with-resources is used,
62+
// which automatically calls close().
5963
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
6064
SessionName session = SessionName.of(projectId, locationId, agentId, sessionId);
6165

dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentStreamingPartialResponse.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public static void detectIntentStreamingPartialResponse(
5656
// Instantiates a client by setting the session name.
5757
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
5858
// Using the same `sessionId` between requests allows continuation of the conversation.
59+
60+
// Note: close() needs to be called on the SessionsClient object to clean up resources
61+
// such as threads. In the example below, try-with-resources is used,
62+
// which automatically calls close().
5963
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
6064
SessionName session = SessionName.of(projectId, locationId, agentId, sessionId);
6165

dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntentSynthesizeTextToSpeechOutput.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public static void detectIntent(
7171

7272
// Instantiates a client by setting the session name.
7373
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
74+
75+
// Note: close() needs to be called on the SessionsClient object to clean up resources
76+
// such as threads. In the example below, try-with-resources is used,
77+
// which automatically calls close().
7478
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
7579
SessionName session =
7680
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);

dialogflow-cx/snippets/src/main/java/dialogflow/cx/ExportAgent.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public static void exportAgent(String projectId, String agentId, String location
4747
String apiEndpoint = String.format("%s-dialogflow.googleapis.com:443", location);
4848

4949
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
50+
// Note: close() needs to be called on the AgentsClient object to clean up resources
51+
// such as threads. In the example below, try-with-resources is used,
52+
// which automatically calls close().
5053
try (AgentsClient agentsClient = AgentsClient.create(agentsSettings)) {
5154
ExportAgentRequest request =
5255
ExportAgentRequest.newBuilder()

dialogflow-cx/snippets/src/main/java/dialogflow/cx/ListPages.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,22 @@ public static void main(String[] args) throws IOException {
3939
// Lists all pages from the provided parameters
4040
public static void listPages(String projectId, String agentId, String flowId, String location)
4141
throws IOException {
42-
PagesClient client = PagesClient.create();
43-
Builder listRequestBuilder = ListPagesRequest.newBuilder();
44-
45-
listRequestBuilder.setParent(
46-
"projects/"
47-
+ projectId
48-
+ "/locations/"
49-
+ location
50-
+ "/agents/"
51-
+ agentId
52-
+ "/flows/"
53-
+ flowId);
54-
listRequestBuilder.setLanguageCode("en");
55-
56-
// Make API request to list all pages in the project
57-
for (Page element : client.listPages(listRequestBuilder.build()).iterateAll()) {
58-
System.out.println(element);
42+
// Note: close() needs to be called on the PagesClient object to clean up resources
43+
// such as threads. In the example below, try-with-resources is used,
44+
// which automatically calls close().
45+
try (PagesClient client = PagesClient.create()) {
46+
Builder listRequestBuilder = ListPagesRequest.newBuilder();
47+
48+
String parentPath =
49+
String.format(
50+
"projects/%s/locations/%s/agents/%s/flows/%s", projectId, location, agentId, flowId);
51+
listRequestBuilder.setParent(parentPath);
52+
listRequestBuilder.setLanguageCode("en");
53+
54+
// Make API request to list all pages in the project
55+
for (Page element : client.listPages(listRequestBuilder.build()).iterateAll()) {
56+
System.out.println(element);
57+
}
5958
}
6059
}
6160
// [END dialogflow_cx_list_pages]

dialogflow-cx/snippets/src/main/java/dialogflow/cx/ListTestCaseResults.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ public static void listTestCaseResults(
5757
TestCasesSettings.newBuilder()
5858
.setEndpoint(location + "-dialogflow.googleapis.com:443")
5959
.build();
60-
TestCasesClient client = TestCasesClient.create(testCasesSettings);
6160

62-
for (TestCaseResult element : client.listTestCaseResults(req.build()).iterateAll()) {
63-
System.out.println(element);
61+
// Note: close() needs to be called on the TestCasesClient object to clean up resources
62+
// such as threads. In the example below, try-with-resources is used,
63+
// which automatically calls close().
64+
try (TestCasesClient client = TestCasesClient.create(testCasesSettings)) {
65+
for (TestCaseResult element : client.listTestCaseResults(req.build()).iterateAll()) {
66+
System.out.println(element);
67+
}
6468
}
6569
}
6670
}

dialogflow-cx/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public static void main(String[] args) throws IOException {
3838
// DialogFlow API List Training Phrases sample.
3939
public static void listTrainingPhrases(
4040
String projectId, String location, String agentId, String intentId) throws IOException {
41+
42+
// Note: close() needs to be called on the IntentsClient object to clean up resources
43+
// such as threads. In the example below, try-with-resources is used,
44+
// which automatically calls close().
4145
try (IntentsClient client = IntentsClient.create()) {
4246
// Set the intent name
4347
IntentName name = IntentName.of(projectId, location, agentId, intentId);

dialogflow-cx/snippets/src/main/java/dialogflow/cx/UpdateIntent.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public static void main(String[] args) throws IOException {
4141
public static void updateIntent(
4242
String projectId, String agentId, String intentId, String location, String displayName)
4343
throws IOException {
44+
45+
// Note: close() needs to be called on the IntentsClient object to clean up resources
46+
// such as threads. In the example below, try-with-resources is used,
47+
// which automatically calls close().
4448
try (IntentsClient client = IntentsClient.create()) {
4549
String intentPath =
4650
"projects/"

dialogflow-cx/snippets/src/main/java/dialogflow/cx/WebhookConfigureSessionParameters.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ public void service(HttpRequest request, HttpResponse response) throws Exception
4949
Gson gson = new GsonBuilder().setPrettyPrinting().create();
5050
String jsonResponseObject = gson.toJson(webhookResponse);
5151

52-
System.out.println("Session Parameter Info: \n");
53-
System.out.println(jsonResponseObject.toString());
54-
5552
/** { "session_info": { "parameters": { "order_number": "12345" } } } */
5653
BufferedWriter writer = response.getWriter();
5754
// Sends the webhookResponseObject

dialogflow-cx/snippets/src/main/java/dialogflow/cx/WebhookValidateFormParameter.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ public void service(HttpRequest request, HttpResponse response) throws Exception
6565
Gson gson = new GsonBuilder().setPrettyPrinting().create();
6666
String jsonResponseObject = gson.toJson(webhookResponse);
6767

68-
System.out.println("Response Object: \n");
69-
System.out.println(jsonResponseObject.toString());
7068
/**
7169
* { "page_info": { "form_info": { "parameter_info": [ { "display_name": "order_number",
7270
* "required": "true", "state": "INVALID", "value": "123" } ] } }, "session_info": {

dialogflow-cx/snippets/src/test/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequiredIT.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ public void beforeTest() throws IOException {
6565
public void helloHttp_bodyParamsPost() throws IOException, Exception {
6666

6767
FulfillmentInfo fulfillmentInfo =
68-
FulfillmentInfo.newBuilder().setTag("configure-session-parameters").build();
68+
FulfillmentInfo.newBuilder()
69+
.setTag("configure-form-parameters-optional-or-parameter")
70+
.build();
6971

7072
WebhookRequest webhookRequest =
7173
WebhookRequest.newBuilder().setFulfillmentInfo(fulfillmentInfo).build();
@@ -94,5 +96,6 @@ public void helloHttp_bodyParamsPost() throws IOException, Exception {
9496
String expectedResponse = gson.toJson(webhookResponse);
9597

9698
assertThat(responseOut.toString()).isEqualTo(expectedResponse);
99+
Thread.sleep(200);
97100
}
98101
}

dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateAgentIT.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public void tearDown() throws IOException, InterruptedException {
4848
String apiEndpoint = "global-dialogflow.googleapis.com:443";
4949

5050
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
51-
AgentsClient client = AgentsClient.create(agentsSettings);
51+
try (AgentsClient client = AgentsClient.create(agentsSettings)) {
52+
client.deleteAgent(CreateAgentIT.agentPath);
5253

53-
client.deleteAgent(CreateAgentIT.agentPath);
54-
55-
// Small delay to prevent reaching quota limit of requests per minute
56-
Thread.sleep(250);
54+
// Small delay to prevent reaching quota limit of requests per minute
55+
Thread.sleep(250);
56+
}
5757
}
5858

5959
@Test

0 commit comments

Comments
 (0)