Skip to content

Commit 8f4bf23

Browse files
committed
Update configuration properties to use Duration
Update appropriate configuration properties to use the `Duration` type, rather than an ad-hoc mix of milliseconds or seconds. Configuration properties can now be defined in a consistent and readable way. For example `server.session.timeout=5m`. Properties that were previously declared using seconds are annotated with `@DurationUnit` to ensure a smooth upgrade experience. For example `server.session.timeout=20` continues to mean 20 seconds. Fixes gh-11080
1 parent cbaf0fa commit 8f4bf23

File tree

69 files changed

+714
-519
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+714
-519
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticsearchHealthIndicatorAutoConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.elasticsearch;
1818

19+
import java.time.Duration;
1920
import java.util.Map;
2021

2122
import io.searchbox.client.JestClient;
@@ -77,8 +78,10 @@ public HealthIndicator elasticsearchHealthIndicator() {
7778

7879
@Override
7980
protected ElasticsearchHealthIndicator createHealthIndicator(Client client) {
81+
Duration responseTimeout = this.properties.getResponseTimeout();
8082
return new ElasticsearchHealthIndicator(client,
81-
this.properties.getResponseTimeout(), this.properties.getIndices());
83+
responseTimeout == null ? 100 : responseTimeout.toMillis(),
84+
this.properties.getIndices());
8285
}
8386

8487
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/elasticsearch/ElasticsearchHealthIndicatorProperties.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.elasticsearch;
1818

19+
import java.time.Duration;
1920
import java.util.ArrayList;
2021
import java.util.List;
2122

