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 new file mode 100644 index 000000000..a9459e804 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datastore/ConfigureConnectionPool.java @@ -0,0 +1,61 @@ +/* + * 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] 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..40b22c89d --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datastore/ConfigureConnectionPoolIT.java @@ -0,0 +1,39 @@ +/* + * 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. + * 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"); + } +}