Skip to content

Commit 0e87b9c

Browse files
nosansnicoll
authored andcommitted
Create RestClient from a RestHighLevelClient if available
See gh-17488
1 parent bd815f6 commit 0e87b9c

File tree

3 files changed

+124
-2
lines changed

3 files changed

+124
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfiguration.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.springframework.beans.factory.ObjectProvider;
3030
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3132
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3233
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3334
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -92,4 +93,28 @@ public RestHighLevelClient restHighLevelClient(RestClientBuilder restClientBuild
9293

9394
}
9495

96+
/**
97+
* Configuration to configure a {@link RestClient} bean from a
98+
* {@link RestHighLevelClient} if such a bean has been registered by the application.
99+
* If {@link RestHighLevelClient} is not unique or does not exist then
100+
* {@link RestClientBuilder#build()} will be used.
101+
*/
102+
@Configuration(proxyBeanMethods = false)
103+
@ConditionalOnClass(RestHighLevelClient.class)
104+
@ConditionalOnBean(RestHighLevelClient.class)
105+
public static class RestClientConfiguration {
106+
107+
@Bean
108+
@ConditionalOnMissingBean
109+
public RestClient restClient(ObjectProvider<RestHighLevelClient> restHighLevelClient,
110+
RestClientBuilder builder) {
111+
RestHighLevelClient client = restHighLevelClient.getIfUnique();
112+
if (client != null) {
113+
return client.getLowLevelClient();
114+
}
115+
return builder.build();
116+
}
117+
118+
}
119+
95120
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfigurationTests.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.action.index.IndexRequest;
2525
import org.elasticsearch.client.RequestOptions;
2626
import org.elasticsearch.client.RestClient;
27+
import org.elasticsearch.client.RestClientBuilder;
2728
import org.elasticsearch.client.RestHighLevelClient;
2829
import org.junit.Test;
2930

@@ -49,8 +50,11 @@ public class RestClientAutoConfigurationTests {
4950

5051
@Test
5152
public void configureShouldCreateBothRestClientVariants() {
52-
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(RestClient.class)
53-
.hasSingleBean(RestHighLevelClient.class));
53+
this.contextRunner.run((context) -> {
54+
assertThat(context).hasSingleBean(RestClient.class).hasSingleBean(RestHighLevelClient.class);
55+
RestHighLevelClient restHighLevelClient = context.getBean(RestHighLevelClient.class);
56+
assertThat(restHighLevelClient.getLowLevelClient()).isNotSameAs(context.getBean(RestClient.class));
57+
});
5458
}
5559

5660
@Test
@@ -59,6 +63,27 @@ public void configureWhenCustomClientShouldBackOff() {
5963
.run((context) -> assertThat(context).hasSingleBean(RestClient.class).hasBean("customRestClient"));
6064
}
6165

66+
@Test
67+
public void configureWhenCustomRestHighLevelClientShouldBackOff() {
68+
this.contextRunner.withUserConfiguration(CustomRestHighLevelClientConfiguration.class).run((context) -> {
69+
assertThat(context).hasSingleBean(RestClient.class).hasSingleBean(RestHighLevelClient.class);
70+
RestHighLevelClient restHighLevelClient = context.getBean(RestHighLevelClient.class);
71+
assertThat(restHighLevelClient.getLowLevelClient()).isSameAs(context.getBean(RestClient.class));
72+
});
73+
}
74+
75+
@Test
76+
public void configureWhenDefaultRestClientShouldCreateWhenNoUniqueRestHighLevelClient() {
77+
this.contextRunner.withUserConfiguration(TwoCustomRestHighLevelClientConfiguration.class).run((context) -> {
78+
assertThat(context).hasSingleBean(RestClient.class);
79+
Map<String, RestHighLevelClient> restHighLevelClients = context.getBeansOfType(RestHighLevelClient.class);
80+
assertThat(restHighLevelClients).isNotEmpty();
81+
for (RestHighLevelClient restHighLevelClient : restHighLevelClients.values()) {
82+
assertThat(restHighLevelClient.getLowLevelClient()).isNotSameAs(context.getBean(RestClient.class));
83+
}
84+
});
85+
}
86+
6287
@Test
6388
public void configureWhenBuilderCustomizerShouldApply() {
6489
this.contextRunner.withUserConfiguration(BuilderCustomizerConfiguration.class).run((context) -> {
@@ -106,4 +131,29 @@ public RestClientBuilderCustomizer myCustomizer() {
106131

107132
}
108133

134+
@Configuration
135+
static class CustomRestHighLevelClientConfiguration {
136+
137+
@Bean
138+
RestHighLevelClient customRestHighLevelClient(RestClientBuilder builder) {
139+
return new RestHighLevelClient(builder);
140+
}
141+
142+
}
143+
144+
@Configuration
145+
static class TwoCustomRestHighLevelClientConfiguration {
146+
147+
@Bean
148+
RestHighLevelClient customRestHighLevelClient(RestClientBuilder builder) {
149+
return new RestHighLevelClient(builder);
150+
}
151+
152+
@Bean
153+
RestHighLevelClient customRestHighLevelClient1(RestClientBuilder builder) {
154+
return new RestHighLevelClient(builder);
155+
}
156+
157+
}
158+
109159
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.elasticsearch.rest;
18+
19+
import org.elasticsearch.client.RestClient;
20+
import org.elasticsearch.client.RestHighLevelClient;
21+
import org.junit.Test;
22+
23+
import org.springframework.boot.autoconfigure.AutoConfigurations;
24+
import org.springframework.boot.test.context.FilteredClassLoader;
25+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link RestClientAutoConfiguration} when {@link RestHighLevelClient} is not
31+
* on the classpath.
32+
*
33+
* @author Dmytro Nosan
34+
*/
35+
public class RestClientAutoConfigurationWithoutRestHighLevelClientTests {
36+
37+
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
38+
.withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
39+
.withClassLoader(new FilteredClassLoader(RestHighLevelClient.class));
40+
41+
@Test
42+
public void shouldCreateRestClientOnly() {
43+
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(RestClient.class)
44+
.doesNotHaveBean(RestHighLevelClient.class));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)