Skip to content

Commit d8a52f8

Browse files
fix: fix WriteToDefaultStream example code to close the client properly (#2433)
* fix: fix channel not shut down properly exception. Client being created has to be properly closed, otherwise during garbage collection an error will be reported showing channel not shutdown properly * 🦉 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 f8535be commit d8a52f8

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

samples/snippets/src/main/java/com/example/bigquerystorage/WriteToDefaultStream.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ private static class DataWriter {
138138

139139
private static final int MAX_RECREATE_COUNT = 3;
140140

141+
private BigQueryWriteClient client;
142+
141143
// Track the number of in-flight requests to wait for all responses before shutting down.
142144
private final Phaser inflightRequestCount = new Phaser(1);
143145
private final Object lock = new Object();
@@ -163,12 +165,16 @@ public void initialize(TableName parentTable)
163165
.setMaxRetryDelay(Duration.ofMinutes(1))
164166
.build();
165167

168+
// Initialize client without settings, internally within stream writer a new client will be
169+
// created with full settings.
170+
client = BigQueryWriteClient.create();
171+
166172
// Use the JSON stream writer to send records in JSON format. Specify the table name to write
167173
// to the default stream.
168174
// For more information about JsonStreamWriter, see:
169175
// https://googleapis.dev/java/google-cloud-bigquerystorage/latest/com/google/cloud/bigquery/storage/v1/JsonStreamWriter.html
170176
streamWriter =
171-
JsonStreamWriter.newBuilder(parentTable.toString(), BigQueryWriteClient.create())
177+
JsonStreamWriter.newBuilder(parentTable.toString(), client)
172178
.setExecutorProvider(
173179
FixedExecutorProvider.create(Executors.newScheduledThreadPool(100)))
174180
.setChannelProvider(
@@ -193,10 +199,7 @@ public void append(AppendContext appendContext)
193199
if (!streamWriter.isUserClosed()
194200
&& streamWriter.isClosed()
195201
&& recreateCount.getAndIncrement() < MAX_RECREATE_COUNT) {
196-
streamWriter =
197-
JsonStreamWriter.newBuilder(
198-
streamWriter.getStreamName(), BigQueryWriteClient.create())
199-
.build();
202+
streamWriter = JsonStreamWriter.newBuilder(streamWriter.getStreamName(), client).build();
200203
this.error = null;
201204
}
202205
// If earlier appends have failed, we need to reset before continuing.
@@ -217,6 +220,7 @@ public void cleanup() {
217220
// Wait for all in-flight requests to complete.
218221
inflightRequestCount.arriveAndAwaitAdvance();
219222

223+
client.close();
220224
// Close the connection to the server.
221225
streamWriter.close();
222226

0 commit comments

Comments
 (0)