forked from deviceinsight/kafka-health-check
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update project for Spring Boot 3.3 and modern Java
Also drops the millisecond based config support, as that has not been mentioned in the README for many years. Due to breaking change, upping the major version
- Loading branch information
Showing
6 changed files
with
89 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 13 additions & 49 deletions
62
src/main/java/me/bvn13/kafka/health/KafkaHealthProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,24 @@ | ||
package me.bvn13.kafka.health; | ||
|
||
import java.time.Duration; | ||
|
||
public class KafkaHealthProperties { | ||
|
||
private String topic = "health-checks"; | ||
private Duration sendReceiveTimeout = Duration.ofMillis(2500); | ||
private Duration pollTimeout = Duration.ofMillis(200); | ||
private KafkaHealthCheckCacheProperties cache = new KafkaHealthCheckCacheProperties(); | ||
|
||
public String getTopic() { | ||
return topic; | ||
} | ||
|
||
public void setTopic(String topic) { | ||
this.topic = topic; | ||
} | ||
|
||
public Duration getSendReceiveTimeout() { | ||
return sendReceiveTimeout; | ||
} | ||
import org.springframework.boot.context.properties.bind.ConstructorBinding; | ||
|
||
public void setSendReceiveTimeout(Duration sendReceiveTimeout) { | ||
this.sendReceiveTimeout = sendReceiveTimeout; | ||
} | ||
|
||
@Deprecated | ||
public void setSendReceiveTimeoutMs(long sendReceiveTimeoutMs) { | ||
setSendReceiveTimeout(Duration.ofMillis(sendReceiveTimeoutMs)); | ||
} | ||
|
||
public Duration getPollTimeout() { | ||
return pollTimeout; | ||
} | ||
|
||
public void setPollTimeout(Duration pollTimeout) { | ||
this.pollTimeout = pollTimeout; | ||
} | ||
import java.time.Duration; | ||
|
||
@Deprecated | ||
public void setPollTimeoutMs(long pollTimeoutMs) { | ||
setPollTimeout(Duration.ofMillis(pollTimeoutMs)); | ||
} | ||
public record KafkaHealthProperties (String topic, Duration sendReceiveTimeout, Duration pollTimeout, KafkaHealthCheckCacheProperties cache){ | ||
|
||
public KafkaHealthCheckCacheProperties getCache() { | ||
return cache; | ||
@ConstructorBinding | ||
public KafkaHealthProperties { | ||
topic = coalesce(topic,"health-checks"); | ||
sendReceiveTimeout = coalesce(sendReceiveTimeout, Duration.ofMillis(2500)); | ||
pollTimeout = coalesce(pollTimeout, Duration.ofMillis(200)); | ||
cache = coalesce(cache, new KafkaHealthCheckCacheProperties()); | ||
} | ||
|
||
public void setCache(KafkaHealthCheckCacheProperties cache) { | ||
this.cache = cache; | ||
public KafkaHealthProperties(){ | ||
this(null,null,null,null); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "KafkaHealthProperties{" + "topic='" + topic + '\'' + ", sendReceiveTimeout=" + sendReceiveTimeout + | ||
", pollTimeout=" + pollTimeout + ", cacheProperties=" + | ||
cache + '}'; | ||
public static <T> T coalesce(T a, T b) { | ||
return a == null ? b : a; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 14 additions & 33 deletions
47
src/test/java/me/bvn13/kafka/health/KafkaHealthPropertiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,36 @@ | ||
package me.bvn13.kafka.health; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.params.provider.Arguments.arguments; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.context.properties.bind.Binder; | ||
import org.springframework.boot.context.properties.source.ConfigurationPropertySource; | ||
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource; | ||
|
||
import java.time.Duration; | ||
import java.util.stream.Stream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class KafkaHealthPropertiesTest { | ||
|
||
// @formatter:off | ||
// @formatter:off | ||
private static final ConfigurationPropertySource DURATION_PROPERTY_SOURCE = new MapConfigurationPropertySource(ImmutableMap.of( | ||
"kafka.health.topic", "custom-topic", | ||
"kafka.health.send-receive-timeout", "1m", | ||
"kafka.health.poll-timeout", "2s", | ||
"kafka.health.cache.maximum-size", "42" | ||
)); | ||
|
||
private static final ConfigurationPropertySource MILLISECONDS_PROPERTY_SOURCE = new MapConfigurationPropertySource(ImmutableMap.of( | ||
"kafka.health.topic", "custom-topic", | ||
"kafka.health.send-receive-timeout-ms", "60000", | ||
"kafka.health.poll-timeout-ms", "2000", | ||
"kafka.health.cache.maximum-size", "42" | ||
)); | ||
// @formatter:on | ||
|
||
@ParameterizedTest(name = "using {0} based setters") | ||
@MethodSource("configurationPropertySources") | ||
@SuppressWarnings("unused") | ||
public void test_that_properties_bind_to_KafkaHealthProperties(String sourceName, | ||
ConfigurationPropertySource propertySource) { | ||
|
||
KafkaHealthProperties kafkaHealthProperties = | ||
new Binder(propertySource).bind("kafka.health", KafkaHealthProperties.class).get(); | ||
|
||
assertThat(kafkaHealthProperties.getTopic()).isEqualTo("custom-topic"); | ||
assertThat(kafkaHealthProperties.getSendReceiveTimeout()).isEqualTo(Duration.ofMinutes(1)); | ||
assertThat(kafkaHealthProperties.getPollTimeout()).isEqualTo(Duration.ofSeconds(2)); | ||
assertThat(kafkaHealthProperties.getCache().getMaximumSize()).isEqualTo(42); | ||
} | ||
@SuppressWarnings("unused") | ||
@Test | ||
public void test_that_properties_bind_to_KafkaHealthProperties() { | ||
|
||
static Stream<Arguments> configurationPropertySources() { | ||
return Stream.of(arguments("Duration", DURATION_PROPERTY_SOURCE), | ||
arguments("long (milliseconds)", MILLISECONDS_PROPERTY_SOURCE)); | ||
} | ||
KafkaHealthProperties kafkaHealthProperties = | ||
new Binder(DURATION_PROPERTY_SOURCE).bind("kafka.health", KafkaHealthProperties.class).get(); | ||
|
||
assertThat(kafkaHealthProperties.topic()).isEqualTo("custom-topic"); | ||
assertThat(kafkaHealthProperties.sendReceiveTimeout()).isEqualTo(Duration.ofMinutes(1)); | ||
assertThat(kafkaHealthProperties.pollTimeout()).isEqualTo(Duration.ofSeconds(2)); | ||
assertThat(kafkaHealthProperties.cache().getMaximumSize()).isEqualTo(42); | ||
} | ||
} |