@@ -38,9 +39,9 @@ public class ElasticsearchHealthIndicatorProperties {
3839
private List<String> indices = new ArrayList<>();
3940

4041
/**
41-
* Time, in milliseconds, to wait for a response from the cluster.
42+
* Time to wait for a response from the cluster.
4243
*/
43-
private long responseTimeout = 100L;
44+
private Duration responseTimeout = Duration.ofMillis(100);
4445

4546
public List<String> getIndices() {
4647
return this.indices;
@@ -50,11 +51,11 @@ public void setIndices(List<String> indices) {
5051
this.indices = indices;
5152
}
5253

53-
public long getResponseTimeout() {
54+
public Duration getResponseTimeout() {
5455
return this.responseTimeout;
5556
}
5657

57-
public void setResponseTimeout(long responseTimeout) {
58+
public void setResponseTimeout(Duration responseTimeout) {
5859
this.responseTimeout = responseTimeout;
5960
}
6061

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/EndpointIdTimeToLivePropertyFunction.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.endpoint;
1818

19+
import java.time.Duration;
1920
import java.util.function.Function;
2021

2122
import org.springframework.boot.actuate.endpoint.cache.CachingOperationInvokerAdvisor;
23+
import org.springframework.boot.context.properties.bind.BindResult;
24+
import org.springframework.boot.context.properties.bind.Bindable;
25+
import org.springframework.boot.context.properties.bind.Binder;
26+
import org.springframework.core.env.Environment;
2227
import org.springframework.core.env.PropertyResolver;
2328

2429
/**
@@ -30,20 +35,24 @@
3035
*/
3136
class EndpointIdTimeToLivePropertyFunction implements Function<String, Long> {
3237

33-
private final PropertyResolver propertyResolver;
38+
private static final Bindable<Duration> DURATION = Bindable.of(Duration.class);
39+
40+
private final Environment environment;
3441

3542
/**
3643
* Create a new instance with the {@link PropertyResolver} to use.
37-
* @param propertyResolver the environment
44+
* @param environment the environment
3845
*/
39-
EndpointIdTimeToLivePropertyFunction(PropertyResolver propertyResolver) {
40-
this.propertyResolver = propertyResolver;
46+
EndpointIdTimeToLivePropertyFunction(Environment environment) {
47+
this.environment = environment;
4148
}
4249

4350
@Override
4451
public Long apply(String endpointId) {
45-
String key = String.format("management.endpoint.%s.cache.time-to-live",
52+
String name = String.format("management.endpoint.%s.cache.time-to-live",
4653
endpointId);
47-
return this.propertyResolver.getProperty(key, Long.class);
54+
BindResult<Duration> duration = Binder.get(this.environment).bind(name, DURATION);
55+
return duration.map(Duration::toMillis).orElse(null);
4856
}
57+
4958
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/CorsEndpointProperties.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet;
1818

19+
import java.time.Duration;
20+
import java.time.temporal.ChronoUnit;
1921
import java.util.ArrayList;
2022
import java.util.List;
2123

2224
import org.springframework.boot.context.properties.ConfigurationProperties;
25+
import org.springframework.boot.context.properties.bind.convert.DurationUnit;
2326

2427
/**
2528
* Configuration properties for MVC endpoints' CORS support.
@@ -58,10 +61,11 @@ public class CorsEndpointProperties {
5861
private Boolean allowCredentials;
5962

6063
/**
61-
* How long, in seconds, the response from a pre-flight request can be cached by
62-
* clients.
64+
* How long the response from a pre-flight request can be cached by clients. If a
65+
* duration suffix is not specified, seconds will be used.
6366
*/
64-
private Long maxAge = 1800L;
67+
@DurationUnit(ChronoUnit.SECONDS)
68+
private Duration maxAge = Duration.ofSeconds(1800);
6569

6670
public List<String> getAllowedOrigins() {
6771
return this.allowedOrigins;
@@ -103,11 +107,11 @@ public void setAllowCredentials(Boolean allowCredentials) {
103107
this.allowCredentials = allowCredentials;
104108
}
105109

106-
public Long getMaxAge() {
110+
public Duration getMaxAge() {
107111
return this.maxAge;
108112
}
109113

110-
public void setMaxAge(Long maxAge) {
114+
public void setMaxAge(Duration maxAge) {
111115
this.maxAge = maxAge;
112116
}
113117

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/WebMvcEndpointManagementContextConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private CorsConfiguration getCorsConfiguration(CorsEndpointProperties properties
7878
configuration.setExposedHeaders(properties.getExposedHeaders());
7979
}
8080
if (properties.getMaxAge() != null) {
81-
configuration.setMaxAge(properties.getMaxAge());
81+
configuration.setMaxAge(properties.getMaxAge().getSeconds());
8282
}
8383
if (properties.getAllowCredentials() != null) {
8484
configuration.setAllowCredentials(properties.getAllowCredentials());

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/AbstractRabbitListenerContainerFactoryConfigurer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected void configure(T factory, ConnectionFactory connectionFactory,
100100
factory.setDefaultRequeueRejected(configuration.getDefaultRequeueRejected());
101101
}
102102
if (configuration.getIdleEventInterval() != null) {
103-
factory.setIdleEventInterval(configuration.getIdleEventInterval());
103+
factory.setIdleEventInterval(configuration.getIdleEventInterval().toMillis());
104104
}
105105
ListenerRetry retryConfig = configuration.getRetry();
106106
if (retryConfig.isEnabled()) {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties config)
105105
factory.setVirtualHost(config.determineVirtualHost());
106106
}
107107
if (config.getRequestedHeartbeat() != null) {
108-
factory.setRequestedHeartbeat(config.getRequestedHeartbeat());
108+
factory.setRequestedHeartbeat(
109+
(int) config.getRequestedHeartbeat().getSeconds());
109110
}
110111
RabbitProperties.Ssl ssl = config.getSsl();
111112
if (ssl.isEnabled()) {
@@ -121,7 +122,8 @@ public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties config)
121122
factory.setTrustStorePassphrase(ssl.getTrustStorePassword());
122123
}
123124
if (config.getConnectionTimeout() != null) {
124-
factory.setConnectionTimeout(config.getConnectionTimeout());
125+
factory.setConnectionTimeout(
126+
(int) config.getConnectionTimeout().toMillis());
125127
}
126128
factory.afterPropertiesSet();
127129
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616

1717
package org.springframework.boot.autoconfigure.amqp;
1818

19+
import java.time.Duration;
20+
import java.time.temporal.ChronoUnit;
1921
import java.util.ArrayList;
2022
import java.util.List;
2123

2224
import org.springframework.amqp.core.AcknowledgeMode;
2325
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode;
2426
import org.springframework.boot.context.properties.ConfigurationProperties;
27+
import org.springframework.boot.context.properties.bind.convert.DurationUnit;
2528
import org.springframework.util.CollectionUtils;
2629
import org.springframework.util.StringUtils;
2730

@@ -74,9 +77,11 @@ public class RabbitProperties {
7477
private String addresses;
7578

7679
/**
77-
* Requested heartbeat timeout, in seconds; zero for none.
80+
* Requested heartbeat timeout; zero for none. If a duration suffix is not specified,
81+
* seconds will be used.
7882
*/
79-
private Integer requestedHeartbeat;
83+
@DurationUnit(ChronoUnit.SECONDS)
84+
private Duration requestedHeartbeat;
8085

8186
/**
8287
* Enable publisher confirms.
@@ -89,9 +94,9 @@ public class RabbitProperties {
8994
private boolean publisherReturns;
9095

9196
/**
92-
* Connection timeout, in milliseconds; zero for infinite.
97+
* Connection timeout; zero for infinite.
9398
*/
94-
private Integer connectionTimeout;
99+
private Duration connectionTimeout;
95100

96101
/**
97102
* Cache configuration.
@@ -258,11 +263,11 @@ public void setVirtualHost(String virtualHost) {
258263
this.virtualHost = ("".equals(virtualHost) ? "/" : virtualHost);
259264
}
260265

261-
public Integer getRequestedHeartbeat() {
266+
public Duration getRequestedHeartbeat() {
262267
return this.requestedHeartbeat;
263268
}
264269

265-
public void setRequestedHeartbeat(Integer requestedHeartbeat) {
270+
public void setRequestedHeartbeat(Duration requestedHeartbeat) {
266271
this.requestedHeartbeat = requestedHeartbeat;
267272
}
268273

@@ -282,11 +287,11 @@ public void setPublisherReturns(boolean publisherReturns) {
282287
this.publisherReturns = publisherReturns;
283288
}
284289

285-
public Integer getConnectionTimeout() {
290+
public Duration getConnectionTimeout() {
286291
return this.connectionTimeout;
287292
}
288293

289-
public void setConnectionTimeout(Integer connectionTimeout) {
294+
public void setConnectionTimeout(Duration connectionTimeout) {
290295
this.connectionTimeout = connectionTimeout;
291296
}
292297

@@ -557,9 +562,9 @@ public static abstract class AmqpContainer {
557562
private Boolean defaultRequeueRejected;
558563

559564
/**
560-
* How often idle container events should be published in milliseconds.
565+
* How often idle container events should be published.
561566
*/
562-
private Long idleEventInterval;
567+
private Duration idleEventInterval;
563568

564569
/**
565570
* Optional properties for a retry interceptor.
@@ -598,11 +603,11 @@ public void setDefaultRequeueRejected(Boolean defaultRequeueRejected) {
598603
this.defaultRequeueRejected = defaultRequeueRejected;
599604
}
600605

601-
public Long getIdleEventInterval() {
606+
public Duration getIdleEventInterval() {
602607
return this.idleEventInterval;
603608
}
604609

605-
public void setIdleEventInterval(Long idleEventInterval) {
610+
public void setIdleEventInterval(Duration idleEventInterval) {
606611
this.idleEventInterval = idleEventInterval;
607612
}
608613

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheProperties.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package org.springframework.boot.autoconfigure.cache;
1818

19+
import java.time.Duration;
1920
import java.util.ArrayList;
2021
import java.util.List;
21-
import java.util.concurrent.TimeUnit;
2222

2323
import org.springframework.boot.context.properties.ConfigurationProperties;
2424
import org.springframework.core.io.Resource;
@@ -141,24 +141,16 @@ public void setSpec(String spec) {
141141
public static class Couchbase {
142142

143143
/**
144-
* Entry expiration in milliseconds. By default the entries never expire. Note
145-
* that this value is ultimately converted to seconds.
144+
* Entry expiration. By default the entries never expire. Note that this value is
145+
* ultimately converted to seconds.
146146
*/
147-
private int expiration;
147+
private Duration expiration;
148148

149-
public int getExpiration() {
149+
public Duration getExpiration() {
150150
return this.expiration;
151151
}
152152

153-
/**
154-
* Return the expiration in seconds.
155-
* @return the expiration in seconds
156-
*/
157-
public int getExpirationSeconds() {
158-
return (int) TimeUnit.MILLISECONDS.toSeconds(this.expiration);
159-
}
160-
161-
public void setExpiration(int expiration) {
153+
public void setExpiration(Duration expiration) {
162154
this.expiration = expiration;
163155
}
164156

@@ -246,9 +238,9 @@ public void setConfig(Resource config) {
246238
public static class Redis {
247239

248240
/**
249-
* Entry expiration in milliseconds. By default the entries never expire.
241+
* Entry expiration. By default the entries never expire.
250242
*/
251-
private long timeToLive = 0;
243+
private Duration timeToLive;
252244

253245
/**
254246
* Allow caching null values.
@@ -265,11 +257,11 @@ public static class Redis {
265257
*/
266258
private boolean useKeyPrefix = true;
267259

268-
public long getTimeToLive() {
260+
public Duration getTimeToLive() {
269261
return this.timeToLive;
270262
}
271263

272-
public void setTimeToLive(long timeToLive) {
264+
public void setTimeToLive(Duration timeToLive) {
273265
this.timeToLive = timeToLive;
274266
}
275267

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CouchbaseCacheConfiguration.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.cache;
1818

19+
import java.time.Duration;
1920
import java.util.List;
2021

2122
import com.couchbase.client.java.Bucket;
@@ -59,11 +60,13 @@ public CouchbaseCacheConfiguration(CacheProperties cacheProperties,
5960
@Bean
6061
public CouchbaseCacheManager cacheManager() {
6162
List<String> cacheNames = this.cacheProperties.getCacheNames();
62-
CouchbaseCacheManager cacheManager = new CouchbaseCacheManager(
63-
CacheBuilder.newInstance(this.bucket)
64-
.withExpiration(this.cacheProperties.getCouchbase()
65-
.getExpirationSeconds()),
66-
cacheNames.toArray(new String[cacheNames.size()]));
63+
CacheBuilder builder = CacheBuilder.newInstance(this.bucket);
64+
Duration expiration = this.cacheProperties.getCouchbase().getExpiration();
65+
if (expiration != null) {
66+
builder = builder.withExpiration((int) expiration.getSeconds());
67+
}
68+
String[] names = cacheNames.toArray(new String[cacheNames.size()]);
69+
CouchbaseCacheManager cacheManager = new CouchbaseCacheManager(builder, names);
6770
return this.customizers.customize(cacheManager);
6871
}
6972

0 commit comments

Comments
 (0)