Skip to content

Commit 4d62f8d

Browse files
committed
HSEARCH-5482 Hide the REST JDK client in the impl package and allow passing a reference to the HttpClient instead
1 parent 8429125 commit 4d62f8d

File tree

5 files changed

+21
-24
lines changed

5 files changed

+21
-24
lines changed

backend/elasticsearch-client/jdk-rest-client/src/main/java/org/hibernate/search/backend/elasticsearch/client/jdk/cfg/spi/ClientJdkElasticsearchBackendClientSpiSettings.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ public final class ClientJdkElasticsearchBackendClientSpiSettings {
1919
public static final String PREFIX = EngineSettings.PREFIX + "backend.";
2020

2121
/**
22-
* An external Elasticsearch client instance that Hibernate Search should use for all requests to Elasticsearch.
22+
* An external HTTP client instance that Hibernate Search should use for all requests to Elasticsearch.
2323
* <p>
24-
* If this is set, Hibernate Search will not attempt to create its own Elasticsearch,
25-
* and all other client-related configuration properties
26-
* (hosts/uris, authentication, discovery, timeouts, max connections, configurer, ...)
24+
* If this is set, Hibernate Search will not attempt to create its own {@link java.net.http.HttpClient},
25+
* and all client-related configuration properties (authentication, timeouts, configurer, ...)
2726
* will be ignored.
2827
* <p>
29-
* Expects a reference to a bean of type {@link RestJdkClient}.
28+
* Hibernate Search will still apply some configuration properties (e.g. hosts/uris)
29+
* that are not handled by the {@link java.net.http.HttpClient} itself.
30+
* <p>
31+
* Expects a reference to a bean of type {@link java.net.http.HttpClient}.
3032
* <p>
3133
* Defaults to nothing: if no client instance is provided, Hibernate Search will create its own.
3234
* <p>

backend/elasticsearch-client/jdk-rest-client/src/main/java/org/hibernate/search/backend/elasticsearch/client/jdk/impl/ClientJdkElasticsearchClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.hibernate.search.backend.elasticsearch.client.common.spi.ElasticsearchRequest;
3131
import org.hibernate.search.backend.elasticsearch.client.common.spi.ElasticsearchResponse;
3232
import org.hibernate.search.backend.elasticsearch.client.common.util.spi.ElasticsearchClientUtils;
33-
import org.hibernate.search.backend.elasticsearch.client.jdk.cfg.spi.RestJdkClient;
3433
import org.hibernate.search.backend.elasticsearch.logging.spi.ElasticsearchClientLog;
3534
import org.hibernate.search.backend.elasticsearch.logging.spi.ElasticsearchRequestLog;
3635
import org.hibernate.search.engine.common.execution.spi.SimpleScheduledExecutor;

backend/elasticsearch-client/jdk-rest-client/src/main/java/org/hibernate/search/backend/elasticsearch/client/jdk/impl/ClientJdkElasticsearchClientFactory.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import org.hibernate.search.backend.elasticsearch.client.jdk.ElasticsearchHttpClientConfigurer;
3131
import org.hibernate.search.backend.elasticsearch.client.jdk.cfg.ClientJdkElasticsearchBackendClientSettings;
3232
import org.hibernate.search.backend.elasticsearch.client.jdk.cfg.spi.ClientJdkElasticsearchBackendClientSpiSettings;
33-
import org.hibernate.search.backend.elasticsearch.client.jdk.cfg.spi.NodeProvider;
34-
import org.hibernate.search.backend.elasticsearch.client.jdk.cfg.spi.RestJdkClient;
3533
import org.hibernate.search.backend.elasticsearch.logging.spi.ConfigurationLog;
3634
import org.hibernate.search.engine.cfg.ConfigurationPropertySource;
3735
import org.hibernate.search.engine.cfg.spi.ConfigurationProperty;
@@ -46,9 +44,9 @@
4644

4745
public class ClientJdkElasticsearchClientFactory implements ElasticsearchClientFactory {
4846

49-
private static final OptionalConfigurationProperty<BeanReference<? extends RestJdkClient>> CLIENT_INSTANCE =
47+
private static final OptionalConfigurationProperty<BeanReference<? extends HttpClient>> CLIENT_INSTANCE =
5048
ConfigurationProperty.forKey( ClientJdkElasticsearchBackendClientSpiSettings.CLIENT_INSTANCE )
51-
.asBeanReference( RestJdkClient.class )
49+
.asBeanReference( HttpClient.class )
5250
.build();
5351

5452
private static final OptionalConfigurationProperty<List<String>> HOSTS =
@@ -106,18 +104,10 @@ public ElasticsearchClientImplementor create(BeanResolver beanResolver, Configur
106104
GsonProvider gsonProvider) {
107105
Optional<Integer> requestTimeoutMs = REQUEST_TIMEOUT.get( propertySource );
108106

109-
Optional<BeanHolder<? extends RestJdkClient>> providedRestClientHolder = CLIENT_INSTANCE.getAndMap(
110-
propertySource, beanResolver::resolve );
111-
112-
BeanHolder<? extends RestJdkClient> restClientHolder;
113-
if ( providedRestClientHolder.isPresent() ) {
114-
restClientHolder = providedRestClientHolder.get();
115-
}
116-
else {
117-
NodeProvider nodeProvider = NodeProvider.fromOptionalStrings( PROTOCOL.get( propertySource ),
118-
HOSTS.get( propertySource ), URIS.get( propertySource ), PATH_PREFIX.get( propertySource ) );
119-
restClientHolder = createClient( beanResolver, propertySource, threadProvider, threadNamePrefix, nodeProvider );
120-
}
107+
NodeProvider nodeProvider = NodeProvider.fromOptionalStrings( PROTOCOL.get( propertySource ),
108+
HOSTS.get( propertySource ), URIS.get( propertySource ), PATH_PREFIX.get( propertySource ) );
109+
BeanHolder<? extends RestJdkClient> restClientHolder =
110+
createClient( beanResolver, propertySource, threadProvider, threadNamePrefix, nodeProvider );
121111

122112
return new ClientJdkElasticsearchClient(
123113
restClientHolder, timeoutExecutorService, requestTimeoutMs,
@@ -149,6 +139,12 @@ private static List<HttpRequestInterceptor> createRequestInterceptors(BeanResolv
149139
private BeanHolder<? extends RestJdkClient> createClient(BeanResolver beanResolver,
150140
ConfigurationPropertySource propertySource,
151141
ThreadProvider threadProvider, String threadNamePrefix, NodeProvider nodeProvider) {
142+
Optional<BeanHolder<? extends HttpClient>> providedHttpClientHolder = CLIENT_INSTANCE.getAndMap(
143+
propertySource, beanResolver::resolve );
144+
if ( providedHttpClientHolder.isPresent() ) {
145+
return BeanHolder.ofCloseable( new RestJdkClient( nodeProvider, providedHttpClientHolder.get().get() ) );
146+
}
147+
152148
HttpClient.Builder builder = HttpClient.newBuilder()
153149
// NOTE: ES does not work ok with HTTP 2 if we don't send the content length and that can happen so let's stick to 1.1 for now ?
154150
// (we end up with Caused by: java.io.IOException: Received RST_STREAM: Stream cancelled)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
5-
package org.hibernate.search.backend.elasticsearch.client.jdk.cfg.spi;
5+
package org.hibernate.search.backend.elasticsearch.client.jdk.impl;
66

77
import java.net.URI;
88
import java.net.URISyntaxException;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
5-
package org.hibernate.search.backend.elasticsearch.client.jdk.cfg.spi;
5+
package org.hibernate.search.backend.elasticsearch.client.jdk.impl;
66

77
import java.net.http.HttpClient;
88
import java.net.http.HttpRequest;

0 commit comments

Comments
 (0)