Skip to content

Commit e6b49e3

Browse files
committed
docs: Sample for configuring connection pool
1 parent 8c16fa4 commit e6b49e3

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable;
18+
19+
// [START bigtable_configure_connection_pool]
20+
21+
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
22+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
23+
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
24+
import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings;
25+
26+
public class ConfigureConnectionPool {
27+
28+
public static void configureConnectionPool(String projectId, String instanceId) {
29+
// String projectId = "my-project-id";
30+
// String instanceId = "my-instance-id";
31+
32+
BigtableDataSettings.Builder settingsBuilder = BigtableDataSettings.newBuilder()
33+
.setProjectId(projectId)
34+
.setInstanceId(instanceId);
35+
36+
settingsBuilder.stubSettings()
37+
.setTransportChannelProvider(
38+
EnhancedBigtableStubSettings.defaultGrpcTransportProviderBuilder()
39+
.setPoolSize(250)
40+
.build());
41+
42+
BigtableDataSettings settings = settingsBuilder.build();
43+
try (BigtableDataClient dataClient = BigtableDataClient.create(settings)) {
44+
InstantiatingGrpcChannelProvider provider =
45+
(InstantiatingGrpcChannelProvider) settings.getStubSettings()
46+
.getTransportChannelProvider();
47+
48+
int poolSize = provider.toBuilder().getPoolSize();
49+
50+
System.out.println(String.format("Connected with pool size of %d", poolSize));
51+
} catch (Exception e) {
52+
System.out.println("Error during ConfigureConnectionPool: \n" + e.toString());
53+
}
54+
}
55+
}
56+
57+
// [END bigtable_configure_connection_pool]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigtable;
18+
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertThat;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import org.hamcrest.CoreMatchers;
25+
import org.junit.Before;
26+
import org.junit.BeforeClass;
27+
import org.junit.FixMethodOrder;
28+
import org.junit.Test;
29+
import org.junit.runners.MethodSorters;
30+
31+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
32+
public class ConfigureConnectionPoolTest {
33+
34+
private static final String INSTANCE_ENV = "BIGTABLE_TESTING_INSTANCE";
35+
private static String projectId;
36+
private static String instanceId;
37+
private ByteArrayOutputStream bout;
38+
39+
private static String requireEnv(String varName) {
40+
String value = System.getenv(varName);
41+
assertNotNull(
42+
String.format("Environment variable '%s' is required to perform these tests.", varName),
43+
value);
44+
return value;
45+
}
46+
47+
@BeforeClass
48+
public static void beforeClass() {
49+
projectId = requireEnv("GOOGLE_CLOUD_PROJECT");
50+
instanceId = requireEnv(INSTANCE_ENV);
51+
}
52+
53+
@Before
54+
public void setupStream() {
55+
bout = new ByteArrayOutputStream();
56+
System.setOut(new PrintStream(bout));
57+
}
58+
59+
@Test
60+
public void testConfigureConnectionPool() {
61+
ConfigureConnectionPool.configureConnectionPool(projectId, instanceId);
62+
63+
String output = bout.toString();
64+
assertThat(output, CoreMatchers.containsString("Connected with pool size of 250"));
65+
}
66+
}

0 commit comments

Comments
 (0)