From fd479168c0a8b7ba72814fbef384ff8f9e0bfeae Mon Sep 17 00:00:00 2001 From: Cindy Peng <148148319+cindy-peng@users.noreply.github.com> Date: Wed, 20 Aug 2025 15:59:57 -0700 Subject: [PATCH 1/3] feat: Add sample code for connection pooling --- .../datastore/ConfigureConnectionPool.java | 59 +++++++++++++++++++ .../datastore/ConfigureConnectionPoolIT.java | 41 +++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/datastore/ConfigureConnectionPool.java create mode 100644 samples/snippets/src/test/java/com/example/datastore/ConfigureConnectionPoolIT.java diff --git a/samples/snippets/src/main/java/com/example/datastore/ConfigureConnectionPool.java b/samples/snippets/src/main/java/com/example/datastore/ConfigureConnectionPool.java new file mode 100644 index 000000000..5b7a4a427 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datastore/ConfigureConnectionPool.java @@ -0,0 +1,59 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datastore; + +// [START datastore_configure_connection_pool] + +import com.google.api.gax.grpc.ChannelPoolSettings; +import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; +import com.google.cloud.datastore.DatastoreOptions; +import com.google.cloud.datastore.v1.DatastoreSettings; +import com.google.cloud.grpc.GrpcTransportOptions; + + +public class ConfigureConnectionPool { + + public static void main(String... args) throws Exception { + InstantiatingGrpcChannelProvider channelProvider = + DatastoreSettings.defaultGrpcTransportProviderBuilder() + .setChannelPoolSettings( + ChannelPoolSettings.builder() + .setInitialChannelCount(10) + .setMinChannelCount(5) + .setMaxChannelCount(200) + .build()) + .build(); + + DatastoreOptions datastoreOptions = DatastoreOptions.newBuilder() + .setProjectId("my-project") + .setChannelProvider(channelProvider) + .setTransportOptions(GrpcTransportOptions.newBuilder().build()) + .build(); + + ChannelPoolSettings channelPoolSettings = + ((InstantiatingGrpcChannelProvider) datastoreOptions.getTransportChannelProvider()) + .getChannelPoolSettings(); + + System.out.printf(String.format( + "Connected with pool with InitialChannelCount: %d, MinChannelCount: %d, MaxChannelCount: %d", + channelPoolSettings.getInitialChannelCount(), channelPoolSettings.getMinChannelCount(), + channelPoolSettings.getMaxChannelCount() + )); + } +} + +// [END datastore_configure_connection_pool] \ No newline at end of file diff --git a/samples/snippets/src/test/java/com/example/datastore/ConfigureConnectionPoolIT.java b/samples/snippets/src/test/java/com/example/datastore/ConfigureConnectionPoolIT.java new file mode 100644 index 000000000..8ed591dad --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datastore/ConfigureConnectionPoolIT.java @@ -0,0 +1,41 @@ +/* + * Copyright 2016 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datastore; + +import com.rule.SystemsOutRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for ConfigureConnectionPool sample. + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ConfigureConnectionPoolIT { + + @Rule + public final SystemsOutRule systemsOutRule = new SystemsOutRule(); + + @Test + public void testConfigureConnectionPool() throws Exception { + ConfigureConnectionPool.main(); + systemsOutRule.assertContains( + "Connected with pool with InitialChannelCount: 10, MinChannelCount: 5, MaxChannelCount: 200"); + } +} From 8580fe1ae64bc330bbba67d14b04fce43c4ff5fa Mon Sep 17 00:00:00 2001 From: Cindy Peng <148148319+cindy-peng@users.noreply.github.com> Date: Wed, 20 Aug 2025 16:02:32 -0700 Subject: [PATCH 2/3] Fix header --- .../java/com/example/datastore/ConfigureConnectionPoolIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/src/test/java/com/example/datastore/ConfigureConnectionPoolIT.java b/samples/snippets/src/test/java/com/example/datastore/ConfigureConnectionPoolIT.java index 8ed591dad..ffbe27590 100644 --- a/samples/snippets/src/test/java/com/example/datastore/ConfigureConnectionPoolIT.java +++ b/samples/snippets/src/test/java/com/example/datastore/ConfigureConnectionPoolIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Google Inc. + * Copyright 2025 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 0313948c319f9b180b90418395a5a3686f1ebdd1 Mon Sep 17 00:00:00 2001 From: cloud-java-bot Date: Wed, 20 Aug 2025 23:04:57 +0000 Subject: [PATCH 3/3] chore: generate libraries at Wed Aug 20 23:03:06 UTC 2025 --- README.md | 1 + .../datastore/ConfigureConnectionPool.java | 26 ++++++++++--------- .../datastore/ConfigureConnectionPoolIT.java | 10 +++---- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 8bc95c0d1..8e900b995 100644 --- a/README.md +++ b/README.md @@ -363,6 +363,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-datastore/tre | Sample | Source Code | Try it | | --------------------------- | --------------------------------- | ------ | +| Configure Connection Pool | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/ConfigureConnectionPool.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/ConfigureConnectionPool.java) | | Quickstart Sample | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java) | | Avg Aggregation On Kind | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationOnKind.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationOnKind.java) | | Avg Aggregation With Limit | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithLimit.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithLimit.java) | diff --git a/samples/snippets/src/main/java/com/example/datastore/ConfigureConnectionPool.java b/samples/snippets/src/main/java/com/example/datastore/ConfigureConnectionPool.java index 5b7a4a427..a9459e804 100644 --- a/samples/snippets/src/main/java/com/example/datastore/ConfigureConnectionPool.java +++ b/samples/snippets/src/main/java/com/example/datastore/ConfigureConnectionPool.java @@ -24,7 +24,6 @@ import com.google.cloud.datastore.v1.DatastoreSettings; import com.google.cloud.grpc.GrpcTransportOptions; - public class ConfigureConnectionPool { public static void main(String... args) throws Exception { @@ -38,22 +37,25 @@ public static void main(String... args) throws Exception { .build()) .build(); - DatastoreOptions datastoreOptions = DatastoreOptions.newBuilder() - .setProjectId("my-project") - .setChannelProvider(channelProvider) - .setTransportOptions(GrpcTransportOptions.newBuilder().build()) - .build(); + DatastoreOptions datastoreOptions = + DatastoreOptions.newBuilder() + .setProjectId("my-project") + .setChannelProvider(channelProvider) + .setTransportOptions(GrpcTransportOptions.newBuilder().build()) + .build(); ChannelPoolSettings channelPoolSettings = ((InstantiatingGrpcChannelProvider) datastoreOptions.getTransportChannelProvider()) .getChannelPoolSettings(); - System.out.printf(String.format( - "Connected with pool with InitialChannelCount: %d, MinChannelCount: %d, MaxChannelCount: %d", - channelPoolSettings.getInitialChannelCount(), channelPoolSettings.getMinChannelCount(), - channelPoolSettings.getMaxChannelCount() - )); + System.out.printf( + String.format( + "Connected with pool with InitialChannelCount: %d, MinChannelCount: %d," + + " MaxChannelCount: %d", + channelPoolSettings.getInitialChannelCount(), + channelPoolSettings.getMinChannelCount(), + channelPoolSettings.getMaxChannelCount())); } } -// [END datastore_configure_connection_pool] \ No newline at end of file +// [END datastore_configure_connection_pool] diff --git a/samples/snippets/src/test/java/com/example/datastore/ConfigureConnectionPoolIT.java b/samples/snippets/src/test/java/com/example/datastore/ConfigureConnectionPoolIT.java index ffbe27590..40b22c89d 100644 --- a/samples/snippets/src/test/java/com/example/datastore/ConfigureConnectionPoolIT.java +++ b/samples/snippets/src/test/java/com/example/datastore/ConfigureConnectionPoolIT.java @@ -22,20 +22,18 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** - * Tests for ConfigureConnectionPool sample. - */ +/** Tests for ConfigureConnectionPool sample. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class ConfigureConnectionPoolIT { - @Rule - public final SystemsOutRule systemsOutRule = new SystemsOutRule(); + @Rule public final SystemsOutRule systemsOutRule = new SystemsOutRule(); @Test public void testConfigureConnectionPool() throws Exception { ConfigureConnectionPool.main(); systemsOutRule.assertContains( - "Connected with pool with InitialChannelCount: 10, MinChannelCount: 5, MaxChannelCount: 200"); + "Connected with pool with InitialChannelCount: 10, MinChannelCount: 5, MaxChannelCount:" + + " 200"); } }