Skip to content

Commit 1b972de

Browse files
authored
Make ApiClient and subclasses Closeable (#851)
1 parent 79754d0 commit 1b972de

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
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.

docs/release-notes/release-highlights.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ These are the important new features and changes in minor releases. Every releas
55

66
For a list of detailed changes, including bug fixes, please see the https://github.com/elastic/elasticsearch-java/releases[GitHub project realease notes].
77

8+
[discrete]
9+
==== Version 8.16
10+
* `ElasticsearchClient` is now `Closeable`. Closing a client object also closes the underlying transport - https://github.com/elastic/elasticsearch-java/pull/851[#851]
11+
812
[discrete]
913
==== Version 8.13
1014

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
@@ -378,6 +378,10 @@ public void add(Function<BulkOperation.Builder, ObjectBuilder<BulkOperation>> f,
378378
add(f.apply(new BulkOperation.Builder()).build(), context);
379379
}
380380

381+
/**
382+
* Close this ingester, first flushing any buffered operations. This <strong>does not close</strong>
383+
* the underlying @{link {@link ElasticsearchClient} and {@link co.elastic.clients.transport.Transport}.
384+
*/
381385
@Override
382386
public void close() {
383387
if (isClosed) {

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public void createClient() throws Exception {
7070

7171
// Use the client...
7272

73-
// Close the transport, freeing the underlying thread
74-
transport.close();
73+
// Close the client, also closing the underlying transport object and network connections.
74+
esClient.close();
7575
//end::create-client
7676

7777
//tag::first-request
@@ -123,8 +123,8 @@ restClient, new JacksonJsonpMapper(), null, esOtelInstrumentation
123123

124124
// Use the client...
125125

126-
// Close the transport, freeing the underlying thread
127-
transport.close();
126+
// Close the client, also closing the underlying transport object and network connections.
127+
esClient.close();
128128
//end::create-client-otel
129129
}
130130

@@ -159,12 +159,12 @@ AuthScope.ANY, new UsernamePasswordCredentials(login, password)
159159

160160
// Create the transport and the API client
161161
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
162-
ElasticsearchClient client = new ElasticsearchClient(transport);
162+
ElasticsearchClient esClient = new ElasticsearchClient(transport);
163163

164164
// Use the client...
165165

166-
// Close the transport, freeing the underlying thread
167-
transport.close();
166+
// Close the client, also closing the underlying transport object and network connections.
167+
esClient.close();
168168
//end::create-secure-client-cert
169169
}
170170

@@ -199,12 +199,12 @@ AuthScope.ANY, new UsernamePasswordCredentials(login, password)
199199

200200
// Create the transport and the API client
201201
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
202-
ElasticsearchClient client = new ElasticsearchClient(transport);
202+
ElasticsearchClient esClient = new ElasticsearchClient(transport);
203203

204204
// Use the client...
205205

206-
// Close the transport, freeing the underlying thread
207-
transport.close();
206+
// Close the client, also closing the underlying transport object and network connections.
207+
esClient.close();
208208
//end::create-secure-client-fingerprint
209209
}
210210

0 commit comments

Comments
 (0)