Skip to content

Commit a8215d8

Browse files
committed
made client autoclosable to free resources
1 parent 03fcfb1 commit a8215d8

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
import java.io.ByteArrayInputStream;
4343
import java.io.ByteArrayOutputStream;
44+
import java.io.Closeable;
4445
import java.io.IOException;
4546
import java.io.InputStream;
4647
import java.lang.reflect.InvocationTargetException;
@@ -95,7 +96,7 @@
9596
* <p>Client is thread-safe. It uses exclusive set of object to perform an operation.</p>
9697
*
9798
*/
98-
public class Client {
99+
public class Client implements AutoCloseable {
99100

100101
private Set<String> endpoints;
101102
private Map<String, String> configuration;
@@ -130,6 +131,24 @@ public String getDefaultDatabase() {
130131
return this.configuration.get("database");
131132
}
132133

134+
135+
/**
136+
* Frees the resources associated with the client.
137+
* <ul>
138+
* <li>Shuts down the shared operation executor by calling {@code shutdownNow()}</li>
139+
* </ul>
140+
*/
141+
@Override
142+
public void close() {
143+
try {
144+
if (!sharedOperationExecutor.isShutdown()) {
145+
this.sharedOperationExecutor.shutdownNow();
146+
}
147+
} catch (Exception e) {
148+
LOG.error("Failed to close shared operation executor", e);
149+
}
150+
}
151+
133152
public static class Builder {
134153
private Set<String> endpoints;
135154

client-v2/src/test/java/com/clickhouse/client/ClientTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public void testAddSecureEndpoint(Client client) {
3737
return;
3838
}
3939
Assert.fail(e.getMessage());
40+
} finally {
41+
client.close();
4042
}
4143
}
4244

@@ -87,6 +89,8 @@ public void testRawSettings() {
8789
} catch (Exception e) {
8890
e.printStackTrace();
8991
Assert.fail(e.getMessage());
92+
} finally {
93+
client.close();
9094
}
9195
}
9296
}

client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
import com.clickhouse.client.api.enums.Protocol;
1212
import com.clickhouse.client.api.insert.InsertResponse;
1313
import com.clickhouse.client.api.insert.InsertSettings;
14-
import com.clickhouse.client.api.metadata.TableSchema;
1514
import com.clickhouse.client.api.metrics.ClientMetrics;
1615
import com.clickhouse.client.api.metrics.OperationMetrics;
1716
import com.clickhouse.client.api.metrics.ServerMetrics;
1817
import org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils;
18+
import org.testng.annotations.AfterMethod;
1919
import org.testng.annotations.BeforeMethod;
2020
import org.testng.annotations.Test;
2121

@@ -32,7 +32,7 @@ public class InsertTests extends BaseIntegrationTest {
3232
private Client client;
3333
private InsertSettings settings;
3434

35-
@BeforeMethod(groups = { "integration" }, enabled = true)
35+
@BeforeMethod(groups = { "integration" })
3636
public void setUp() throws IOException {
3737
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP);
3838
client = new Client.Builder()
@@ -45,6 +45,11 @@ public void setUp() throws IOException {
4545
.setQueryId(String.valueOf(UUID.randomUUID()));
4646
}
4747

48+
@AfterMethod(groups = { "integration" })
49+
public void tearDown() {
50+
client.close();
51+
}
52+
4853
private void createTable(String tableQuery) throws ClickHouseException {
4954
try (ClickHouseClient client = ClickHouseClient.builder().config(new ClickHouseConfig())
5055
.nodeSelector(ClickHouseNodeSelector.of(ClickHouseProtocol.HTTP))

client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.fasterxml.jackson.databind.JsonNode;
3333
import com.fasterxml.jackson.databind.ObjectMapper;
3434
import org.testng.Assert;
35+
import org.testng.annotations.AfterMethod;
3536
import org.testng.annotations.BeforeMethod;
3637
import org.testng.annotations.DataProvider;
3738
import org.testng.annotations.Test;
@@ -83,6 +84,11 @@ public void setUp() {
8384
System.out.println("Real port: " + node.getPort());
8485
}
8586

87+
@AfterMethod(groups = {"integration"})
88+
public void tearDown() {
89+
client.close();
90+
}
91+
8692
private static void delayForProfiler(long millis) {
8793
try {
8894
Thread.sleep(millis);

0 commit comments

Comments
 (0)