Use java.time.Duration for duration based properties. (#294)

* Use java.time.Duration for duration based properties.

* Update javadoc for period in ConfigReloadProperties.
This commit is contained in:
Mark Anderson
2018-12-14 16:29:58 +00:00
committed by Ryan Baxter
parent f335da672c
commit a5baf013f6
8 changed files with 81 additions and 53 deletions

View File

@@ -392,7 +392,7 @@ This means, for example, that using polling on file mounted secret sources does
| spring.cloud.kubernetes.reload.monitoring-secrets | Boolean | false | Allow monitoring changes in secrets
| spring.cloud.kubernetes.reload.strategy | Enum | refresh | The strategy to use when firing a reload (*refresh*, *restart_context*, *shutdown*)
| spring.cloud.kubernetes.reload.mode | Enum | event | Specifies how to listen for changes in property sources (*event*, *polling*)
| spring.cloud.kubernetes.reload.period | Long | 15000 | The period in milliseconds for verifying changes when using the *polling* strategy
| spring.cloud.kubernetes.reload.period | Duration| 15s | The period for verifying changes when using the *polling* strategy
|===
**Notes**:
- Properties under *spring.cloud.kubernetes.reload.* should not be used in config maps or secrets: changing such properties at runtime may lead to unexpected results;

View File

@@ -16,6 +16,8 @@
*/
package org.springframework.cloud.kubernetes.config.reload;
import java.time.Duration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
@@ -50,9 +52,9 @@ public class ConfigReloadProperties {
private ReloadDetectionMode mode = ReloadDetectionMode.EVENT;
/**
* Sets the polling period in milliseconds to use when the detection mode is POLLING.
* Sets the polling period to use when the detection mode is POLLING.
*/
private Long period = 15000L;
private Duration period = Duration.ofMillis(15000L);
public ConfigReloadProperties() {
}
@@ -93,12 +95,12 @@ public class ConfigReloadProperties {
return mode;
}
public Long getPeriod() {
return period;
public void setPeriod(Duration period) {
this.period = period;
}
public void setPeriod(Long period) {
this.period = period;
public Duration getPeriod() {
return period;
}
public void setMode(ReloadDetectionMode mode) {

View File

@@ -21,6 +21,9 @@ import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import java.time.Duration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.actuate.health.HealthIndicator;
@@ -78,11 +81,11 @@ public class KubernetesAutoConfiguration {
or(kubernetesClientProperties.getClientKeyPassphrase(),
base.getClientKeyPassphrase()))
.withConnectionTimeout(
or(kubernetesClientProperties.getConnectionTimeout(),
orDurationInt(kubernetesClientProperties.getConnectionTimeout(),
base.getConnectionTimeout()))
.withRequestTimeout(or(kubernetesClientProperties.getRequestTimeout(),
.withRequestTimeout(orDurationInt(kubernetesClientProperties.getRequestTimeout(),
base.getRequestTimeout()))
.withRollingTimeout(or(kubernetesClientProperties.getRollingTimeout(),
.withRollingTimeout(orDurationLong(kubernetesClientProperties.getRollingTimeout(),
base.getRollingTimeout()))
.withTrustCerts(or(kubernetesClientProperties.isTrustCerts(),
base.isTrustCerts()))
@@ -134,4 +137,22 @@ public class KubernetesAutoConfiguration {
return dat;
}
}
private static Integer orDurationInt(Duration dis, Integer dat) {
if (dis != null) {
return (int)dis.toMillis();
}
else {
return dat;
}
}
private static Long orDurationLong(Duration dis, Long dat) {
if (dis != null) {
return dis.toMillis();
}
else {
return dat;
}
}
}

View File

@@ -17,7 +17,8 @@
package org.springframework.cloud.kubernetes;
import java.util.List;
import java.time.Duration;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("spring.cloud.kubernetes.client")
@@ -37,12 +38,12 @@ public class KubernetesClientProperties {
private String clientKeyPassphrase;
private String username;
private String password;
private Integer watchReconnectInterval;
private Integer watchReconnectLimit;
private Integer connectionTimeout;
private Integer requestTimeout;
private Long rollingTimeout;
private Integer loggingInterval;
private Duration watchReconnectInterval;
private Duration watchReconnectLimit;
private Duration connectionTimeout;
private Duration requestTimeout;
private Duration rollingTimeout;
private Duration loggingInterval;
private String httpProxy;
private String httpsProxy;
private String proxyUsername;
@@ -161,51 +162,51 @@ public class KubernetesClientProperties {
this.password = password;
}
public Integer getWatchReconnectInterval() {
public Duration getWatchReconnectInterval() {
return watchReconnectInterval;
}
public void setWatchReconnectInterval(Integer watchReconnectInterval) {
public void setWatchReconnectInterval(Duration watchReconnectInterval) {
this.watchReconnectInterval = watchReconnectInterval;
}
public Integer getWatchReconnectLimit() {
public Duration getWatchReconnectLimit() {
return watchReconnectLimit;
}
public void setWatchReconnectLimit(Integer watchReconnectLimit) {
public void setWatchReconnectLimit(Duration watchReconnectLimit) {
this.watchReconnectLimit = watchReconnectLimit;
}
public Integer getConnectionTimeout() {
public Duration getConnectionTimeout() {
return connectionTimeout;
}
public void setConnectionTimeout(Integer connectionTimeout) {
public void setConnectionTimeout(Duration connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}
public Integer getRequestTimeout() {
public Duration getRequestTimeout() {
return requestTimeout;
}
public void setRequestTimeout(Integer requestTimeout) {
public void setRequestTimeout(Duration requestTimeout) {
this.requestTimeout = requestTimeout;
}
public Long getRollingTimeout() {
public Duration getRollingTimeout() {
return rollingTimeout;
}
public void setRollingTimeout(Long rollingTimeout) {
public void setRollingTimeout(Duration rollingTimeout) {
this.rollingTimeout = rollingTimeout;
}
public Integer getLoggingInterval() {
public Duration getLoggingInterval() {
return loggingInterval;
}
public void setLoggingInterval(Integer loggingInterval) {
public void setLoggingInterval(Duration loggingInterval) {
this.loggingInterval = loggingInterval;
}

View File

@@ -86,9 +86,9 @@
},
{
"name": "spring.cloud.kubernetes.client.watchReconnectInterval",
"type": "java.lang.Integer",
"description": "Reconnect Interval in milliseconds",
"defaultValue": 1000
"type": "java.time.Duration",
"description": "Reconnect Interval",
"defaultValue": "1s"
},
{
"name": "spring.cloud.kubernetes.client.watchReconnectLimit",
@@ -98,27 +98,27 @@
},
{
"name": "spring.cloud.kubernetes.client.connectionTimeout",
"type": "java.lang.Integer",
"description": "Connection timeout in milliseconds",
"defaultValue": 10000
"type": "java.time.Duration",
"description": "Connection timeout",
"defaultValue": "10s"
},
{
"name": "spring.cloud.kubernetes.client.requestTimeout",
"type": "java.lang.Integer",
"description": "Request timeout in milliseconds",
"defaultValue": 10000
"type": "java.time.Duration",
"description": "Request timeout",
"defaultValue": "10s"
},
{
"name": "spring.cloud.kubernetes.client.rollingTimeout",
"type": "java.lang.Long",
"description": "Rolling timeout in milliseconds",
"defaultValue": 900000
"type": "java.time.Duration",
"description": "Rolling timeout",
"defaultValue": "900s"
},
{
"name": "spring.cloud.kubernetes.client.loggingInterval",
"type": "java.lang.Integer",
"type": "java.time.Duration",
"description": "Logging interval",
"defaultValue": 20000
"defaultValue": "20s"
}
]
}

View File

@@ -65,7 +65,7 @@ public class LeaderInitiator implements SmartLifecycle {
hostPodWatcher.start();
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleAtFixedRate(leadershipController::update,
leaderProperties.getUpdatePeriod(), leaderProperties.getUpdatePeriod(), TimeUnit.MILLISECONDS);
leaderProperties.getUpdatePeriod().toMillis(), leaderProperties.getUpdatePeriod().toMillis(), TimeUnit.MILLISECONDS);
isRunning = true;
}
}

View File

@@ -17,6 +17,8 @@
package org.springframework.cloud.kubernetes.leader;
import java.time.Duration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
@@ -33,7 +35,7 @@ public class LeaderProperties {
private static final String DEFAULT_CONFIG_MAP_NAME = "leaders";
private static final long DEFAULT_UPDATE_PERIOD = 60000;
private static final Duration DEFAULT_UPDATE_PERIOD = Duration.ofMillis(60000);
private static final boolean DEFAULT_PUBLISH_FAILED_EVENTS = false;
@@ -75,7 +77,7 @@ public class LeaderProperties {
* Leadership status check period.
* Default: 60s
*/
private long updatePeriod = DEFAULT_UPDATE_PERIOD;
private Duration updatePeriod = DEFAULT_UPDATE_PERIOD;
/**
* Enable/disable publishing events in case leadership acquisition fails.
@@ -139,11 +141,11 @@ public class LeaderProperties {
this.leaderIdPrefix = leaderIdPrefix;
}
public long getUpdatePeriod() {
public Duration getUpdatePeriod() {
return updatePeriod;
}
public void setUpdatePeriod(long updatePeriod) {
public void setUpdatePeriod(Duration updatePeriod) {
this.updatePeriod = updatePeriod;
}

View File

@@ -29,6 +29,8 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
import static org.mockito.internal.verification.VerificationModeFactory.atLeastOnce;
import java.time.Duration;
/**
* @author Gytis Trikleris
*/
@@ -72,7 +74,7 @@ public class LeaderInitiatorTest {
@Test
public void shouldStart() throws InterruptedException {
given(mockLeaderProperties.getUpdatePeriod()).willReturn(1L);
given(mockLeaderProperties.getUpdatePeriod()).willReturn(Duration.ofMillis(1L));
leaderInitiator.start();
@@ -85,7 +87,7 @@ public class LeaderInitiatorTest {
@Test
public void shouldStartOnlyOnce() {
given(mockLeaderProperties.getUpdatePeriod()).willReturn(10000L);
given(mockLeaderProperties.getUpdatePeriod()).willReturn(Duration.ofMillis(10000L));
leaderInitiator.start();
leaderInitiator.start();
@@ -95,7 +97,7 @@ public class LeaderInitiatorTest {
@Test
public void shouldStop() {
given(mockLeaderProperties.getUpdatePeriod()).willReturn(10000L);
given(mockLeaderProperties.getUpdatePeriod()).willReturn(Duration.ofMillis(10000L));
leaderInitiator.start();
leaderInitiator.stop();
@@ -108,7 +110,7 @@ public class LeaderInitiatorTest {
@Test
public void shouldStopOnlyOnce() {
given(mockLeaderProperties.getUpdatePeriod()).willReturn(10000L);
given(mockLeaderProperties.getUpdatePeriod()).willReturn(Duration.ofMillis(10000L));
leaderInitiator.start();
leaderInitiator.stop();
@@ -119,7 +121,7 @@ public class LeaderInitiatorTest {
@Test
public void shouldStopAndExecuteCallback() {
given(mockLeaderProperties.getUpdatePeriod()).willReturn(10000L);
given(mockLeaderProperties.getUpdatePeriod()).willReturn(Duration.ofMillis(10000L));
leaderInitiator.start();
leaderInitiator.stop(mockRunnable);