Skip to content

Commit a95431c

Browse files
authored
fix: Spring auto-configuration works even without influxdb-client-reactive (#252)
1 parent 9d59455 commit a95431c

8 files changed

Lines changed: 197 additions & 75 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## 3.2.0 [unreleased]
22

3+
### Bug Fixes
4+
1. [#252](https://github.com/influxdata/influxdb-client-java/pull/252): Spring auto-configuration works even without `influxdb-client-reactive` [spring]
5+
6+
37
## 3.1.0 [2021-07-27]
48

59
### Breaking Changes

spring/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The default configuration can be override via properties:
1818

1919
```yaml
2020
influx:
21-
url: http://localhost:8086/api/v2 # URL to connect to InfluxDB.
21+
url: http://localhost:8086/ # URL to connect to InfluxDB.
2222
username: my-user # Username to use in the basic auth.
2323
password: my-password # Password to use in the basic auth.
2424
token: my-token # Token to use for the authorization.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
package com.influxdb.spring.influx;
23+
24+
import java.util.Collections;
25+
import javax.annotation.Nonnull;
26+
27+
import com.influxdb.client.InfluxDBClientOptions;
28+
29+
import okhttp3.OkHttpClient;
30+
import okhttp3.Protocol;
31+
import org.springframework.util.StringUtils;
32+
33+
/**
34+
* @author Jakub Bednar (04/08/2021 11:41)
35+
*/
36+
abstract class AbstractInfluxDB2AutoConfiguration {
37+
protected final InfluxDB2Properties properties;
38+
protected final InfluxDB2OkHttpClientBuilderProvider builderProvider;
39+
40+
protected AbstractInfluxDB2AutoConfiguration(final InfluxDB2Properties properties,
41+
final InfluxDB2OkHttpClientBuilderProvider builderProvider) {
42+
this.properties = properties;
43+
this.builderProvider = builderProvider;
44+
}
45+
46+
@Nonnull
47+
protected InfluxDBClientOptions.Builder makeBuilder() {
48+
OkHttpClient.Builder okHttpBuilder;
49+
if (builderProvider == null) {
50+
okHttpBuilder = new OkHttpClient.Builder()
51+
.protocols(Collections.singletonList(Protocol.HTTP_1_1))
52+
.readTimeout(properties.getReadTimeout())
53+
.writeTimeout(properties.getWriteTimeout())
54+
.connectTimeout(properties.getConnectTimeout());
55+
} else {
56+
okHttpBuilder = builderProvider.get();
57+
}
58+
59+
InfluxDBClientOptions.Builder influxBuilder = InfluxDBClientOptions.builder()
60+
.url(properties.getUrl())
61+
.bucket(properties.getBucket())
62+
.org(properties.getOrg())
63+
.okHttpClient(okHttpBuilder);
64+
65+
if (StringUtils.hasLength(properties.getToken())) {
66+
influxBuilder.authenticateToken(properties.getToken().toCharArray());
67+
} else if (StringUtils.hasLength(properties.getUsername()) && StringUtils.hasLength(properties.getPassword())) {
68+
influxBuilder.authenticate(properties.getUsername(), properties.getPassword().toCharArray());
69+
}
70+
return influxBuilder;
71+
}
72+
}

spring/src/main/java/com/influxdb/spring/influx/InfluxDB2AutoConfiguration.java

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,18 @@
2121
*/
2222
package com.influxdb.spring.influx;
2323

24-
import java.util.Collections;
25-
import javax.annotation.Nonnull;
26-
2724
import com.influxdb.client.InfluxDBClient;
2825
import com.influxdb.client.InfluxDBClientFactory;
2926
import com.influxdb.client.InfluxDBClientOptions;
30-
import com.influxdb.client.reactive.InfluxDBClientReactive;
31-
import com.influxdb.client.reactive.InfluxDBClientReactiveFactory;
3227

33-
import okhttp3.OkHttpClient;
34-
import okhttp3.Protocol;
3528
import org.springframework.beans.factory.ObjectProvider;
3629
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3730
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3831
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
39-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
4032
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4133
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4234
import org.springframework.context.annotation.Bean;
4335
import org.springframework.context.annotation.Configuration;
44-
import org.springframework.util.StringUtils;
4536

4637
/**
4738
* {@link EnableAutoConfiguration Auto-configuration} for InfluxDB 2.
@@ -51,63 +42,20 @@
5142
@Configuration
5243
@ConditionalOnClass(InfluxDBClient.class)
5344
@EnableConfigurationProperties(InfluxDB2Properties.class)
54-
public class InfluxDB2AutoConfiguration {
55-
56-
private final InfluxDB2Properties properties;
57-
58-
private final InfluxDB2OkHttpClientBuilderProvider builderProvider;
45+
public class InfluxDB2AutoConfiguration extends AbstractInfluxDB2AutoConfiguration {
5946

6047
public InfluxDB2AutoConfiguration(final InfluxDB2Properties properties,
6148
final ObjectProvider<InfluxDB2OkHttpClientBuilderProvider> builderProvider) {
62-
this.properties = properties;
63-
this.builderProvider = builderProvider.getIfAvailable();
49+
super(properties, builderProvider.getIfAvailable());
6450
}
6551

6652
@Bean
6753
@ConditionalOnProperty("influx.url")
68-
@ConditionalOnMissingBean
69-
@ConditionalOnMissingClass("com.influxdb.client.reactive.InfluxDBClientReactive")
54+
@ConditionalOnMissingBean(InfluxDBClient.class)
7055
public InfluxDBClient influxDBClient() {
7156

7257
InfluxDBClientOptions.Builder influxBuilder = makeBuilder();
7358

7459
return InfluxDBClientFactory.create(influxBuilder.build()).setLogLevel(properties.getLogLevel());
7560
}
76-
77-
@Bean
78-
@ConditionalOnProperty("influx.url")
79-
@ConditionalOnMissingBean
80-
@ConditionalOnClass(InfluxDBClientReactive.class)
81-
public InfluxDBClientReactive influxDBClientReactive() {
82-
InfluxDBClientOptions.Builder influxBuilder = makeBuilder();
83-
84-
return InfluxDBClientReactiveFactory.create(influxBuilder.build()).setLogLevel(properties.getLogLevel());
85-
}
86-
87-
@Nonnull
88-
private InfluxDBClientOptions.Builder makeBuilder() {
89-
OkHttpClient.Builder okHttpBuilder;
90-
if (builderProvider == null) {
91-
okHttpBuilder = new OkHttpClient.Builder()
92-
.protocols(Collections.singletonList(Protocol.HTTP_1_1))
93-
.readTimeout(properties.getReadTimeout())
94-
.writeTimeout(properties.getWriteTimeout())
95-
.connectTimeout(properties.getConnectTimeout());
96-
} else {
97-
okHttpBuilder = builderProvider.get();
98-
}
99-
100-
InfluxDBClientOptions.Builder influxBuilder = InfluxDBClientOptions.builder()
101-
.url(properties.getUrl())
102-
.bucket(properties.getBucket())
103-
.org(properties.getOrg())
104-
.okHttpClient(okHttpBuilder);
105-
106-
if (StringUtils.hasLength(properties.getToken())) {
107-
influxBuilder.authenticateToken(properties.getToken().toCharArray());
108-
} else if (StringUtils.hasLength(properties.getUsername()) && StringUtils.hasLength(properties.getPassword())) {
109-
influxBuilder.authenticate(properties.getUsername(), properties.getPassword().toCharArray());
110-
}
111-
return influxBuilder;
112-
}
11361
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
package com.influxdb.spring.influx;
23+
24+
import com.influxdb.client.InfluxDBClientOptions;
25+
import com.influxdb.client.reactive.InfluxDBClientReactive;
26+
import com.influxdb.client.reactive.InfluxDBClientReactiveFactory;
27+
28+
import org.springframework.beans.factory.ObjectProvider;
29+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
32+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
33+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
34+
import org.springframework.context.annotation.Bean;
35+
import org.springframework.context.annotation.Configuration;
36+
37+
/**
38+
* {@link EnableAutoConfiguration Auto-configuration} for InfluxDB 2.
39+
*
40+
* @author Jakub Bednar (bednar@github) (06/05/2019 13:09)
41+
*/
42+
@Configuration
43+
@ConditionalOnClass(name = "com.influxdb.client.reactive.InfluxDBClientReactive")
44+
@EnableConfigurationProperties(InfluxDB2Properties.class)
45+
public class InfluxDB2AutoConfigurationReactive extends AbstractInfluxDB2AutoConfiguration {
46+
47+
public InfluxDB2AutoConfigurationReactive(final InfluxDB2Properties properties,
48+
final ObjectProvider<InfluxDB2OkHttpClientBuilderProvider>
49+
builderProvider) {
50+
super(properties, builderProvider.getIfAvailable());
51+
}
52+
53+
@Bean
54+
@ConditionalOnProperty("influx.url")
55+
@ConditionalOnMissingBean(InfluxDBClientReactive.class)
56+
public InfluxDBClientReactive influxDBClientReactive() {
57+
InfluxDBClientOptions.Builder influxBuilder = makeBuilder();
58+
59+
return InfluxDBClientReactiveFactory.create(influxBuilder.build()).setLogLevel(properties.getLogLevel());
60+
}
61+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
22
com.influxdb.spring.health.InfluxDB2HealthIndicatorAutoConfiguration,\
3-
com.influxdb.spring.influx.InfluxDB2AutoConfiguration
3+
com.influxdb.spring.influx.InfluxDB2AutoConfiguration,\
4+
com.influxdb.spring.influx.InfluxDB2AutoConfigurationReactive
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
package com.influxdb.spring.influx;
23+
24+
import com.influxdb.client.InfluxDBClient;
25+
import com.influxdb.client.reactive.InfluxDBClientReactive;
26+
27+
import org.assertj.core.api.Assertions;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.platform.runner.JUnitPlatform;
30+
import org.junit.runner.RunWith;
31+
import org.springframework.boot.autoconfigure.AutoConfigurations;
32+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
33+
34+
/**
35+
* Tests for {@link InfluxDB2AutoConfiguration}.
36+
*
37+
* @author Jakub Bednar (bednar@github) (07/05/2019 12:59)
38+
*/
39+
@RunWith(JUnitPlatform.class)
40+
class InfluxDB2AutoConfigurationReactiveTest {
41+
42+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
43+
.withConfiguration(AutoConfigurations.of(InfluxDB2AutoConfigurationReactive.class));
44+
45+
46+
@Test
47+
public void influxDBClientReactive() {
48+
this.contextRunner.withPropertyValues("influx.url=http://localhost:8086/")
49+
.run(((context) -> Assertions.assertThat(context.getBeansOfType(InfluxDBClientReactive.class))
50+
.hasSize(1)))
51+
.run(((context) -> Assertions.assertThat(context.getBeansOfType(InfluxDBClient.class))
52+
.hasSize(0)));
53+
}
54+
}

spring/src/test/java/com/influxdb/spring/influx/InfluxDB2AutoConfigurationTest.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import javax.annotation.Nonnull;
2727

2828
import com.influxdb.client.InfluxDBClient;
29-
import com.influxdb.client.reactive.InfluxDBClientReactive;
3029

3130
import okhttp3.OkHttpClient;
3231
import okhttp3.Protocol;
@@ -35,7 +34,6 @@
3534
import org.junit.platform.runner.JUnitPlatform;
3635
import org.junit.runner.RunWith;
3736
import org.springframework.boot.autoconfigure.AutoConfigurations;
38-
import org.springframework.boot.test.context.FilteredClassLoader;
3937
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
4038
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
4139
import org.springframework.context.annotation.Bean;
@@ -52,7 +50,6 @@
5250
class InfluxDB2AutoConfigurationTest {
5351

5452
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
55-
.withClassLoader(new FilteredClassLoader(InfluxDBClientReactive.class))
5653
.withConfiguration(AutoConfigurations.of(InfluxDB2AutoConfiguration.class));
5754

5855
@Test
@@ -102,21 +99,6 @@ public void influxDBClientWithReadTimeout() {
10299
});
103100
}
104101

105-
@Test
106-
public void influxDBClientReactive() {
107-
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
108-
.withPropertyValues("influx.url=http://localhost:8086/")
109-
.withConfiguration(AutoConfigurations.of(InfluxDB2AutoConfiguration.class));
110-
111-
contextRunner
112-
.run(((context) -> Assertions.assertThat(context.getBeansOfType(InfluxDBClientReactive.class))
113-
.hasSize(1)));
114-
115-
contextRunner
116-
.run(((context) -> Assertions.assertThat(context.getBeansOfType(InfluxDBClient.class))
117-
.hasSize(0)));
118-
}
119-
120102
@Test
121103
public void protocolVersion() {
122104
this.contextRunner.withPropertyValues("influx.url=http://localhost:8086/", "spring.influx2.token:token")

0 commit comments

Comments
 (0)