Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
0e87b9c1
Commit
0e87b9c1
authored
Jul 11, 2019
by
Dmytro Nosan
Committed by
Stephane Nicoll
Aug 01, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create RestClient from a RestHighLevelClient if available
See gh-17488
parent
bd815f6b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
124 additions
and
2 deletions
+124
-2
RestClientAutoConfiguration.java
...igure/elasticsearch/rest/RestClientAutoConfiguration.java
+25
-0
RestClientAutoConfigurationTests.java
.../elasticsearch/rest/RestClientAutoConfigurationTests.java
+52
-2
RestClientAutoConfigurationWithoutRestHighLevelClientTests.java
...ientAutoConfigurationWithoutRestHighLevelClientTests.java
+47
-0
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfiguration.java
View file @
0e87b9c1
...
@@ -28,6 +28,7 @@ import org.elasticsearch.client.RestHighLevelClient;
...
@@ -28,6 +28,7 @@ import org.elasticsearch.client.RestHighLevelClient;
import
org.springframework.beans.factory.ObjectProvider
;
import
org.springframework.beans.factory.ObjectProvider
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
...
@@ -92,4 +93,28 @@ public class RestClientAutoConfiguration {
...
@@ -92,4 +93,28 @@ public class RestClientAutoConfiguration {
}
}
/**
* Configuration to configure a {@link RestClient} bean from a
* {@link RestHighLevelClient} if such a bean has been registered by the application.
* If {@link RestHighLevelClient} is not unique or does not exist then
* {@link RestClientBuilder#build()} will be used.
*/
@Configuration
(
proxyBeanMethods
=
false
)
@ConditionalOnClass
(
RestHighLevelClient
.
class
)
@ConditionalOnBean
(
RestHighLevelClient
.
class
)
public
static
class
RestClientConfiguration
{
@Bean
@ConditionalOnMissingBean
public
RestClient
restClient
(
ObjectProvider
<
RestHighLevelClient
>
restHighLevelClient
,
RestClientBuilder
builder
)
{
RestHighLevelClient
client
=
restHighLevelClient
.
getIfUnique
();
if
(
client
!=
null
)
{
return
client
.
getLowLevelClient
();
}
return
builder
.
build
();
}
}
}
}
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfigurationTests.java
View file @
0e87b9c1
...
@@ -24,6 +24,7 @@ import org.elasticsearch.action.get.GetRequest;
...
@@ -24,6 +24,7 @@ import org.elasticsearch.action.get.GetRequest;
import
org.elasticsearch.action.index.IndexRequest
;
import
org.elasticsearch.action.index.IndexRequest
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RequestOptions
;
import
org.elasticsearch.client.RestClient
;
import
org.elasticsearch.client.RestClient
;
import
org.elasticsearch.client.RestClientBuilder
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.junit.Test
;
import
org.junit.Test
;
...
@@ -49,8 +50,11 @@ public class RestClientAutoConfigurationTests {
...
@@ -49,8 +50,11 @@ public class RestClientAutoConfigurationTests {
@Test
@Test
public
void
configureShouldCreateBothRestClientVariants
()
{
public
void
configureShouldCreateBothRestClientVariants
()
{
this
.
contextRunner
.
run
((
context
)
->
assertThat
(
context
).
hasSingleBean
(
RestClient
.
class
)
this
.
contextRunner
.
run
((
context
)
->
{
.
hasSingleBean
(
RestHighLevelClient
.
class
));
assertThat
(
context
).
hasSingleBean
(
RestClient
.
class
).
hasSingleBean
(
RestHighLevelClient
.
class
);
RestHighLevelClient
restHighLevelClient
=
context
.
getBean
(
RestHighLevelClient
.
class
);
assertThat
(
restHighLevelClient
.
getLowLevelClient
()).
isNotSameAs
(
context
.
getBean
(
RestClient
.
class
));
});
}
}
@Test
@Test
...
@@ -59,6 +63,27 @@ public class RestClientAutoConfigurationTests {
...
@@ -59,6 +63,27 @@ public class RestClientAutoConfigurationTests {
.
run
((
context
)
->
assertThat
(
context
).
hasSingleBean
(
RestClient
.
class
).
hasBean
(
"customRestClient"
));
.
run
((
context
)
->
assertThat
(
context
).
hasSingleBean
(
RestClient
.
class
).
hasBean
(
"customRestClient"
));
}
}
@Test
public
void
configureWhenCustomRestHighLevelClientShouldBackOff
()
{
this
.
contextRunner
.
withUserConfiguration
(
CustomRestHighLevelClientConfiguration
.
class
).
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
RestClient
.
class
).
hasSingleBean
(
RestHighLevelClient
.
class
);
RestHighLevelClient
restHighLevelClient
=
context
.
getBean
(
RestHighLevelClient
.
class
);
assertThat
(
restHighLevelClient
.
getLowLevelClient
()).
isSameAs
(
context
.
getBean
(
RestClient
.
class
));
});
}
@Test
public
void
configureWhenDefaultRestClientShouldCreateWhenNoUniqueRestHighLevelClient
()
{
this
.
contextRunner
.
withUserConfiguration
(
TwoCustomRestHighLevelClientConfiguration
.
class
).
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
RestClient
.
class
);
Map
<
String
,
RestHighLevelClient
>
restHighLevelClients
=
context
.
getBeansOfType
(
RestHighLevelClient
.
class
);
assertThat
(
restHighLevelClients
).
isNotEmpty
();
for
(
RestHighLevelClient
restHighLevelClient
:
restHighLevelClients
.
values
())
{
assertThat
(
restHighLevelClient
.
getLowLevelClient
()).
isNotSameAs
(
context
.
getBean
(
RestClient
.
class
));
}
});
}
@Test
@Test
public
void
configureWhenBuilderCustomizerShouldApply
()
{
public
void
configureWhenBuilderCustomizerShouldApply
()
{
this
.
contextRunner
.
withUserConfiguration
(
BuilderCustomizerConfiguration
.
class
).
run
((
context
)
->
{
this
.
contextRunner
.
withUserConfiguration
(
BuilderCustomizerConfiguration
.
class
).
run
((
context
)
->
{
...
@@ -106,4 +131,29 @@ public class RestClientAutoConfigurationTests {
...
@@ -106,4 +131,29 @@ public class RestClientAutoConfigurationTests {
}
}
@Configuration
static
class
CustomRestHighLevelClientConfiguration
{
@Bean
RestHighLevelClient
customRestHighLevelClient
(
RestClientBuilder
builder
)
{
return
new
RestHighLevelClient
(
builder
);
}
}
@Configuration
static
class
TwoCustomRestHighLevelClientConfiguration
{
@Bean
RestHighLevelClient
customRestHighLevelClient
(
RestClientBuilder
builder
)
{
return
new
RestHighLevelClient
(
builder
);
}
@Bean
RestHighLevelClient
customRestHighLevelClient1
(
RestClientBuilder
builder
)
{
return
new
RestHighLevelClient
(
builder
);
}
}
}
}
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfigurationWithoutRestHighLevelClientTests.java
0 → 100644
View file @
0e87b9c1
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
autoconfigure
.
elasticsearch
.
rest
;
import
org.elasticsearch.client.RestClient
;
import
org.elasticsearch.client.RestHighLevelClient
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.test.context.FilteredClassLoader
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
/**
* Tests for {@link RestClientAutoConfiguration} when {@link RestHighLevelClient} is not
* on the classpath.
*
* @author Dmytro Nosan
*/
public
class
RestClientAutoConfigurationWithoutRestHighLevelClientTests
{
private
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withConfiguration
(
AutoConfigurations
.
of
(
RestClientAutoConfiguration
.
class
))
.
withClassLoader
(
new
FilteredClassLoader
(
RestHighLevelClient
.
class
));
@Test
public
void
shouldCreateRestClientOnly
()
{
this
.
contextRunner
.
run
((
context
)
->
assertThat
(
context
).
hasSingleBean
(
RestClient
.
class
)
.
doesNotHaveBean
(
RestHighLevelClient
.
class
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment