Skip to content

Commit e0e4a82

Browse files
authored
Make ApiClient and subclasses Closeable (#851) (#857)
1 parent f62cc7c commit e0e4a82

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

docs/api-conventions/object-lifecycles.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ closed to release the underlying resources such as network connections.
1717
**Clients**::
1818
Immutable, stateless and thread-safe.
1919
These are very lightweight objects that just wrap a transport and provide API
20-
endpoints as methods.
20+
endpoints as methods. Closing a client closes the underlying transport.
2121

2222
**Builders**::
2323
Mutable, non thread-safe.

java-client/src/main/java/co/elastic/clients/ApiClient.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
import co.elastic.clients.json.JsonpMapperBase;
2727

2828
import javax.annotation.Nullable;
29+
import java.io.Closeable;
30+
import java.io.IOException;
2931
import java.lang.reflect.Type;
3032
import java.util.function.Function;
3133

32-
public abstract class ApiClient<T extends Transport, Self extends ApiClient<T, Self>> {
34+
public abstract class ApiClient<T extends Transport, Self extends ApiClient<T, Self>> implements Closeable {
3335

3436
protected final T transport;
3537
protected final TransportOptions transportOptions;
@@ -85,4 +87,15 @@ public TransportOptions _transportOptions() {
8587
public JsonpMapper _jsonpMapper() {
8688
return transport.jsonpMapper();
8789
}
90+
91+
/**
92+
* Close this client and associated resources, including the underlying {@link Transport} object.
93+
* <p>
94+
* If the underlying {@code Transport} object is shared with other API client objects, these objects will also become
95+
* unavailable.
96+
*/
97+
@Override
98+
public void close() throws IOException {
99+
transport.close();
100+
}
88101
}

java-client/src/main/java/co/elastic/clients/elasticsearch/_helpers/bulk/BulkIngester.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ public void add(Function<BulkOperation.Builder, ObjectBuilder<BulkOperation>> f,
373373
add(f.apply(new BulkOperation.Builder()).build(), context);
374374
}
375375

376+
/**
377+
* Close this ingester, first flushing any buffered operations. This <strong>does not close</strong>
378+
* the underlying @{link {@link ElasticsearchClient} and {@link co.elastic.clients.transport.Transport}.
379+
*/
376380
@Override
377381
public void close() {
378382
if (isClosed) {

java-client/src/test/java/co/elastic/clients/documentation/getting_started/ConnectingTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ public void createClient() throws Exception {
4646
restClient, new JacksonJsonpMapper());
4747

4848
// And create the API client
49-
ElasticsearchClient client = new ElasticsearchClient(transport);
49+
ElasticsearchClient esClient = new ElasticsearchClient(transport);
50+
51+
// Use the client...
52+
53+
// Close the client, also closing the underlying transport object and network connections.
54+
esClient.close();
5055
//end::create-client
5156

5257
//tag::first-request
53-
SearchResponse<Product> search = client.search(s -> s
58+
SearchResponse<Product> search = esClient.search(s -> s
5459
.index("products")
5560
.query(q -> q
5661
.term(t -> t

0 commit comments

Comments
 (0)