From a22d089cdc9b06cdc97e0a3b2d8a03451218ed23 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 9 Oct 2018 13:33:14 +0200 Subject: [PATCH] Use a dedicated hook point to customize InfluxDB's HTTP client This commit introduces a dedicated hook point for InfluxDB's http client builder and retains backward compatibility, in a deprecated fashion, for looking up a OkHttpClient.Builder bean. Closes gh-14709 --- .../influx/InfluxDbAutoConfiguration.java | 30 ++++++++- .../InfluxDbOkHttpClientBuilderProvider.java | 35 +++++++++++ .../InfluxDbAutoConfigurationTests.java | 61 ++++++++++++++----- .../main/asciidoc/spring-boot-features.adoc | 2 +- 4 files changed, 110 insertions(+), 18 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbOkHttpClientBuilderProvider.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java index 3f94e38175..91d754ce4a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -17,6 +17,8 @@ package org.springframework.boot.autoconfigure.influx; import okhttp3.OkHttpClient; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.influxdb.InfluxDB; import org.influxdb.impl.InfluxDBImpl; @@ -42,14 +44,36 @@ import org.springframework.context.annotation.Configuration; @EnableConfigurationProperties(InfluxDbProperties.class) public class InfluxDbAutoConfiguration { + private static final Log logger = LogFactory.getLog(InfluxDbAutoConfiguration.class); + private final InfluxDbProperties properties; private final OkHttpClient.Builder builder; + @SuppressWarnings("deprecation") public InfluxDbAutoConfiguration(InfluxDbProperties properties, - ObjectProvider builder) { + ObjectProvider builder, + ObjectProvider deprecatedBuilder) { this.properties = properties; - this.builder = builder.getIfAvailable(OkHttpClient.Builder::new); + this.builder = determineBuilder(builder.getIfAvailable(), + deprecatedBuilder.getIfAvailable()); + } + + @Deprecated + private static OkHttpClient.Builder determineBuilder( + InfluxDbOkHttpClientBuilderProvider builder, + OkHttpClient.Builder deprecatedBuilder) { + if (builder != null) { + return builder.get(); + } + else if (deprecatedBuilder != null) { + logger.warn( + "InfluxDB client customizations using a OkHttpClient.Builder is deprecated, register a " + + InfluxDbOkHttpClientBuilderProvider.class.getSimpleName() + + " bean instead"); + return deprecatedBuilder; + } + return new OkHttpClient.Builder(); } @Bean diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbOkHttpClientBuilderProvider.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbOkHttpClientBuilderProvider.java new file mode 100644 index 0000000000..9a01b92e70 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbOkHttpClientBuilderProvider.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-2018 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 + * + * http://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.influx; + +import java.util.function.Supplier; + +import okhttp3.OkHttpClient; +import org.influxdb.InfluxDB; + +/** + * Provide the {@link OkHttpClient.Builder} to use to customize the auto-configured + * {@link InfluxDB} instance. + * + * @author Stephane Nicoll + * @since 2.1.0 + */ +@FunctionalInterface +public interface InfluxDbOkHttpClientBuilderProvider + extends Supplier { + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java index cc9481ca78..f096a74f4d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -20,6 +20,7 @@ import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import org.influxdb.InfluxDB; +import org.junit.Rule; import org.junit.Test; import retrofit2.Retrofit; @@ -27,6 +28,7 @@ import org.springframework.beans.DirectFieldAccessor; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.boot.test.rule.OutputCapture; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -41,6 +43,9 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class InfluxDbAutoConfigurationTests { + @Rule + public OutputCapture output = new OutputCapture(); + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(InfluxDbAutoConfiguration.class)); @@ -71,25 +76,43 @@ public class InfluxDbAutoConfigurationTests { } @Test - public void influxDbWithoutCredentialsAndOkHttpClientBuilder() { + public void influxDbWithOkHttpClientBuilderProvider() { + this.contextRunner + .withUserConfiguration(CustomOkHttpClientBuilderProviderConfig.class) + .withPropertyValues("spring.influx.url=http://localhost") + .run((context) -> { + assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1); + int readTimeout = getReadTimeoutProperty(context); + assertThat(readTimeout).isEqualTo(40_000); + }); + } + + @Test + public void influxDbWithOkHttpClientBuilderProviderIgnoreOkHttpClientBuilder() { + this.contextRunner + .withUserConfiguration(CustomOkHttpClientBuilderConfig.class, + CustomOkHttpClientBuilderProviderConfig.class) + .withPropertyValues("spring.influx.url=http://localhost") + .run((context) -> { + assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1); + int readTimeout = getReadTimeoutProperty(context); + assertThat(readTimeout).isEqualTo(40_000); + assertThat(this.output.toString()).doesNotContain( + "InfluxDB client customizations using a OkHttpClient.Builder is deprecated"); + }); + } + + @Test + @Deprecated + public void influxDbWithOkHttpClientBuilder() { this.contextRunner.withUserConfiguration(CustomOkHttpClientBuilderConfig.class) .withPropertyValues("spring.influx.url=http://localhost") .run((context) -> { assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1); int readTimeout = getReadTimeoutProperty(context); assertThat(readTimeout).isEqualTo(30_000); - }); - } - - @Test - public void influxDbWithOkHttpClientBuilder() { - this.contextRunner.withUserConfiguration(CustomOkHttpClientBuilderConfig.class) - .withPropertyValues("spring.influx.url=http://localhost", - "spring.influx.password:password", "spring.influx.user:user") - .run((context) -> { - assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1); - int readTimeout = getReadTimeoutProperty(context); - assertThat(readTimeout).isEqualTo(30_000); + assertThat(this.output.toString()).contains( + "InfluxDB client customizations using a OkHttpClient.Builder is deprecated"); }); } @@ -102,6 +125,16 @@ public class InfluxDbAutoConfigurationTests { return callFactory.readTimeoutMillis(); } + @Configuration + static class CustomOkHttpClientBuilderProviderConfig { + + @Bean + public InfluxDbOkHttpClientBuilderProvider influxDbOkHttpClientBuilderProvider() { + return () -> new OkHttpClient.Builder().readTimeout(40, TimeUnit.SECONDS); + } + + } + @Configuration static class CustomOkHttpClientBuilderConfig { diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 5f355e25d8..cc7f982183 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -4829,7 +4829,7 @@ If the connection to InfluxDB requires a user and password, you can set the `spring.influx.user` and `spring.influx.password` properties accordingly. InfluxDB relies on OkHttp. If you need to tune the http client `InfluxDB` uses behind the -scenes, you can register an `OkHttpClient.Builder` bean. +scenes, you can register an `InfluxDbOkHttpClientBuilderProvider` bean.