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
5bacb325
Commit
5bacb325
authored
Feb 15, 2019
by
Dmytro Nosan
Committed by
Stephane Nicoll
Apr 18, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow to configure the Elasticsearch rest client timeouts
See gh-15965
parent
af4ce2d5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
74 additions
and
1 deletion
+74
-1
RestClientAutoConfiguration.java
...igure/elasticsearch/rest/RestClientAutoConfiguration.java
+10
-0
RestClientProperties.java
...utoconfigure/elasticsearch/rest/RestClientProperties.java
+28
-1
RestClientAutoConfigurationTests.java
.../elasticsearch/rest/RestClientAutoConfigurationTests.java
+36
-0
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfiguration.java
View file @
5bacb325
...
@@ -16,6 +16,8 @@
...
@@ -16,6 +16,8 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
elasticsearch
.
rest
;
package
org
.
springframework
.
boot
.
autoconfigure
.
elasticsearch
.
rest
;
import
java.time.Duration
;
import
org.apache.http.HttpHost
;
import
org.apache.http.HttpHost
;
import
org.apache.http.auth.AuthScope
;
import
org.apache.http.auth.AuthScope
;
import
org.apache.http.auth.Credentials
;
import
org.apache.http.auth.Credentials
;
...
@@ -68,6 +70,14 @@ public class RestClientAutoConfiguration {
...
@@ -68,6 +70,14 @@ public class RestClientAutoConfiguration {
builder
.
setHttpClientConfigCallback
((
httpClientBuilder
)
->
httpClientBuilder
builder
.
setHttpClientConfigCallback
((
httpClientBuilder
)
->
httpClientBuilder
.
setDefaultCredentialsProvider
(
credentialsProvider
));
.
setDefaultCredentialsProvider
(
credentialsProvider
));
});
});
builder
.
setRequestConfigCallback
((
requestConfigBuilder
)
->
{
map
.
from
(
properties:
:
getConnectionTimeout
).
whenNonNull
()
.
as
(
Duration:
:
toMillis
).
asInt
(
Math:
:
toIntExact
)
.
to
(
requestConfigBuilder:
:
setConnectTimeout
);
map
.
from
(
properties:
:
getReadTimeout
).
whenNonNull
().
as
(
Duration:
:
toMillis
)
.
asInt
(
Math:
:
toIntExact
).
to
(
requestConfigBuilder:
:
setSocketTimeout
);
return
requestConfigBuilder
;
});
builderCustomizers
.
orderedStream
()
builderCustomizers
.
orderedStream
()
.
forEach
((
customizer
)
->
customizer
.
customize
(
builder
));
.
forEach
((
customizer
)
->
customizer
.
customize
(
builder
));
return
builder
;
return
builder
;
...
...
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientProperties.java
View file @
5bacb325
/*
/*
* Copyright 2012-201
8
the original author or authors.
* Copyright 2012-201
9
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -16,6 +16,7 @@
...
@@ -16,6 +16,7 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
elasticsearch
.
rest
;
package
org
.
springframework
.
boot
.
autoconfigure
.
elasticsearch
.
rest
;
import
java.time.Duration
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.List
;
...
@@ -47,6 +48,16 @@ public class RestClientProperties {
...
@@ -47,6 +48,16 @@ public class RestClientProperties {
*/
*/
private
String
password
;
private
String
password
;
/**
* Connection timeout.
*/
private
Duration
connectionTimeout
=
Duration
.
ofSeconds
(
1
);
/**
* Read timeout.
*/
private
Duration
readTimeout
=
Duration
.
ofSeconds
(
30
);
public
List
<
String
>
getUris
()
{
public
List
<
String
>
getUris
()
{
return
this
.
uris
;
return
this
.
uris
;
}
}
...
@@ -71,4 +82,20 @@ public class RestClientProperties {
...
@@ -71,4 +82,20 @@ public class RestClientProperties {
this
.
password
=
password
;
this
.
password
=
password
;
}
}
public
Duration
getConnectionTimeout
()
{
return
this
.
connectionTimeout
;
}
public
void
setConnectionTimeout
(
Duration
connectionTimeout
)
{
this
.
connectionTimeout
=
connectionTimeout
;
}
public
Duration
getReadTimeout
()
{
return
this
.
readTimeout
;
}
public
void
setReadTimeout
(
Duration
readTimeout
)
{
this
.
readTimeout
=
readTimeout
;
}
}
}
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfigurationTests.java
View file @
5bacb325
...
@@ -16,6 +16,7 @@
...
@@ -16,6 +16,7 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
elasticsearch
.
rest
;
package
org
.
springframework
.
boot
.
autoconfigure
.
elasticsearch
.
rest
;
import
java.time.Duration
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -23,6 +24,7 @@ import org.elasticsearch.action.get.GetRequest;
...
@@ -23,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.ClassRule
;
import
org.junit.ClassRule
;
import
org.junit.Test
;
import
org.junit.Test
;
...
@@ -32,6 +34,7 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner;
...
@@ -32,6 +34,7 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import
org.springframework.boot.testsupport.testcontainers.ElasticsearchContainer
;
import
org.springframework.boot.testsupport.testcontainers.ElasticsearchContainer
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
mock
;
...
@@ -74,6 +77,30 @@ public class RestClientAutoConfigurationTests {
...
@@ -74,6 +77,30 @@ public class RestClientAutoConfigurationTests {
});
});
}
}
@Test
public
void
defaultTimeoutsShouldBeConfigured
()
{
this
.
contextRunner
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
RestClient
.
class
);
RestClient
restClient
=
context
.
getBean
(
RestClient
.
class
);
assertTimeouts
(
restClient
,
Duration
.
ofMillis
(
RestClientBuilder
.
DEFAULT_CONNECT_TIMEOUT_MILLIS
),
Duration
.
ofMillis
(
RestClientBuilder
.
DEFAULT_SOCKET_TIMEOUT_MILLIS
)
);
});
}
@Test
public
void
timeoutsCanBeConfigured
()
{
this
.
contextRunner
.
withPropertyValues
(
"spring.elasticsearch.rest.connection-timeout=15s"
,
"spring.elasticsearch.rest.read-timeout=1m"
)
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
RestClient
.
class
);
RestClient
restClient
=
context
.
getBean
(
RestClient
.
class
);
assertTimeouts
(
restClient
,
Duration
.
ofSeconds
(
15
),
Duration
.
ofMinutes
(
1
)
);
});
}
@Test
@Test
public
void
restClientCanQueryElasticsearchNode
()
{
public
void
restClientCanQueryElasticsearchNode
()
{
this
.
contextRunner
this
.
contextRunner
...
@@ -94,6 +121,15 @@ public class RestClientAutoConfigurationTests {
...
@@ -94,6 +121,15 @@ public class RestClientAutoConfigurationTests {
});
});
}
}
private
static
void
assertTimeouts
(
RestClient
restClient
,
Duration
connectTimeout
,
Duration
readTimeout
)
{
Object
client
=
ReflectionTestUtils
.
getField
(
restClient
,
"client"
);
Object
config
=
ReflectionTestUtils
.
getField
(
client
,
"defaultConfig"
);
assertThat
(
config
).
hasFieldOrPropertyWithValue
(
"socketTimeout"
,
Math
.
toIntExact
(
readTimeout
.
toMillis
()));
assertThat
(
config
).
hasFieldOrPropertyWithValue
(
"connectTimeout"
,
Math
.
toIntExact
(
connectTimeout
.
toMillis
()));
}
@Configuration
(
proxyBeanMethods
=
false
)
@Configuration
(
proxyBeanMethods
=
false
)
static
class
CustomRestClientConfiguration
{
static
class
CustomRestClientConfiguration
{
...
...
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