refactor(config): rename watching method and remove useless covariant

This commit is contained in:
2026-04-02 22:12:08 +08:00
parent 29d6546b07
commit f37bef57ba
2 changed files with 9 additions and 10 deletions

View File

@@ -14,14 +14,14 @@ object ConfigCenter : AutoCloseable {
private val log = LoggerFactory.getLogger(ConfigCenter::class.java) private val log = LoggerFactory.getLogger(ConfigCenter::class.java)
val paths = resolvePaths() val paths = resolvePaths()
private val registrations = mutableMapOf<Path, ConfigRegistration<out Config>>() private val registrations = mutableMapOf<Path, ConfigRegistration<Config>>()
private var watchExecutor: ExecutorService? = null private var watchExecutor: ExecutorService? = null
private var watchSupport: DirectoryWatchSupport? = null private var watchSupport: DirectoryWatchSupport? = null
@Synchronized @Synchronized
fun register(configurable: Configurable) { fun register(configurable: Configurable) {
val declared = configurable.declare() val declared = configurable.declare()
val normalized = mutableMapOf<Path, ConfigRegistration<out Config>>() val normalized = mutableMapOf<Path, ConfigRegistration<Config>>()
declared.forEach { (path, registration) -> declared.forEach { (path, registration) ->
val normalizedPath = normalizeRelativePath(path) val normalizedPath = normalizeRelativePath(path)
@@ -41,7 +41,7 @@ object ConfigCenter : AutoCloseable {
} }
@Synchronized @Synchronized
fun startWatching() { fun start() {
if (watchSupport != null) { if (watchSupport != null) {
return return
} }
@@ -109,9 +109,8 @@ object ConfigCenter : AutoCloseable {
return JSON.parseObject(Files.readString(file, StandardCharsets.UTF_8), registration.type()) as Config return JSON.parseObject(Files.readString(file, StandardCharsets.UTF_8), registration.type()) as Config
} }
@Suppress("UNCHECKED_CAST") private fun notifyReload(registration: ConfigRegistration<Config>, config: Config) {
private fun notifyReload(registration: ConfigRegistration<out Config>, config: Config) { registration.onReload(config)
(registration as ConfigRegistration<Config>).onReload(config)
} }
private fun toRelativeConfigPath(file: Path): Path? { private fun toRelativeConfigPath(file: Path): Path? {
@@ -151,7 +150,7 @@ object ConfigCenter : AutoCloseable {
abstract class Config abstract class Config
interface Configurable { interface Configurable {
fun declare(): Map<Path, ConfigRegistration<out Config>> fun declare(): Map<Path, ConfigRegistration<Config>>
fun register() { fun register() {
ConfigCenter.register(this) ConfigCenter.register(this)
} }

View File

@@ -61,7 +61,7 @@ class ConfigCenterTest {
declared.put(IDEMPOTENT_PATH, idempotentRegistration); declared.put(IDEMPOTENT_PATH, idempotentRegistration);
return declared; return declared;
}); });
ConfigCenter.INSTANCE.startWatching(); ConfigCenter.INSTANCE.start();
} }
@AfterAll @AfterAll
@@ -203,12 +203,12 @@ class ConfigCenterTest {
@Test @Test
@Order(7) @Order(7)
void testStartWatchingIsIdempotent() throws Exception { void testStartIsIdempotent() throws Exception {
Path file = configDir.resolve(IDEMPOTENT_PATH); Path file = configDir.resolve(IDEMPOTENT_PATH);
writeJson(file, "before-idempotent", 1); writeJson(file, "before-idempotent", 1);
waitForCount(idempotentRegistration, 1, 3000); waitForCount(idempotentRegistration, 1, 3000);
ConfigCenter.INSTANCE.startWatching(); ConfigCenter.INSTANCE.start();
int baseline = idempotentRegistration.reloadCount(); int baseline = idempotentRegistration.reloadCount();
writeJson(file, "after-idempotent", 2); writeJson(file, "after-idempotent", 2);