Allow to customize OkHttpClient.Builder
This commit enables to auto-configure OkHttpClient.Builder in InfluxDbAutoConfiguration if it is available. See gh-9669
This commit is contained in:
committed by
Stephane Nicoll
parent
99b394a668
commit
406f41740e
@@ -16,10 +16,11 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.influx;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.influxdb.InfluxDB;
|
||||
import org.influxdb.InfluxDBFactory;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -27,12 +28,14 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for InfluxDB.
|
||||
*
|
||||
* @author Sergey Kuptsov
|
||||
* @author Stephane Nicoll
|
||||
* @author Eddú Meléndez
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Configuration
|
||||
@@ -42,19 +45,34 @@ public class InfluxDbAutoConfiguration {
|
||||
|
||||
private final InfluxDbProperties properties;
|
||||
|
||||
public InfluxDbAutoConfiguration(InfluxDbProperties properties) {
|
||||
private final OkHttpClient.Builder builder;
|
||||
|
||||
public InfluxDbAutoConfiguration(InfluxDbProperties properties,
|
||||
ObjectProvider<OkHttpClient.Builder> builder) {
|
||||
this.properties = properties;
|
||||
this.builder = builder.getIfAvailable();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty("spring.influx.url")
|
||||
public InfluxDB influxDb() {
|
||||
if (Strings.isNullOrEmpty(this.properties.getUser())) {
|
||||
return InfluxDBFactory.connect(this.properties.getUrl());
|
||||
if (hasCredentials() && this.builder != null) {
|
||||
return InfluxDBFactory.connect(this.properties.getUrl(),
|
||||
this.properties.getUser(), this.properties.getUrl(), this.builder);
|
||||
}
|
||||
return InfluxDBFactory.connect(this.properties.getUrl(),
|
||||
this.properties.getUser(), this.properties.getPassword());
|
||||
else if (hasCredentials()) {
|
||||
return InfluxDBFactory.connect(this.properties.getUrl(),
|
||||
this.properties.getUser(), this.properties.getPassword());
|
||||
}
|
||||
else if (this.builder != null) {
|
||||
return InfluxDBFactory.connect(this.properties.getUrl(), this.builder);
|
||||
}
|
||||
return InfluxDBFactory.connect(this.properties.getUrl());
|
||||
}
|
||||
|
||||
private boolean hasCredentials() {
|
||||
return StringUtils.hasText(this.properties.getUser())
|
||||
&& StringUtils.hasText(this.properties.getPassword());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,12 +16,20 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.influx;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.influxdb.InfluxDB;
|
||||
import org.influxdb.impl.InfluxDBImpl;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -30,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*
|
||||
* @author Sergey Kuptsov
|
||||
* @author Stephane Nicoll
|
||||
* @author Eddú Meléndez
|
||||
*/
|
||||
public class InfluxDbAutoConfigurationTests {
|
||||
|
||||
@@ -59,14 +68,59 @@ public class InfluxDbAutoConfigurationTests {
|
||||
public void influxDbCanBeCreatedWithoutCredentials() {
|
||||
load("spring.influx.url=http://localhost");
|
||||
assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1);
|
||||
int readTimeout = getReadTimeoutProperty();
|
||||
assertThat(readTimeout).isEqualTo(10_000);
|
||||
}
|
||||
|
||||
private void load(String... environment) {
|
||||
@Test
|
||||
public void influxDbWithoutCredentialsAndOkHttpClientBuilder() {
|
||||
load(CustomOkHttpClientBuilderConfig.class, "spring.influx.url=http://localhost");
|
||||
assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1);
|
||||
int readTimeout = getReadTimeoutProperty();
|
||||
assertThat(readTimeout).isEqualTo(30_000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void influxDbWithOkHttpClientBuilder() {
|
||||
load(CustomOkHttpClientBuilderConfig.class, "spring.influx.url=http://localhost",
|
||||
"spring.influx.password:password", "spring.influx.user:user");
|
||||
assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1);
|
||||
int readTimeout = getReadTimeoutProperty();
|
||||
assertThat(readTimeout).isEqualTo(30_000);
|
||||
}
|
||||
|
||||
private int getReadTimeoutProperty() {
|
||||
InfluxDB influxDB = this.context.getBean(InfluxDB.class);
|
||||
Retrofit retrofit = (Retrofit) ReflectionTestUtils.getField(influxDB,
|
||||
InfluxDBImpl.class, "retrofit");
|
||||
OkHttpClient callFactory = (OkHttpClient) ReflectionTestUtils.getField(retrofit,
|
||||
Retrofit.class, "callFactory");
|
||||
return callFactory.readTimeoutMillis();
|
||||
}
|
||||
|
||||
private void load(Class<?> clazz, String... environment) {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
TestPropertyValues.of(environment).applyTo(ctx);
|
||||
ctx.register(InfluxDbAutoConfiguration.class);
|
||||
if (clazz != null) {
|
||||
ctx.register(clazz);
|
||||
}
|
||||
ctx.refresh();
|
||||
this.context = ctx;
|
||||
}
|
||||
|
||||
private void load(String... environment) {
|
||||
load(null, environment);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class CustomOkHttpClientBuilderConfig {
|
||||
|
||||
@Bean
|
||||
public OkHttpClient.Builder builder() {
|
||||
return new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user