Commit 2cfcd269 authored by Stephane Nicoll's avatar Stephane Nicoll

Polish "Allow to configure the Elasticsearch rest client timeouts"

Closes gh-15965
parent 5bacb325
...@@ -78,29 +78,39 @@ public class RestClientAutoConfigurationTests { ...@@ -78,29 +78,39 @@ public class RestClientAutoConfigurationTests {
} }
@Test @Test
public void defaultTimeoutsShouldBeConfigured() { public void configureWithNoTimeoutsApplyDefaults() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(RestClient.class); assertThat(context).hasSingleBean(RestClient.class);
RestClient restClient = context.getBean(RestClient.class); RestClient restClient = context.getBean(RestClient.class);
assertTimeouts(restClient, assertTimeouts(restClient,
Duration.ofMillis(RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MILLIS), Duration.ofMillis(RestClientBuilder.DEFAULT_SOCKET_TIMEOUT_MILLIS) Duration.ofMillis(RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MILLIS),
); Duration.ofMillis(RestClientBuilder.DEFAULT_SOCKET_TIMEOUT_MILLIS));
}); });
} }
@Test @Test
public void timeoutsCanBeConfigured() { public void configureWithCustomTimeouts() {
this.contextRunner this.contextRunner
.withPropertyValues("spring.elasticsearch.rest.connection-timeout=15s", .withPropertyValues("spring.elasticsearch.rest.connection-timeout=15s",
"spring.elasticsearch.rest.read-timeout=1m") "spring.elasticsearch.rest.read-timeout=1m")
.run((context) -> { .run((context) -> {
assertThat(context).hasSingleBean(RestClient.class); assertThat(context).hasSingleBean(RestClient.class);
RestClient restClient = context.getBean(RestClient.class); RestClient restClient = context.getBean(RestClient.class);
assertTimeouts(restClient, Duration.ofSeconds(15), Duration.ofMinutes(1) assertTimeouts(restClient, Duration.ofSeconds(15),
); Duration.ofMinutes(1));
}); });
} }
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()));
}
@Test @Test
public void restClientCanQueryElasticsearchNode() { public void restClientCanQueryElasticsearchNode() {
this.contextRunner this.contextRunner
...@@ -121,15 +131,6 @@ public class RestClientAutoConfigurationTests { ...@@ -121,15 +131,6 @@ 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 {
......
...@@ -4805,6 +4805,7 @@ You can further tune how `RestClient` is configured, as shown in the following e ...@@ -4805,6 +4805,7 @@ You can further tune how `RestClient` is configured, as shown in the following e
[source,properties,indent=0] [source,properties,indent=0]
---- ----
spring.elasticsearch.rest.uris=https://search.example.com:9200 spring.elasticsearch.rest.uris=https://search.example.com:9200
spring.elasticsearch.rest.read-timeout=10s
spring.elasticsearch.rest.username=user spring.elasticsearch.rest.username=user
spring.elasticsearch.rest.password=secret spring.elasticsearch.rest.password=secret
---- ----
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment